How add unknown control at runtime

  • Thread starter José Teixeira Junior
  • Start date
T

Tom Spink

Hi Fergus,

The reason I said it looked suspicious is that my trained eye ;-) spots
things that could be potential errors. IMO, it would be better to use CType
in this case because you maybe unsure of type conversion. Of course, there's
absolutely nothing wrong with DirectCast, I use it all the type, where I
can. But IMO it just scares me slightly to see it being used when types
could be potentially different.

That's all, I probably haven't said "Your code is fantastic" yet, so here we
go:

Oh by the way, your code is fantastic!!

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
F

Fergus Cooney

Hi Tom,

Thanks for that, I know about using CType for ambiguity. ;-)))

ROFL. I'm playing, of course.

I'm like you in my sensitivity with these two, although it's often the
other way round - are you sure it needs a CType - couldn't a DirectCast be use
instead! But for objects? I don't think that CType can do anything to coerce
an object from one type to another. It's either DirectCastable or not. Then
again, maybe I'm wrong? [As an aside - for such a common tool, why couldn't
they have just called it Cast? And what and where is IndirectCast?]


~~ nothing wrong with DirectCast,
~~ I use it all the <type>, where I can.

Isn't the Mind wonderful the way it create typos. Perfectly in tune with
what you're saying.


~~ Oh by the way, your code is fantastic!!

ROFL. That's very kind of you. Some of it, maybe, but not this code I'm
afraid. :-(

Here's what Jon Skeet had to suggest about this routine in a different
newsgroup.

\\ That looks complicated and slightly tricksy to me.
\\ You can make the code simpler than that:
\\
\\ Dim formsAssembly as Assembly = GetType (Form).Assembly
\\ Dim oType as Type = formsAssembly.GetType _
\\ ("System.Windows.Forms."& sTypeName)
\\ Return DirectCase (Activator.CreateInstance (oType), Control)

I'm not the master of Type and Assembly yet - still learning. ;-)

Regards,
Fergus
 
T

Tom Spink

MSDN states that CType tries to perform coercion, I cannot remember the
page, however. Perhaps a google will reveal more.

Type (whoops, I mean _time_) for a snooze

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 

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