views:

225

answers:

3

I asked a still unanswered question that will shed more light on this question.

Why can't I do this...

_wizardDialog.UIRoot.Controls.Clear()
_wizardDialog.UIRoot.Controls.Add(TryCast(wizardUserControl, wizardUserControl.GetType))

Why does using GetType in this way fail. The argument for try cast are object and type. Since wizardUserControl.GetType returns a type how come this is not legal. Visual Studio is complaining wizardUserControl.GetType is not defined.

The bottom line is how can I get WizardUserControl to return the type that is being passed in to my method. The method that is being called into here should not have to have the type hard-coded...that's the point of all of this OOP stuff...right? So how do you do this.

Please read the other question and answer there if you can...that is the problem I am tyring to solve.

I am learning the oop stuff.

Seth

+5  A: 

GetType() does not return a type. It returns an instance of class Type, which describes a type at runtime. However, an instance of Type is not substitutable where a compile-time type reference is expected (such as TryCast). They're just different things.

Think about it this way. TryCast operator has a definite compile-time result type. If you use a Type object obtained from elsewhere (and it could be a conditional with Random, so there's no way to predict the result at compile-time in general), then what should be the compile-time result type of TryCast?

Pavel Minaev
A: 

There's absolutely nothing wrong with "hard-coding" a type in your code. The general idea of OOP programming (specifically the idea of "programming to the interface") in this regard is that you specify the class that most generically describes what you need to do. For example, if I'm writing code where I'm adding instances of a UserControl to a form where all of those instances inherit from a base class and I need functionality from that base class (your WizardUserControlBase class) then that's the type that I would use the WizardUserControlBase class to refer to my controls.

As far as this question goes, OOP is not about not having types "hard-coded" (at some level it HAS to be hard-coded, even if it's just Object, since that's still a class). Disregarding that, I'm not quite sure what you're trying to accomplish by (as it looks like you're trying to) casting it to its own type. You should just be able to call _wizardDialog.UIRoot.Controls.Add(wizardUserControl)

Adam Robinson
Thanks for your answer.I do understand that at some point you have to reference a concrete type. But this code needs to be able to load any type of user control that implements IWizardUserControl. At this point in the code it might be any number of implementations. So what I was saying is that this code in the InitUserControl should not have a hard-coded reference to any of the IWizardUserControl implementations. THAT defeats the purpose of OOP.BTW _wizardDialog.UIRoot.Controls.Add(wizardUserControl) is the code I had that doesn't work. Seth
Seth Spearman
+1  A: 

Think about it this way::

  • GetType() is a function. It returns an Object.

    • You do not know what the type is, or you wouldn't need to ask :)
  • When you are casting an object, you are telling the COMPILER what it's type is.

You can't both ask a type what it is, which happens at runtime, and tell the compiler what it is at compile time, in the same place.

I guess you can also think of something like GetType(String) as a keyword. It is clearer in this example:

  // Makes sense, we tell the compiler what the object is.  We could still get a 
  // *runtime* exception, if we were lying to the compiler.
  Dim car = CType(vehicle, Car); 

  // this doesn't make sense, since we don't know what is in "anObj"
  Dim anObj As Object = "(I don't know what it is, thats why it's an object)"
  Dim car = ctype(anObj, anObj.GetType() )

  // and this is the clearest, in vb.  you can see the type is being used kinda like
  // a keyword.  it won't change, but a call to GetType could
  If TypeOf anObj is Car Then
   ...

In the second case, you don't know what type "anObj" is. Since

Andrew Backer
Thanks...I get it.Seth
Seth Spearman