Object creation

V

Vannela

I create a instance of a .cs class
in this way

assume Class1.cs is my C# class file

i create a instance in this way

Class1 instance1=new Class1();

THEN IS THERE ANY WAY TO USE THIS INSTANCE VARIABLE TO
CREATE FOR DIFFERENT CLASS OBJECT

FOR EXAMPLE IF I HAVE GOT SOME OTHER CLASS File BY NAME
Class2.cs and there is a function in it by name "funct1"

then i can i say

Class2 instance2 = new Class2();

instance1=instance2.funct1();

Because this is done in the unmanaged code how can i
achieve this concept in C# . Please send me few code
snippets .

Thank you
 
J

Jon Skeet [C# MVP]

Vannela said:
I create a instance of a .cs class
in this way

assume Class1.cs is my C# class file

i create a instance in this way

Class1 instance1=new Class1();

THEN IS THERE ANY WAY TO USE THIS INSTANCE VARIABLE TO
CREATE FOR DIFFERENT CLASS OBJECT

FOR EXAMPLE IF I HAVE GOT SOME OTHER CLASS File BY NAME
Class2.cs and there is a function in it by name "funct1"

then i can i say

Class2 instance2 = new Class2();

instance1=instance2.funct1();

Because this is done in the unmanaged code how can i
achieve this concept in C# . Please send me few code
snippets .

Just declare Class2.funct1 to return a Class1 reference, eg:

class Class2
{
public Class1 funct1()
{
return new Class1();
}
}
 
N

nickp

Vannela
I would just like to include the idea that Jon suggests is correct, but you might consider writing a sole class to handle such a task. This is formally known as a class factory. You can use Reflection to help you with this, here is an example

public sealed ClassFactor

public ClassFactory(){
private static ClassFactory _instance

// Other methods and properties could be defined here

public ClassFactory Instanc

ge

if(_instance == null
lock(typeof(ClassFactory)
if(_instance == null
_instance = new ClassFactory()

return _instance



public static MyClass CreateClass(string name

MyClass cls = null
Object o = Activator.CreateInstance(Type.GetType(name))
cls = o as MyClass
return cls





Hope this helps

-Nick Parker
 
J

Jon Skeet [C# MVP]

nickp said:
Vannela,
I would just like to include the idea that Jon suggests is correct,
but you might consider writing a sole class to handle such a task. This
is formally known as a class factory. You can use Reflection to help you
with this, here is an example:


public sealed ClassFactory
{
public ClassFactory(){}
private static ClassFactory _instance;

// Other methods and properties could be defined here.

public ClassFactory Instance
{
get
{
if(_instance == null)
lock(typeof(ClassFactory))
if(_instance == null)
_instance = new ClassFactory();


return _instance;
}
}

This is an attempt to use the double-checked locking algorithm - which
doesn't work under .NET (i.e. it's not threadsafe).

See http://www.pobox.com/~skeet/csharp/singleton.html

(Note that singletons have private constructors rather than public ones
- the class above isn't a singleton, but it acts like one if you don't
use the constructor directly.)
 

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