473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Object creation

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
Nov 22 '05 #1
3 1376
Vannela <an*******@discussions.microsoft.com> wrote:
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();
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
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

---
Posted using Wimdows.net NntpNews Component - Posted from .NET's Largest Community Website: http://www.dotnetjunkies.com/newsgroups/
Nov 22 '05 #3
nickp <nickp@-NOSPAM-developernotes.com> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Richard | last post by:
Hi all, I am looking for some help on understanding the overhead associated with object creation in Java. I am writing an application where I have written a class to encapsulate some text...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
3
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
8
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
3
by: Nick Dreyer | last post by:
I was quite surprised to notice that Sub New() gets called twice, once at declaration time and once at creation time. I can't figure out why it would be called at declaration if there is no class...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
by: Dhananjay | last post by:
Hi All, I want to develop one application in vb.net for exchange 2000. I tried to add one contact with the code snippet below. The same logic is there for appointment on Microsoft's site. (I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.