Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# accesing non static member in a static function

Tags:

c#

static

member

So I have a function:

List<string> names = new string();

private static void getName(string name)
{
    names.add(name);
}

When I attempt to compile I get a: 'object reference is required for the non-static field' notice. What do I have to do to make this member (names) compatible with getName?

I need it to be non static or converted because I want to put the results into other non static functions and forms.

like image 380
Drake Avatar asked Jul 07 '11 19:07

Drake


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

You need to tell the system which list of names you're interested in. It's part of the state of an object, an instance of the class... but which one? Maybe you've created several instances of the class - maybe you've created no instances of the class. The static method has no visibility of that - so which instance do you want it to fetch the names variable value from?

To put it in another example, suppose we had a class like this:

public class Person
{
    public double MassInGrams { get; set; }
    public double HeightInMetres { get; set; }

    public static double ComputeBodyMassIndex()
    {
        // Which person are we interested in?
    }
}

Person p1 = new Person { MassInGrams = 76203, HeightInMetres = 1.8 };
Person p2 = new Person { MassInGrams = 65000, HeightInMetres = 1.7 };

double bmi = Person.ComputeBodyMassIndex();

What would you expect the result to be? You've asked the Person class to compute "the BMI" but without telling it whose BMI to compute. You need to give it that information.

Some options for your situation:

  • Change names to be static instead
  • Change the method to be an instance method
  • Pass in an instance of the class
  • Create an instance of the class, possibly returning it
  • Fetch an instance of the class some other way

By the way, that's a very strange method name for something which adds a name. It's also somewhat unconventional...

like image 173
Jon Skeet Avatar answered Sep 28 '22 13:09

Jon Skeet


You need to make names static if you want to use it from inside of a static method:

 // If this is static, you can use it from your static method
 static List<string> names = new List<string>();

The issue is that getName is defined on your type, not on an instance of the type. However, names is defined so each instance of your type gets its own value.

like image 22
Reed Copsey Avatar answered Sep 28 '22 12:09

Reed Copsey