How do I define a class to be accessible from other classes.

Hello

I have a class called CHardware.

I would like to define this class once as:

CHardware hardware = new Hardware();

Then be able to access hardware from any where in my program. Such as from within other classes.

How do I do that.

David

[291 byte] By [DavidAtPEfiberoptics] at [2007-12-24]
# 1

You should search the web for Object Oriented Programming.

But anyway, I suspect you have a class

namespace MyApplication.Objects

public class Hardware

{

// default constructor

public Hardware()

{

}

//properties

//methods

}

From another class if this class is in the same solution at the top of your code indicate

using MyApplication.Objects

now you can say:

Hardware myHardware = new Hardware();

If this class is in a separate dll then you have to add it as a reference, and use the same code.

ryan031581 at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 2

Hello

No that is not what I mean.

If I declare my hardware class as above

CHardware hardware = new Hardware();

in FormMain

publicpartialclassFormMain : Form

{

CHardware HardWare1 = newCHardware();

CHardware HardWare2 = newCHardware();

public FormMain()

{

InitializeComponent();

}

So I can call Hardware1.AnyMethod() from within FormMain.

But what I want to do is call Hardware1 from within another class, such as another form without having to declare it again.

DavidAtPEfiberoptics at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 3

Have a look at Static classes/members/variables. I think that is what you need.

http://msdn2.microsoft.com/en-us/library/79b3xss3.aspx

HTH
rv

RahulVirli at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 4

Make You class Static and use them any where in you application when you need it.

Best Regards,

RizwanSharp at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...