Assign Type at Runtime

B

blisspikle

I tried closely copying some code that I found on this group for
assigning a type at runtime, but I cannot get it to work. Can someone
see what is wrong with my logic?

Thanks,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim s As String
s = "blah"
Dim t As Type
t = s.GetType
Dim obj As Object
obj = System.Activator.CreateInstance(t) 'Missing Exception
Unhandled - No parameterless constructor defined for this object.
obj = "hello"
s = DirectCast(obj, String)
RichTextBox1.Text = s
End Sub
 
B

Branco Medeiros

blisspikle said:
I tried closely copying some code that I found on this group for
assigning a type at runtime, but I cannot get it to work. Can someone
see what is wrong with my logic?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim s As String
s = "blah"
Dim t As Type
t = s.GetType
Dim obj As Object
obj = System.Activator.CreateInstance(t) 'Missing Exception
Unhandled - No parameterless constructor defined for this object.
obj = "hello"
s = DirectCast(obj, String)
RichTextBox1.Text = s
End Sub

You're getting the exception because, really, there's no parameterless
constructor in String (try for yourself, type "Dim S As New
String(" ... and you'll see).

Also, when you assign "hello" to obj, a new string object is created
and it's *that* object that is assigned to s, in the end. I guess what
you want is:

<aircode>
Dim S As String = "Blah"
Dim T As Type = S.GetType
Dim O As Object = _
System.Activator.CreateInstance(T, "hello".ToCharArray)
S = DirectCast(O, String)
</aircode>

But I still don't get the point, unless you want to instanciate
something other than a string...

HTH.

Regards,

Branco.
 
W

wfairl

I tried closely copying some code that I found on this group for
assigning a type at runtime, but I cannot get it to work. Can someone
see what is wrong with my logic?

Thanks,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim s As String
s = "blah"
Dim t As Type
t = s.GetType
Dim obj As Object
obj = System.Activator.CreateInstance(t) 'Missing Exception
Unhandled - No parameterless constructor defined for this object.
obj = "hello"
s = DirectCast(obj, String)
RichTextBox1.Text = s
End Sub

The problem is exactly as the message indicates, the string type has
no public parameterless constructor. You need to use one of the
possible constructors, for example the one taking a character array:

obj = System.Activator.CreateInstance(t, New Object()
{"hello".ToCharArray()})
 
T

Tom Shelton

I tried closely copying some code that I found on this group for
assigning a type at runtime, but I cannot get it to work. Can someone
see what is wrong with my logic?

Thanks,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim s As String
s = "blah"
Dim t As Type
t = s.GetType
Dim obj As Object
obj = System.Activator.CreateInstance(t) 'Missing Exception
Unhandled - No parameterless constructor defined for this object.

Because System.String has no parameterless constructor... In other
words, you can't call new String() on a string. You need to pass in
something like this:
obj = System.Activateor.CreateInstance (t, New Object[] {"Hello"})

s = DirectCast(obj, String)
RichTextBox1.Text = s
End Sub

HTH,
 
B

blisspikle

I get messed up between instanciating an object and dimensioning the
object. Can someone point me to a place that lets me know a little
behind the scenes on what the difference is?

Is the New constructor called by default for a string when you use
s="blah". Is this because it has a default constructor? This cannot
be done with all Types right? I am going to list what I thought was
going on in my code below, it would probably be way too long to answer
these questions here?

Thanks for earlier answers, they were all really good. I could
probably try and plug my way through VB and have no idea what I am
doing, maybe that is why VB is popular?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim s As String 'Not sure exactly what this
does behind the scenes, just know that you have to do it?
s = "blah" 'Somehow stores
(character array) into a String Type
Dim t As Type 'Not sure what it does
behind the scenes, but makes a variable that can hold a type?
t = s.GetType 'Get what type a string is,
which not sure what that would look like?
Dim obj As Object 'Dimension a non-specific
object that can store just about anything, not sure how it does it?
obj = System.Activator.CreateInstance(t) 'Create
instance of the type stored in t, and set to be obj, is there another
way to just

'dimension with the same type here rather then have to instantiate it?
obj = "hello" 'If obj is now a string
type I should be able to assign a value to it
s = DirectCast(obj, String) 'VB somehow switches types
RichTextBox1.Text = s 'assigns a string to a
richtextbox
End Sub
 
T

Tom Shelton

I get messed up between instanciating an object and dimensioning the
object. Can someone point me to a place that lets me know a little
behind the scenes on what the difference is?

Is the New constructor called by default for a string when you use
s="blah". Is this because it has a default constructor? This cannot
be done with all Types right? I am going to list what I thought was
going on in my code below, it would probably be way too long to answer
these questions here?

Thanks for earlier answers, they were all really good. I could
probably try and plug my way through VB and have no idea what I am
doing, maybe that is why VB is popular?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim s As String 'Not sure exactly what this
does behind the scenes, just know that you have to do it?

It allocates a reference to a string and clears it to Nothing. What
does that mean? Well, a string is a reference type, so it lives on
the heap (the dynamic data store). So, what you are really setting
aside is a variable that can point to a location on the heap that
holds a string. When the string is assigned and the memory is
allocated, then the reference will then contain the address of the
string...
s = "blah" 'Somehow stores
(character array) into a String Type

While string is a reference type, it is treated in some ways like a
value type by the compiler. This is one of those ways. Your
assignment above, is really equivalent to:

s = New String ("blah".ToCharArray())

But, because that would be a pain to type all the time the compiler
gives a break on it's syntax.
Dim t As Type 'Not sure what it does
behind the scenes, but makes a variable that can hold a type?

Yep. You go it that's what it does.
t = s.GetType 'Get what type a string is,
which not sure what that would look like?

Look at the docs for System.Type. It is an object that can be used to
discover a lot of information about the type of an object.
Dim obj As Object 'Dimension a non-specific
object that can store just about anything, not sure how it does it?

It does it by the power of polymorphism. In .Net everything derives
from System.Object. When you don't explicitly inherit from another
object, then you are inheriting from System.Object. Because of that,
you can store any .net object in a variable of type object. Of
course, you functionality is limited to that defined by the
System.Object interface :)
obj = System.Activator.CreateInstance(t) 'Create
instance of the type stored in t, and set to be obj, is there another
way to just

'dimension with the same type here rather then have to instantiate it?

I'm not sure what you mean? With the string you could have just done:

Dim s2 As String = s

Now, with other reference types you might be suprised to learn that a
change to s would effect s2. But, that isn't the case with string.
It's another one of those places where the runtime treats string a
little more like a value type. Anyway, if you look in the docs about
string interning, then you will get an idea of what I'm talking about.
obj = "hello" 'If obj is now a string
type I should be able to assign a value to it

It's not a string type - obj is type object. You can assign anything
you want to it because everything in .net ultimately derives from
object.
s = DirectCast(obj, String) 'VB somehow switches types

DirectCast will essentially make sure that the object your casting is
really of the type you are wanting to convert it too and if it is make
the assignment. If it isn't you get an exception.
RichTextBox1.Text = s 'assigns a string to a
richtextbox
End Sub

I hope this helps somewhat. There is a lot more that could be said
about this, but I didn't want to go to deep. If you have any
questions, feel free to ask again :)
 
