Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a class 1

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
I'm new to C#, that said, VB was very forgiving but C# is not.

I'm using VS 2005 & VS 2008.

Where do I instantiate my class if I want it available to me from all of my forms and all of my procedures?

I tried the Program module but it gave me an error saying in my button sub on my form that the class was does not exist in the current context.


I'm after a global object. :)

Thanks
 
Do you only ever want one of them?

You can use a singleton IF you use it correctly. Global variables/objects are a terrible idea but a singleton can be used as a good controller. It can provide you with access to the information you need from anywhere in a controlled manner.

The only downfall is that you can only have one of them in your application ever.

 
Right now, I'm just playing with it as I'm getting used to the C# language.

If I create it on the form then I want to assign something to one of it's properties in my button click, it tells me it does not exist.

I know the "no global" rule and I have used it in VB in the past but do I need to pass it into the button's parameters?

If so then how?

If I only use it within the button then it's useless for storing any sort of data to be used in another class or even it's own class.

I could make the member static, but then I'd have to reference the class itself, and not my new derived object.
 
What is the class used for. Typically if its for providing application settings you would use a Singleton. This guarantees there is only one in memory for each application instance. It would have to be stored in heap memory so then it could be available application wide.

Add a class to the project, and call it MySingleton

Code:
public sealed class MySingleton
{
    static mySingleton instance=null;
    private void MySingleton()
    {
    }

    public static MySingleton getInstance
    {
        get
        {
            if (instance==null)
                {
                    instance = new MySingleton();
                }
             return instance;
        }
    }
}

Then from anywhere in your application declare it like this

Code:
MySingleton mySingle = MySingleton.getInstance();

Now disecting the singleton pattern

Code:
public sealed class MySingleton

public means it is accessible inside and outside the assembly and to non subclasses.

sealed means it cannot be inherited from

Code:
static mySingleton instance=null;

static variables are tied to the class and not the instance. Having a static class variable means there is only one variable to hold a reference to the instance of the singleton.

Code:
  private MySingleton()
    {
    }

The constructor is declared as private in a singleton.

Code:
public static Singleton getInstance
    {
        get
        {
            if (instance==null)
                {
                    instance = new MySingleton();
                }
             return instance;
        }
    }

This is the plumbing work. Instead of calling 'new' on the class to instantiate a new object (and call the constructor) you call the static method getInstance. Its basically a class factory that only ever returns one instance of one type of class. By checking to see that instance==null before creating a new instance it guarantees you only get a single instance.

 
Excellent!
Thank you so much. You went above and beyond. :)

I was brought up in the VB world and coming into .NET I came right into web. Now that I'm playing with windows applications, I might as well learn C#. It'll help me in the web world too.
You can teach an old dog new tricks.

Thanks for the complete explanation.

~G

 
Check out the generic Singleton Provider here:
It saves you a lot of code, because you don't need to specifically set up each individual class to act as a singelton. If you want a singleton of ClassX, you can get it by using SingletonProvider<ClassX>.Instance

Hope this helps,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
You are welcome. I came into this world from VB too. Stick at it, there are loads of good tuts our there, and there is no greater form of learning than explaining. I would recommend trying to answer some articles with the knowledge that you do have eg VB, then as you get into C# you can spread your wings.
Thanks for the reply, made my day!

Charlie Benger-Stevenson
Hart Hill IT Ltd
 
Check out the generic Singleton Provider here:
It saves you a lot of code, because you don't need to specifically set up each individual class to act as a singelton. If you want a singleton of ClassX, you can get it by using SingletonProvider<ClassX>.Instance

Hope this helps,

Alex

Very well, my young Padawan ;-)

Christiaan Baes
Belgium

My Blog
 
Hehe now that you've shown yourself I can give you a star for it ;-)

I don't know how I ever lived without it

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top