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