Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1018495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:51:58+00:00 2026-05-16T10:51:58+00:00

I am a vb.net programmer switching to C#. I have the following code for

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:51:59+00:00Added an answer on May 16, 2026 at 10:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a C# programmer but I have to work with some VB.Net code
I am a .NET programmer who needs to port one good Desktop OTP system
I am a PHP programmer. I have no .Net coding experience (last seen it
I am a begginer C++ programmer. In C#, I used the System.Net.Sockets.Socket class, which
i am a vb.net programmer, and i have done lots of VBA with excel.
I'm a .NET programmer, and I'm developing a system that collects real time production
My other programmer download this code from the net #import UIImage+Alpha.h // Private helper
Good day (and happy New Year), I'm a beginner VB.Net programmer using VS 2008.
The following code is not entering the i for loop. I have had problems
I'm a Windows (native, not .NET) programmer and I'd like to port an application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.