Member Avatar

If there is something I understand least of in programming, it's accessing arrays. Does anyone know why this won't work? I can't find the answer anywhere. What I'm basically trying to do is take the months that have 30 days and perform a math problem, but I can't seem to access the array. 'month' is a string. It is giving me the error: Operator '==' cannot be applied to operands of type 'string' and 'string[]. Not sure what that means. I've looked for the error, googled it and whatnot but I can't find any answers anywhere. I appreciate any help.

string[] months30 = new string[4] { "September", "April", "June", "November" };



            if (month == months30)
            {

            }

Recommended Answers

An array is a collection of items. To access individual items you need to specify which index in the collection you want (ranging from 0 to N-1):

for (int i = 0; i < months30.Length; i++)
{
    if (months30[i] == month) // Now you can test against the entered …
Jump to Post

All 4 Replies

An array is a collection of items. To access individual items you need to specify which index in the collection you want (ranging from 0 to N-1):

for (int i = 0; i < months30.Length; i++)
{
    if (months30[i] == month) // Now you can test against the entered month
    {
        Console.WriteLine("{0} has 30 days", month);
    }
}

months30' is a string array (string[]) while, and I'm assuming here, 'month' is of type string.' Since they aren't the same type, you can't really compare them.

If you want to find if 'month' is in the array 'months30' you'll have to iterate through the array and compare elements.

Member Avatar

Thank you to the both of you! That makes sense. I'll try that out.

Member Avatar

I implemented it into my code and it worked. Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.