Initialize in and unit (cs file)

L

laurent.sass

Hello,

I want to know how could i have an initialize method in a namespace.
I want to make an auto-registration component like that :

in ParentCollectionUnit

public class myParentCollection
{
public static GetInstance();
// it is a singloton parent with a addSonCollection
public addSonCollection(SonCollection NewSon);
}

in a specific SonCollection

public class SonCollection: MySpecificSon
{

}

this is the initialize process i want to make.
ParentCollectionUnit.GetInstance().addSonCollection(MySpecificSon);

Like that, i can have one new unit with a specific SonCollection who
are put in my project and will be automaticly refered by :
myParentCollection.

I'm used Visual Studio 2003.

Thanks for your help.
 
J

Joanna Carter [TeamB]

<[email protected]> a écrit dans le message de (e-mail address removed)...

| I want to know how could i have an initialize method in a namespace.
| I want to make an auto-registration component like that :

No, this is not possible in the unit alone, you are going to need a
non-instantiable class with static methods.

namespace MyCompany.MyProject
{
public class ParentCollection
{
private static SonCollection sons = new SonCollection();

private ParentCollection() { }

public static SonCollection Sons
{
get { return sons; }
}
}
}

Now you can do :

{
ParentCollection.Sons.Add(new Son());
...
}

Joanna
 
L

laurent.sass

I think it's very stange that isn't possible.
Because dotnet is compatible with COM and it is a COM method.

In VB we can create an variable with initialisation like that :
Dim myObject = myParent.mySingloton().AddSon(myLocalSon);
But in C#, all variables are on a class.

In Delphi we can use an Initialisation part for writing this code.
C++ borland used the same method.

I'm very surprise.
 
J

Joanna Carter [TeamB]

<[email protected]> a écrit dans le message de (e-mail address removed)...

| I think it's very stange that isn't possible.
| Because dotnet is compatible with COM and it is a COM method.

Not necessarily; there is nothing that says that all units contain COM
methods, therefore why would you expect all languages to support procedures
in the unit ?

| In VB we can create an variable with initialisation like that :
| Dim myObject = myParent.mySingloton().AddSon(myLocalSon);
| But in C#, all variables are on a class.

| In Delphi we can use an Initialisation part for writing this code.
| C++ borland used the same method.

Both VB for .NET and Delphi for .NET create a wrapper class for the code
unit; just because you don't see it in the code, doesn't mean the compiler
doesn't make such a class. And that class contains static methods that are
constructed out of your unit procedures.

Welcome to the world of "real" OO programming :)

Joanna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top