-5

I have created the following program:

namespace MyNamespace
{
    public enum MyEnum { Jan = 1, Feb = 2, Mar = 3, Apr = 4, May = 5, Jun = 6, Jul = 7, Aug = 8, Sep = 9, Okt = 10, Nov = 11, Dec = 12 }

    class Program
    {
        static void Main(string[] args)
        {
            private static string sourcePath = @"T:\his\is\my\source";
            private static string destinationPath = @"T:\his\is\my\destination";
        }
    }
}

That's all I have so far. The problem is that it doesn't compile like this. I get the following exception:

} expected

It only works when I leave the Main method body empty. When I use Ctrl + KD to format the code, it is formatted like this:

namespace MyNamespace
{
    public enum MyEnum { SomeField = 1, SomeOtherField = 2, ... }

    class Program
    {
        static void Main(string[] args)
        {
            private static string sourcePath = @"T:\his\is\my\source";
        private static string destinationPath = @"T:\his\is\my\destination";
    }
}
}

This totally doesn't make any sense. I can't even access the parameter args:

The name 'args' does not exist in the current context

I really don't know why my project is behaving like this. Did anyone else encounter the same problem? What should I do?

diiN__________
  • 7,393
  • 6
  • 42
  • 69

2 Answers2

10

You cannot declare the class fields inside a method.
Move the following lines out of it:

private static string sourcePath = @"T:\his\is\my\source";
private static string destinationPath = @"T:\his\is\my\destination";

Or if you want these variables to be local for the method, declare them as:

string sourcePath = @"T:\his\is\my\source";
string destinationPath = @"T:\his\is\my\destination";
Dmitry
  • 13,797
  • 6
  • 32
  • 48
2

Assuming you have copied the code directly, you have three main mistakes. Firstly in your enum you have some ellipses at the end:

public enum MyEnum { SomeField = 1, SomeOtherField = 2, ... }

You need to remove them and if necessary put in more values.

Secondly you are trying to declare what look like fields inside of you Main method. You need to move them out of the method:

private static string sourcePath = "T:\his\is\my\source";
private static string destinationPath = "T:\his\is\my\destination";

static void Main(string[] args)
{

}

Or make them method level variables by removing the access level modifiers and the static modifier:

static void Main(string[] args)
{
    string sourcePath = "T:\his\is\my\source";
    string destinationPath = "T:\his\is\my\destination";
}

Finally your paths are constructed like so:

"T:\his\is\my\source"

Here you have back slashes which in C# strings are used to create escape sequences. You need to either escape them with another back slash:

"T:\\his\\is\\my\\source"

Or use the @ modifier on them so they are treated verbatim, see this question for more information.:

@"T:\his\is\my\source"

Side note: It is probably better to either pass your paths in as command line arguments, or use a config file, rather than hardcoding them into your code. For example (untested code):

static void Main(string[] args)
{
    if (args.Length < 2)
    {
        throw new ArgumentOutOfRangeException("Two arguments must be supplied!");
    }

    //Add error checking as appropriate
    //You could also format the arguments like -s:... and -d:...
    //Then the order of the arguments will not matter

    string sourcePath = args[0];
    string destinationPath = args[1];
}
Community
  • 1
  • 1
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
  • I've added the ellipses only to show that there are other fields. I don't have them in my actual code. The problem was the variable declaration which Dmitry already mentioned. – diiN__________ Jan 12 '17 at 14:32
  • @diiN__________ Note my opening comment "Assuming you have copied the code directly" and there are 3 main mistakes with the code you posted whereas Dmitry only showed one. Next time post an [MCVE] otherwise you'll get extra info on what is wrong with your code like I posted – TheLethalCoder Jan 12 '17 at 14:34