Results 1 to 9 of 9

Thread: [RESOLVED] Static and non-static things

  1. #1

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Resolved [RESOLVED] Static and non-static things

    I am having a major problem here and I can't figure out how to solve it.

    I have written my own custom class. The class has some properties, an event and some functions.

    I want the class to be non-static, meaning that I can declare like this:
    Code:
    MyClass thing = new MyClass();
    And I can declare like that, but here's the next problem:

    If I make all the properties non-static the debugger says I need to have an instance each time I should set one of them...
    Doesn't that mean that I need to to do something like this:

    Code:
    IntervalProperty myprop = new IntervalProperty();
    myprop = 5;
    Instead of just this (static):

    Code:
    IntervalProperty = 5;
    ??

    And it's the same about everything :/
    I want to be able to call just MyClass.Start(); without any extra code...

    Urgent help needed, can't move forward in my project. Please show how to do with the properties, functions and events to make everything go around as a non static class or whateveryoucallit

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

    Re: Static and non-static things

    You are completely contradicting yourself there. First you say that you specifically want to this class to be used by creating instances and then you say the complete opposite, i.e. that you don't want to create an instance. You can't have your cake and eat it to.

    I think you should start by explaining exactly what this class represents and exactly how you envision it being used. We can then help you decide whether or not the class itself should be static and, if not, whether or not some of the members should be static. It is not an arbitrary decision.
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Static and non-static things

    If I make all the properties non-static the debugger says I need to have an instance each time I should set one of them...
    Doesn't that mean that I need to to do something like this:
    No it doesn't. It means this:
    Code:
    MyClass obj = new MyClass();
    
    obj.IntervalProperty = 5;
    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

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Static and non-static things

    You don't create a new instance for every property you want to set. You create one instance (or however many you need), and set all of its properties. An instance of a class is nothing but one particular object of that type. For example if you have a class Car then an instance of Car could be my car, or your car, or your neighbors car. Each of these are cars, but each instance has different properties.

  5. #5

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Static and non-static things

    Okay I will try to explain how I want It to work and then you can help me:

    The class is a worker class, it runs a backgroundworker.
    The class has some properties which I need to set before I start the class.
    These properties should be accessible from another class and within the class.
    The class also have some functions. Some of them, like the .Start() and .Cancel(), need to be accessible from another class. Some of them, should only be accessible from within the class (operations done by the backgroundworker).
    The class has atm one event but I will add more. The events should be accessible from another class.

    That is my explanation. The class worked good before, because it was all static. But then when I needed to make an event everything ****ed up. The event can't be static because then the debugger complains about me using the "this" thingy in the event declaration.
    Hope you can help me

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

    Re: Static and non-static things

    Just remove all the 'static' modifiers and create an instance. The only difference then is that instead of doing this:
    csharp Code:
    1. MyClass.SomeProperty = someValue;
    2. MyClass.SomeMethod();
    you do this:
    csharp Code:
    1. MyClass myObject = new MyClass()
    2.  
    3. myObject.SomeProperty = someValue;
    4. myObject.SomeMethod();
    You access each member via an instance of the class instead of the class itself. That's all there is to it.

    Of course, this also means that, if you want to be able to access those members somewhere other than where you create the object, you must have a reference to the object you created. You already do this with just about every other class you use though, because static classes are very much the exception rather than the rule.
    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

  7. #7

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Static and non-static things

    Thank you, solved

  8. #8
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] Static and non-static things

    Furthermore, making properties available outside of the class or keeping them only available from the same class has nothing to do with static vs non-static. For that you use the access modifiers, like public, private, internal, etc. For a short explanation (you can find the rest yourself): public properties are accessible anywhere, while private properties are accessible only in the same class.

  9. #9

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: [RESOLVED] Static and non-static things

    Thank you for the explanation

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