Results 1 to 7 of 7

Thread: [1.1] accessing a non-static member from a static method

  1. #1

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    [1.1] accessing a non-static member from a static method

    Hi. I have a class with a static method. In the static method I want to access a non-static variable of the same class and when I try to do it directly, i'm getting this error message:

    An object reference is required for the nonstatic field, method, or property 'Client.clTcpIp.MessToServer'

    string MessToServer;

    ...

    public static void method()
    {
    Access MessToServer....
    }


    Does anyone know how to solve this error?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [1.1] accessing a non-static member from a static method

    A static method is called on the class itself, not an instance. You cannot access an instance variable within a static method because there is no current instance. You can call a static method whether you have created one instance, one hundred instances or no instances at all. Exactly which instance would this instance variable be a member of? If you want to access an instance in a static method you have to pass the instance as a parameter. Array.Sort is an example of this technique. You don't call Sort on an array object. You call it on the class and pass the array object as a parameter:
    VB Code:
    1. int[] myArray = {5, 3, 7, 1, 2, 8, 6};
    2.  
    3. Array.Sort(myArray);
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [1.1] accessing a non-static member from a static method

    Either you pass an object instance to the static method, or you make the variable static.

    You can think of static class elements as one fixed instance of a class, if you like, that contains only the static members. All the non-static stuff can be in inifinte amounts of instances. No instance knows about any other.

  4. #4

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: [1.1] accessing a non-static member from a static method

    What I did was to make the variable static. I did this:

    string x;
    static string y;

    ...
    y = x;

    public static void method()
    {
    Access y; // and it worked.
    }

    I understand what you was saying about the object reference but that would be more difficult for me since I'm working with threads and it just looks complicated. One question, what I did there with converting from a normal to static, is there anything wrong with that or could I expect any side effects?

    Jennifer. Nice to see you back penegate, i missed you!

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [1.1] accessing a non-static member from a static method

    Sorry Jen, I've been hanging around the web development forum too much

    There shouldn't be any side effects... (Famous last words ) As long as you understand the difference between the two.

    If you need multiple instances of the class, then you'd have to make both method and variable non-static. That does change the approach required a bit.

  6. #6

    Thread Starter
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: [1.1] accessing a non-static member from a static method

    I tried passing the non-static member as an argument to a method. I have a little problem though. The method I'm passing it to i'm using to create a thread thus I have something like this:

    Code:
    MainThread = new Thread(new ThreadStart(StartListening));
    MainThread.Start();
    
    ....
    
    public static void StartListening(dattype variable)
    ......
    but its not working. I tried doing this: new Thread(new ThreadStart(StartListening(variable)));

    what am i doing wrong?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [1.1] accessing a non-static member from a static method

    I'm guessing that the IDE told you that the signature the StartListening method is not what's required. The method that you pass to the ThreadStart constructor cannot have any arguments, nor can it return a value. .NET 2.0 has the ParameterizedThreadStart class that does allow you to use a method with arguments but that's not available to you. That means that you're either going to have to set a variable, which will have to be static as penagate says, from which the thread can retrieve the data, or find another way. One common method of passing data to threads is to create a class especially for the purpose. You create an instance of this class and assign the data to its fields or, preferably, write-only properties, then use a method of that instance as the entry point for the thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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