Re: Class Placement - stupid question
- From: "ABC" <abc@xxxxxxxxxxx>
- Date: Wed, 4 May 2005 22:43:37 +0430
> 1. Do I have to have the tank.cs class file, or can I just place it in the
> application namespace?
It doesn't matter in which file (*.cs) your class is. You can put your
class anywhere you like, as far as you specify the proper namespace for it.
(Class are put into different files usually for team works or for some other
purposes. It is not a "must" to do this in your applications.)
> 2. Shouldn't I be able to instantiate classes anywhere in my event code?
> If
> not, how can I?
Your classes are not instantiate as far as you have not used the "new +
className". This means that you can instantiate your class in your event
code. Sth like this will not instantiate the class unless you invoke the
Click method:
//--------------------------------------------------------
MyClass cls; // < - This is where you set the type of your identifier (or
object)
public void Click()
{
cls = new MyClass(); // <- This is where the class is instantiated.
cls.DoSth(); //< This is where you can use the instance members of the
class
}
//-------------------------------------------------------------
Also, you should make sure that your classes are instantiated before you can
use their instance (non-static) members. If in another method in your code
you try to use the class while it has not been instantiated, you will face
errors. As a result, you should try to whether instantiate all you classes
in the application's Main() entry (which is not so common) or instantiate
them in your methods wherever you like and "then" use its members,
.
- References:
- Class Placement - stupid question
- From: StillStuckOnJava
- Re: Class Placement - stupid question
- From: Ignacio Machin \( .NET/ C# MVP \)
- Re: Class Placement - stupid question
- From: StillStuckOnJava
- Re: Class Placement - stupid question
- From: Ignacio Machin \( .NET/ C# MVP \)
- Re: Class Placement - stupid question
- From: StillStuckOnJava
- Re: Class Placement - stupid question
- From: ABC
- Re: Class Placement - stupid question
- From: StillStuckOnJava
- Class Placement - stupid question
- Prev by Date: Re: No method in C# but exists in VB?
- Next by Date: Re: run test code
- Previous by thread: Re: Class Placement - stupid question
- Next by thread: DataGrid question
- Index(es):
Relevant Pages
|