CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2008
    Posts
    154

    questions about Fields, static non-static etc

    C#
    I am working on a 2d hex map with XNA. Right now I am figuring out the math to correctly plotting hexes. I have a question on fields and their variables. Sometimes you can do simple math with variables like this

    int something = 1 * 5;

    however if we have something like this

    Code:
    int anumber = 2;
    int something = 1 * anumber;
    we get an error

    ***A field initializer cannot reference the non-static field, method, or property***

    there seems to be 2 ways around it; I can do this

    Code:
    static int anumber = 2;
    int something = 1 * anumber;
    or this

    Code:
            int something
            {
                get
                {
                    int result = 1 * anumber;
                    return result;
                }
            }
    Can somebody tell me the difference here? I need some sort of explanation on static fields and this other thing.

  2. #2
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: questions about Fields, static non-static etc

    fields can not access other fields because their value is unknown unless static (they use a default initializer), they simply can't be resolved at compile time.

    static fields can however, since they're resolved statically before the containing type is constructed.

    Field:
    Code:
    public MyClass()
    {
       mIsInitialized = true; // this is usually the preferred initialization pattern for instanced fields.
    }
    
    private bool mIsInitialized;
    Property:
    Code:
    public bool IsInitialized
    {
       get { return mIsInitialized; }
       set { mIsInitialized = value; }
    }
    
    public MyClass()
    {
       mIsInitialized = true;
    }
    
    private bool mIsInitialized;

  3. #3
    Join Date
    Dec 2009
    Posts
    22

    Re: questions about Fields, static non-static etc

    I'm guessing you are trying to access a member of an object inside a static method of that class that created the obejct?

    This is not possible.
    A static method is method on the class, not on the object created from the class
    A non-static member of a class is created on run-time in the object.

    This means that when you call a static method, you call a method "outside" the object and hence cannot access non-static members "inside" the object.

    A solution could be to make the static method non-static, or to make the non-static member static. However if you make the method non-static it means that a new method is created along with every object (which is usually what you want), or if you make the non-static member static, there will only be one "instance" of that member shared among all the objects (which is usually not what you want).

    The best way to solve the problem depends on the nature of the objects/classes that you are manipulating. Does it make sense to have a member that is shared between all the objects of the class, or does it make more sense to have a member for each class.

  4. #4
    Join Date
    Jun 2008
    Posts
    154

    Re: questions about Fields, static non-static etc

    well I like this example

    Code:
    public MyClass()
    {
       mIsInitialized = true; // this is usually the preferred initialization pattern for instanced fields.
    }
    
    private bool mIsInitialized;
    Does that mean I could rewrite my (unneeded property) to be

    Code:
    int anumber = 2;
    public int something
    {
        something = 1 * anumber;
    }
    it just seems like if I wanted something to be unchanged I would make it a Constant
    or if I wanted it protected I would make it private

    mathematically if ThisVariable = 6 then I should be able to do
    ThisOtherVariable = 8 + ThisVariable

    but C# doesn't like that. Does this sound naive? I dunno to me it seems like that creators of C# have plenty of protection tools I don't know why the above example is not made available.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: questions about Fields, static non-static etc

    Quote Originally Posted by bixel View Post
    well I like this example
    mathematically if ThisVariable = 6 then I should be able to do
    ThisOtherVariable = 8 + ThisVariable

    but C# doesn't like that. Does this sound naive? I dunno to me it seems like that creators of C# have plenty of protection tools I don't know why the above example is not made available.
    That comparison makes no sense; this is not math, it is programming. They are completely different fields, you may as well compare programming to cooking. The fact is that there is no guaranteed initialization order for instance fields, so no, you can't do what you want. It is simple enough to just initialize them in a method, so what's wrong with that?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured