tags:

views:

98

answers:

7

Hello,

I am a vb.net programmer switching to C#.

I have the following code for a console application (targeting NET20)

using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace eScoreSwapper
{
    class Program
    {
        private string _dbName = ConfigurationManager.AppSettings["dbName"];

        static void Main(string[] args) {}

        static void InitVars()
        {
            if (string.IsNullOrEmpty(_dbName)) _dbName = "";
        }
    }
}

This is giving a compile error in the if clause of InitVars for the _dbName variable:

Error   1   An object reference is required for the non-static field, method, or property 'eScoreSwapper.Program._dbName'   C:\Users\SethS\Documents\eScore\Versions\Trunk\dotNet\eScoreSwapper\eScoreSwapper\Program.cs    26  38  eScoreSwapper

Is it because it is true. C# does not allow you to reference private class fields unless they are declared static? I am sure I am doing something wrong.

While I am at it can I ask another C# question. Why is the if statement valid? Why are braces not required? Is it valid syntax as long as if condition is followed by a single expression (as in t-sql IF ).

Thanks for your help.

Seth

+5  A: 

You can't use an instance member variable in a static method. Either make your variable static or put the method and variable in an instance class and new it up in main on program.

The reason you can't access an instance variable from a static method becomes obvious when you think about the fact that the method exists "on the class" while the variable exists "on the instance". Hope that helps.

As a final comment, I'd just add that the fact that the compiler is giving you the error has nothing to do with the fact that your field is private; if your field was public, internal, etc. the compiler would still give you the same error: ie. static methods can't access instance variables.

Esteban Araya
You need to qualify member with *instance*.
ChaosPandion
+8  A: 

No, but private fields used by static method do. What I think you really want to do is remove the static from the declaration of InitVars().

I'm gonna assume that you previously tried:

static void Main(string[] args) 
{
   InitVars();
} 

and that failed, so you added the static to InitVars(). Wrong answer. Basically, you should pretend Main() is not part of the class.

static void Main(string[] args) 
{
   Program prog = new Program();
   prog.InitVars();
} 

As far the If() statement

if (string.IsNullOrEmpty(_dbName)) _dbName = "";

is just a short version of:

if (string.IsNullOrEmpty(_dbName)) 
     _dbName = "";

Now, in C-ish language, officially, an if() that is true, will execute the (one) next statement. But, you can make several statement into one statement, by wrapping them in curly-braces. But, if you really only have one statement, they aren't needed.

James Curran
Yep...this is exactly what happened. Thanks for the help.Seth
Seth Spearman
+1  A: 

You can't access a non-static member in a static method.

Braces are not required unless you want it to apply to more than one statement. But you should put them anyways.

BioBuckyBall
+1  A: 

To answer the question about braces, they are only truly mandatory in C# if there is more than one statement in the if block. Otherwise, you can put your one-liner on the same line as the if, or on the next line. I believe that it is generally advised in coding standards that you always use the brackets. It is always often advised not to put the entire series of code on all one line, but that kind of went out the window when we got tertiary and null coalescing operators.

DOK
+1  A: 

You can't access a non-static class member from within a static method without using a method parameter to bring a reference of the class member in.

The if syntax is normal c# (c++, c). Braces are only required for complex, multi-line blocks. The following else can have a singular line as well.

Joel Etherton
A: 

You're trying to access an instance field from a static context. Since _dbName isn't marked static, it could possibly be different for each instance of the Program class, so the static method InitVars() has no way of knowing what the right value is.

If every single instance of Program will have the same value for _dbName, then _dbName should be marked static. If different instances may have different values, then InitVars() should be an instance method (remove the static keyword and only invoke it on actual Program objects rather than from the static Main).

dpoeschl
A: 

"static" in VB parlance would be "shared".

boo