B

blisspikle

Thank you all for the answers.

Tom, you gave me a better understanding in one answer then I have been
able to get through reading the books. I'm sure that I will have more
questions :)
 
R

RobinS

Tom Shelton said:
Good thing to. Variant was evil :)

Variants had their time and their place. I used them when reading data from
a database, because in VB, there was no way to set an integer to Null or
Nothing, and when dealing with data, there's a big difference between Null
and 0.

Now I use Nullable Types, and am much happier because they are so specific.
:-D

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
 
T

Tom Shelton

Variants had their time and their place. I used them when reading data from
a database, because in VB, there was no way to set an integer to Null or
Nothing, and when dealing with data, there's a big difference between Null
and 0.

I realize that they had their place (notice the smiley). The problem with
them was that they got over used - often by mistake, remember this one:

dim i, j as integer

ops, i is a variant.
Now I use Nullable Types, and am much happier because they are so specific.
:-D

Nullable Types rock. Now if VB.NET would get anonymous methods, it would
almost be usable :)

C#:

int [] GetValues (int[] values, int value)
{
return Array.FindAll ( values, delegate (int i) { return (i == value); } );
}

// calling code
Array.ForEach ( GetValues (6), delegate (int i ) { Console.WriteLine (i); } );

Love it!
 
A

