473,503 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

create a new instance of Object ?!

24 New Member
dear friends
I want to Create new instance of object with using Type with this code;


Expand|Select|Wrap|Line Numbers
  1. class Myclass(){}
  2.  
  3. class OtherClass()
  4. {
  5.  
  6. Myclass mc =  CreateNewInstance(typeof(MyClass));
  7.  
  8. public ???? CreateNewInstance(Type e)
  9. {
  10.  return // how can i create a new instance of Myclass here ? 
  11. }
  12. }

is it possible to use this method o I have to find other solution .
please help.


ali.
Apr 28 '09 #1
3 3437
Plater
7,872 Recognized Expert Expert
Well you can use the Reflection namespace to instanciate instances of objects, but I think the return value will be "object" and you would need to type-cast it
Apr 28 '09 #2
PRR
750 Recognized Expert Contributor
Type Class is used for reflection purpose... Like at run time you want to compare object whether they belong to same base type or are objects of same type ... You cant return a Type and then cast it to another class object ...
Apr 28 '09 #3
vekipeki
229 Recognized Expert New Member
You might want to take a look at Activator.CreateInstance method.

This should also work:

Expand|Select|Wrap|Line Numbers
  1. class OtherClass
  2. {
  3.     public static T CreateInstance<T>() where T : new()
  4.     {
  5.         return new T();
  6.     }
  7. }
(if I understood correctly what you are asking for)

In that case you would use it like this:

Expand|Select|Wrap|Line Numbers
  1. MyClass obj = OtherClass.CreateInstance<MyClass>();
Apr 30 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

15
12765
by: Brian Rogers | last post by:
Hello everyone, I apologize for the cross and re-post, but I am still searching for an answer. Why can C++ can create this object, but C# can't? I am trying to create an instance of the...
3
4307
by: equip200 | last post by:
I have a third party program that has the ability to control my program trough ActiveX/COM. My program is built in c#? What would I need to do to have the third party program connect to my...
4
17000
by: Ray | last post by:
I want to dynamically load DLLs (created from VB) and instantiate a class with a particular name, like "ProcessClass". I am able to load the DLL and confirm there is a class by that name BUT I...
2
2106
by: Markus Prediger | last post by:
Hi NG, I have an asp.net project that uses an vb6 com object for some database-manipulation (I cannot rewrite it in .net, sorry, its not my decision). I want it to be instanciated seperately...
6
2401
by: Rene Mansveld | last post by:
Hi, how can I create an instance (object) of a class (form) if I only know the classname (VB.NET 1.0)? I need to do this in a complex app where jobs consist of parts. Each part's data is saved...
18
7365
by: Sandra-24 | last post by:
Can you create an instance of a subclass using an existing instance of the base class? Such things would be impossible in some languages or very difficult in others. I wonder if this can be done...
6
1643
by: madpython | last post by:
For the pure theory sake and mind expansion i got a question. Experimenting with __new__ i found out how to create a singleton. class SingleStr(object): def __new__(cls,*args,**kwargs):...
2
6549
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
2
12759
by: ashishjainism | last post by:
I am unable to install my windows service and the installutil is throwing the following error: Running a transacted installation. Beginning the Install phase of the installation. See the...
2
2094
by: =?Utf-8?B?T2xkQnV0U3RpbGxMZWFybmluZw==?= | last post by:
I want create an instance of an object which accepts a Data Reader as a constructor. I realize that I could create a gigantic case statement in which I test for each text name, but that case...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.