Resolved Several errors came up while programming, I don't understand..

Program4Food

Active member
Joined
Nov 22, 2021
Messages
40
Programming Experience
10+
I was learning the switch case function and was adding line 25, and I have no clue what happened (maybe I hit another key somewhere??) and then
I got several errors and I don't even understand them in order for me to resolve it.

Some of the errors I am looking at make no sense when you review the line of code it references, and I never even changed that line! (line 21)

I need to know what the error is trying to cryptically tell me so I can prevent/fix in the future.

C#:
using System;
using System.Collections.Generic;
using System.Text;

namespace Console_switch_case
{
    class Program
    {
        static void Main(string[] args)

            {
}
        {

            // define variables
            string[] MyValues;
            int ValuesLength;
            string MyValuesCombined;

            
            MyValues = new String[]{"one", "two", "three", "four", "five"};
            MyValuesCombined = string.Join(",", MyValues);
            ValuesLength = MyValues.Length;
            Console.WriteLine("The number of elements in MyValues is {0}\r\n\", ValuesLength);
            Console.WriteLine("The elements of the string array is {0}\r\n\r\n", MyValuesCombined);



            for (int i = 0; i < ValuesLength; i++)
            {
                // Console.WriteLine("Value of i: {0}", i);
                Console.WriteLine("Element number {0} has the value of {1}", i, MyValues[i]);

                switch (MyValues[i])
                {

                    case "one":
                        Console.WriteLine("one spelled out is the number 1");
                        break;

                    case "four":
                        Console.WriteLine("four spelled out is the number 4");
                        break;

                }

                Console.WriteLine("\r\n\r\n");

            }



            // pause for key press
            Console.WriteLine("\r\n\r\nPress a key to exit...");
            Console.ReadKey();

        }
    }
}



Error 9 Type or namespace definition, or end-of-file expected F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 57 9 Console-switch_case
Error 12 The type or namespace name 'iAttribute' could not be found (are you missing a using directive or an assembly reference?) F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 32 90 Console-switch_case
Error 14 The type or namespace name 'iAttribute' could not be found (are you missing a using directive or an assembly reference?) F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 34 34 Console-switch_case
Error 11 The type or namespace name 'i' could not be found (are you missing a using directive or an assembly reference?) F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 32 90 Console-switch_case
Error 13 The type or namespace name 'i' could not be found (are you missing a using directive or an assembly reference?) F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 34 34 Console-switch_case
Error 1 Newline in constant F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 24 31 Console-switch_case
Error 3 Invalid token '=' in class, struct, or interface member declaration F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 21 22 Console-switch_case
Error 2 Invalid token '{' in class, struct, or interface member declaration F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 13 9 Console-switch_case
Error 4 Invalid token '{' in class, struct, or interface member declaration F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 21 36 Console-switch_case
Error 6 Expected class, delegate, enum, interface, or struct F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 32 92 Console-switch_case
Error 7 Expected class, delegate, enum, interface, or struct F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 34 36 Console-switch_case
Error 5 A namespace cannot directly contain members such as fields or methods F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 22 13 Console-switch_case
Error 8 A namespace cannot directly contain members such as fields or methods F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 54 13 Console-switch_case
Error 10 'Console_switch_case.Program.MyValues' is a 'field' but is used like a 'type' F:\VisualStudio\2012\Console-switch_case\Console-switch_case\Program.cs 21 13 Console-switch_case
 
Move line 12 to the end (line 60). You can see the } there would end the Main block, making everything that follows wrong.
Then fix line 24 (remove escape \ before ")
 
Move line 12 to the end (line 60). You can see the } there would end the Main block, making everything that follows wrong.
Then fix line 24 (remove escape \ before ")

Man, I had a feeling it some something simple I was overlooking, I examined the code line by line and totally over looked that extra escape \ (It had dual \r\n, but I removed one pair; evidently I didnt get it all)

And the } that was far left on line 12, WTH was I thinking? I dont recall what I was doing, but it was far left and honestly dont think I saw it when I was counting curly brackets!

THANK YOU JOHN!!
Its al good now!
 
As a quick aside, the C# style/idiom is not too use "\r\n". Prefer to just call Console.WriteLine() or append Environment.NewLine.
 
While JohnH's suggestions made it work alright, I still question that little circus of curly brackets in lines 11-13 of your original post. Moving the middle one to the end now leaves you with a superfluous extra level of nesting. Not a problem, but no good reason for it. It seems important to me to always neatly line up your curly brackets. This usually makes you spot things like this.
 
I had a CS teacher that was obsessed with alignment of indentations in Pascal and C. At first we thought he was a bit OCD, but later we learned that he was trying to teach us a programming life skill, because he once spent a few days trying to uncover a misplaced curly brace when all he had was print outs of the 4K lines of code from a group project he was in.
 
I had a CS teacher that was obsessed with alignment of indentations in Pascal and C. At first we thought he was a bit OCD, but later we learned that he was trying to teach us a programming life skill, because he once spent a few days trying to uncover a misplaced curly brace when all he had was print outs of the 4K lines of code from a group project he was in.
In my experience, the benefits of correct and tidy indentation (and tabulation, IMO) can hardly be overstated. Not just in code, but also in configuration files and such. I've seen so many people running into problems just because they carelessly jumble things together haphazardly without any concern for readability and maintainability.
 
Back
Top Bottom