aaron.kempf

tom

just because you worked with some dipshit programmers; it doesn't mean
that microsoft needed to change the language without reason

it's like.. the whole datareader .read method is it?
I read something in MS Press book that said 'because you dipshits are
too stupid to do a .movenext method we are going to make it easier by
just having a .read method'

I'm like..

**** THIS MOTHER ****ING COMPANY THAT BRINGS VB DOWN TO THE LOWEST
COMMON DENOMINATOR

UNNECESSARY CHANGE IS NEITHER NECESSARY OR SEXY

TAKE YOUR DOTNET AND SHOVE IT
DOTNET DOESNT EVEN RUN ON ANY OF THESE OPERATING SYSTEMS, WHICH HANDLE
VB6 JUST PERFECTLY

windows 95
windows 98
windows nt
windows me
windows 2000
windows xp
windows vista

seriosly here.

I've got a random machine; how do I determine which version of the
framework is on it?
I've got a random machine; how do I determine which version of the
framework is on it?
I've got a random machine; how do I determine which version of the
framework is on it?


THERE IS NO ****ING ANSWER, MICROSOFT SHOULD HAVE MADE THIS PRACTICAL
for example you need to remember that R2 ships with 2.0 and you've got
to remember that XP ships with 1.0

I MEAN IT DOESNT SHOW UP VIA ADD / REMOVE PROGRAMS

please tell me, el wisest dotnet _FAGS_

i've got a random computer right here under my desk, please tell me
how to determine which version of the framework is on it.
because when you give me the wrong answer?

_THAT_ is when I'm going to swear off the DOTNOT framework.

THERE IS NO ANSWER; IT IS BASICALLY IMPOSSIBLE TO DETERMINE WHICH
VERSION OF THE FRAMEWORK IS ON A PARTICULAR MACHINE
 
B

Brian Gideon

I tried closely copying some code that I found on this group for
assigning a type at runtime, but I cannot get it to work. Can someone
see what is wrong with my logic?

Thanks,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim s As String
s = "blah"
Dim t As Type
t = s.GetType
Dim obj As Object
obj = System.Activator.CreateInstance(t) 'Missing Exception
Unhandled - No parameterless constructor defined for this object.
obj = "hello"
s = DirectCast(obj, String)
RichTextBox1.Text = s
End Sub

Hi,

Maybe I'm not understand the intent of your question, but why not just
execute RichTextBox1.Text = "Hello"?

Anyway, here you go.

Dim typ As Type = GetType(String)
Dim arg As Object() = {"Hello".ToCharArray()}
Dim obj As Object = Activator.CreateInstance(typ, arg)
Dim str As String = DirectCast(obj, String)

Here's an alternate method.

Dim typ As Type = GetType(String)
Dim arg As Object() = {"Hello".ToCharArray()}
Dim obj As Object = typ.GetConstructor(New Type()
{GetType(Char())}).Invoke(arg)
Dim str As String = DirectCast(obj, String)

Brian
 
R

RobinS

Tom Shelton said:
Variants had their time and their place. I used them when reading data
from
a database, because in VB, there was no way to set an integer to Null or
Nothing, and when dealing with data, there's a big difference between
Null
and 0.

I realize that they had their place (notice the smiley). The problem
with
them was that they got over used - often by mistake, remember this one:

dim i, j as integer

ops, i is a variant.
Now I use Nullable Types, and am much happier because they are so
specific.
:-D

Nullable Types rock. Now if VB.NET would get anonymous methods, it would
almost be usable :)

C#:

int [] GetValues (int[] values, int value)
{
return Array.FindAll ( values, delegate (int i) { return (i ==
value); } );
}

// calling code
Array.ForEach ( GetValues (6), delegate (int i ) { Console.WriteLine
(i); } );

Love it!

I never did
Dim i, j as Integer

I don't even do that now, even though I can. Too anal-retentive. I want it
to be really, really clear.

And I agree; I think anonymous methods are cool. I've seen them in action.
Of course, it doesn't kill me to do it "the hard way". (As Nietzsche said,
"What doesn't destroy me makes me stronger." Of course, he was a nutcase.)

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
 
R

RobinS

I don't think this has anything to do with variants or anonymous methods.
You're ranting out of topic.


