I'm having big trouble understanding this.

namespace nspace
{
    
    public class eclass
    {
        public double elevation { get; set; }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            eclass first = new eclass();
            first.elevation = 500;
            Console.WriteLine(first.elevation);
            Console.ReadKey();
        }

        static void setup()
        {
            Console.WriteLine(eclass.first.elevation); // line dosent work
            Console.ReadKey();
        }

    }

}

I cannot output the value where it is commented, and I dont understand why, or how it can be possible, I need to set the properties of that object anywhere in my code.

can someone who nows explain my wrongdoing?

You need to understand variable scope.

first only exists in the block where it was declared, it is a method variable and isn't visible outside the method.

You instantiated eclass as first local to your Main method.
The method setup never can see first.
If you want that behaviour take line 13 out of the Main method and put it before it but still in the class program.

Hi, and thanks for replies.

I understand scope (basically) and tried having the line "eclass first = new eclass();" outside of Main Method, but in Program class, but then "first" instance is not aknwledged anywhere, in either the Main method, or the setup method. So I cannot set or get.

I have a feeling im doing something fundamentily wrong here.

public class MyClass {
    public MyClass(int v) {
        MyValue = v;
    }

    public int MyValue {get; set;}
}

public class Program {
    static MyClass myStatic = new MyClass(1);

    MyClass myClassVar = new MyClass(2);

    public static void Main() {
        MyClass myMethodVar = new MyClass(3);
    }

    public void MyMethod1() {
        Console.WriteLine(Program.myStatic.MyValue); // static always visible
        Console.WriteLine(myClassVar.MyValue); // Since this isn't a static method it can see instance variables
        Console.WriteLine(myMethodVar.MyValue); // error, cannot see method variables
    }
}

Given this what is it you are trying to do?

Hmmm, that looks good and runs without error, except I cannot call MyMethod1() from Main because it cannot see it, and says it needs an object reference now.

You think there may be something wrong with my IDE? I'm using vs2010.

EDIT: I rebooted, no difference.

I just want to create 5-6 instances of eclass which will hold a few properties, and be able to get and set them from anywhere in my code. Global Instances if you will, I need them to live the duration of the script.

The number of instances may vary or I might have created them static.

Nothing wrong, you still aren't fully aware of scope :)

Since MyMethod1() isn't static, you have to create an instance of the object first:

public void Main() {
    Program myProgram = new Program();
    myProgram.MyMethod1();
}

Thanks, I really appreciate you time. Unfortunately, whail you have solved my immedite problem I still dont understand it :(

I'm inside the Program Class, and have to create a new instance of the class I am already in to call a method which is in the very same class?

It all seems overly complicated, its probably why I shy away from classes and such, I'm still trying to get my head around it though, before I revert to just using a global array and type conversions.

From where I was with my code, It seemed I was just missing a little detail which I could not put my finger on, but now I'm even more confused.

Thanks anyway Momerath.

Objects and classes are confusing at first, but once you grasp the concept you'll wonder why you were ever confused by them. Just keep with it.

And remember, a class is not an object, it is a 'template' for creating an object. Without an object to work with you can only access static parts of the class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.