Member Avatar for lithium112

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

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 for lithium112

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

Member Avatar for lithium112

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.