Robin S.
(King of Russia to *you*)
Ts'i mahnu uterna ot twan ot geifur hingts uto.
 
B

Brian Gideon

Variants had their time and their place. I used them when reading data from
a database, because in VB, there was no way to set an integer to Null or
Nothing, and when dealing with data, there's a big difference between Null
and 0.

I realize that they had their place (notice the smiley). The problem with
them was that they got over used - often by mistake, remember this one:

dim i, j as integer

ops, i is a variant.
Now I use Nullable Types, and am much happier because they are so specific.
:-D

Nullable Types rock. Now if VB.NET would get anonymous methods, it would
almost be usable :)

C#:

int [] GetValues (int[] values, int value)
{
return Array.FindAll ( values, delegate (int i) { return (i == value); } );

}

// calling code
Array.ForEach ( GetValues (6), delegate (int i ) { Console.WriteLine (i); } );

Love it!

Hi,

And with lambda expressions in C# 3.0 it would look like:

int [] GetValues (int[] values, int value)
{
return Array.FindAll ( values, i => i == value);
}

// calling code
Array.ForEach ( GetValues (6), i => Console.WriteLine(i) );

Love it more!

Brian
 
B

Bruce W. Darby

Robin,

If Aaron WAS the guru he makes himself out to be, he would simply open the
Control Panel, go to Add/Remove Programs and 'scroll down' to the listing
that tells him the version of the framework that he has installed. Each
framework installs into seperate folders under the
Windows\Microsoft.net\Framework directory structure so users can implement
EITHER framework. Users can even set which framework they want their
application to use should they so choose, but I'm not going to continue on
that topic here. So though he is off-topic, he STILL hasn't said anything
that is even CLOSE to being realistic or true.

And just to make sure that my post is 'on-topic' :), Aaron likes to waste
memory by using variants in his work, making his VB6 applications run slower
and hog memory that could be put to better use by the operating system doing
more meaningful tasks.

Bruce
 
A

aaron.kempf

BRUCE

what kindof ****ing idiot are you?

LIKE SERIOUSLY

windows xp, out of the box; go add/remove and _PLEASE_ tell me if it
has the framework.
server 2003 rs, out of the box; go add/remove and _PLEASE_ tell me if
it has the framework.

from what I've seen-- you have to 'REMEMBER' that XP and Server 2003
are exceptions to that rule.

PLEASE BUDDY; KEEP GUESSING.

because if you can't even mother ****ing tell me what version of the
framework is on machine X then WHY IN THE **** WOULD YOU USE THIS
LANGUAGE?
 
A

aaron.kempf

Window XP ships with .NET 1.0 right? Does it?

AND IT SURE DOESNT SHOW UP IN ADD / REMOVE PROGRAMS
 
A

aaron.kempf

mother ****er

I don't use variants

I don't need better performance (and for the record, my apps launch
faster than yours LoL)

and I don't have memory shortages.

NICE TRY DIPSHIT.

why would they try to sell us on PERFORMANCE?

real quick -- if you could have a programming language that was
_MAYBE_ 30% faster at _SOME_ tasks.. but it only ran on 1/4 of the
operating systems or applications... would you do it?

I haven't had a performance problem with BASIC since, uh-- well the
day I started writing BASIC back in the commodore 64.

SO LICK A NUT DICKWAD

It's like trying to sell a freezer to an eskimo -- WE JUST DO NOT NEED
IT AT ANY PRICE
mother ****ers gave us 1/4 of the code portability-- can we even run
VB 2005 apps on Vista yet?

can I cut and paste code from VB 2005 into an Excel workbook? ROFL
can I cut and paste code from VB 2005 into an ASP page? ROFL
can I cut and paste code from VB 2005 into a DHTML page? ROFL
can I cut and paste code from VB 2005 into a DTS package? ROFL
can I _SAVE_ a DTS pacakge as a VB 2005 module? ROFL
can I cut and paste code from VB 2005 into OUTLOOK? ROFL
can I cut and paste code from VB 2005 into WORD? ROFL
can I cut and paste code from VB 2005 into ACCESS? ROFL
can I cut and paste code from VB 2005 into SQL AGENT? ROFL
can I cut and paste code from VB 2005 into a VBS FILE? ROFL

your language is for QUEERS AND RETARDS; you do not even know what
operating systems your shit will work on.

-Aaron
 

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