PC Review


Reply
Thread Tools Rate Thread

How to create an instance of a class from a variable?

 
 
teel
Guest
Posts: n/a
 
      14th Feb 2007
Hi there,
I need to create an instance of a class whose name I have in a string
variable, let's say that:
string myTypeName = "MyType";

and in a result I need the equivalent of:
MyType obj = new MyType();

I've tried something like Type.GetType which returns the System.Type
but I don't know how to create a new object with this. My .NET version
is 1.1

I'd be grateful for help.

Best regards
teel

 
Reply With Quote
 
 
 
 
pfc_sadr@hotmail.com
Guest
Posts: n/a
 
      14th Feb 2007
1.1 does not support classes; sorry

PFC Sadr



On Feb 14, 5:52 am, "teel" <tlo...@gmail.com> wrote:
> Hi there,
> I need to create an instance of a class whose name I have in a string
> variable, let's say that:
> string myTypeName = "MyType";
>
> and in a result I need the equivalent of:
> MyType obj = new MyType();
>
> I've tried something like Type.GetType which returns the System.Type
> but I don't know how to create a new object with this. My .NET version
> is 1.1
>
> I'd be grateful for help.
>
> Best regards
> teel



 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      14th Feb 2007
Activator.CreateInstance(type)
or
type.GetConstructor(Type.EmptyTypes).Invoke(null)

(or a range of similar)

*However* note that you will only normally be able to use "object" as
the receiving variable. You can get around this by casting to
base-classes and interfaces if you know that the concrete type will
(at runtime) support it, e.g.

using System;
using System.Windows.Forms;

public interface IMyInterface { void SomeMethod();}

public class MyClass : IMyInterface {
public void SomeMethod() {
MessageBox.Show("Hi");
}
}
static class Program {
static void Main() {
Type type = Type.GetType("MyClass");
IMyInterface i = (IMyInterface)Activator.CreateInstance(type);
i.SomeMethod();
}
}

Marc


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      14th Feb 2007
<(E-Mail Removed)> wrote:
> 1.1 does not support classes; sorry


What on earth do you mean by that? Classes are a fundamental part of
..NET and always have been.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      14th Feb 2007
Hi,

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 1.1 does not support classes; sorry


What?


--
Ignacio Machin
machin AT laceupsolutions com


 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      14th Feb 2007
Hi,


"teel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi there,
> I need to create an instance of a class whose name I have in a string
> variable, let's say that:
> string myTypeName = "MyType";
>
> and in a result I need the equivalent of:
> MyType obj = new MyType();
>
> I've tried something like Type.GetType which returns the System.Type
> but I don't know how to create a new object with this. My .NET version
> is 1.1
>
> I'd be grateful for help.


Take a look at the different CreateInstance method available from Assembly
and AppDomain classes.


--
Ignacio Machin
machin AT laceupsolutions com


 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      15th Feb 2007
He's trolling. He's been doing this kind of crap in the VB.Net newsgroup
for quite a while.

Robin S.
----------------------
"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> <(E-Mail Removed)> wrote:
>> 1.1 does not support classes; sorry

>
> What on earth do you mean by that? Classes are a fundamental part of
> .NET and always have been.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
pfc_sadr@hotmail.com
Guest
Posts: n/a
 
      16th Feb 2007
C# does not support multi-inheritence

..NET has no tangible benefit over VB and C++ is for fags

so until .NET supports multiple inheritence, classes are POINTLESS




On Feb 14, 6:23 am, Jon Skeet [C# MVP] <s...@pobox.com> wrote:
> <pfc_s...@hotmail.com> wrote:
> > 1.1 does not support classes; sorry

>
> What on earth do you mean by that? Classes are a fundamental part of
> .NET and always have been.
>
> --
> Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
pfc_sadr@hotmail.com
Guest
Posts: n/a
 
      16th Feb 2007
CLASSES ARE POINTLESS DIPSHIT

THEY MAKE YOUR CODE RUN SLOWER AND MORE VERBOSE

ONLY A ****ING RETARD WOULD USE C#


YOU ARE CRIPPLED BY CLASSES, KIDS





On Feb 14, 6:23 am, Jon Skeet [C# MVP] <s...@pobox.com> wrote:
> <pfc_s...@hotmail.com> wrote:
> > 1.1 does not support classes; sorry

>
> What on earth do you mean by that? Classes are a fundamental part of
> .NET and always have been.
>
> --
> Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
Ravichandran J.V.
Guest
Posts: n/a
 
      16th Feb 2007
Hey!!

What does PFC Sadr mean?

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamically create class instance from class name Scott Izu Microsoft C# .NET 1 7th Oct 2008 03:20 PM
Class instance variable question Janos Microsoft C# .NET 3 28th Jan 2007 05:19 PM
How to have New() in a Base Class create and return an instance of a Derived Class? Joe HM Microsoft VB .NET 4 22nd Nov 2005 09:31 PM
How to create a class instance from a string variable Steve Long Microsoft VB .NET 5 26th Mar 2004 10:56 PM
Activator could not create a instance of the class as the Length of the class name is 36 characters Jack Wright Microsoft C# .NET 0 18th Mar 2004 04:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:23 AM.