DirectCast with unkown type

  • Thread starter Thread starter Tubs
  • Start date Start date
T

Tubs

i am attempting to write something which can morph itself to whatever
comes in and get the value property from it but i don't know what type
it is until runtime. I am therefore trying to use reflection and get
the type from there and then cast it to that type so i can use it.
When i use DirectCast, it won't let me do something like
DirectCast(myBinding.Control,myBinding.Control.GetType()), in fact
intellisense doesn't work at all because it is looking for a type
ONLY. Isn't there a way to late cast this?
 
DirectCast is strictly for cases when the Source and Target Types are fixed
and also don't need coercion. CType an option ??
 
Tubs,
If you do not know the type of the variable you are assigning to at compile
time, you cannot use DirectCast or CType! As both require that you know the
actual type at compile time.

Can you give a more complete example of what you are attempting? (10 to 15
lines of code).

Late Binding (Option Strict Off) or CallByName may be an option instead of
Reflection, as both Late Binding & CallByName wrap reflection for you.

Hope this helps
Jay
 
Cant you do a select case and then say

Case . . . .
'cast your type

Case . . .

?


OHM
 
i am attempting to write something which can morph itself to whatever
comes in and get the value property from it but i don't know what type
it is until runtime. I am therefore trying to use reflection and get
the type from there and then cast it to that type so i can use it.
When i use DirectCast, it won't let me do something like
DirectCast(myBinding.Control,myBinding.Control.GetType()), in fact
intellisense doesn't work at all because it is looking for a type
ONLY. Isn't there a way to late cast this?

Like Jay said, post a little code as to what your trying to
accomplish... But, I have to ask - are these classes that you have
control over? Because if they are - this may be a good place to
implement an interface...
 
Thanks guys but i am now going at it a little differently. What i was
trying to do, which i guess i would still like to know if it is
possible, is:

within the binding.parse event of a binding get at the bound controls
property values. The problem i has was that i needed to be able to cast
that control to the one that it was, which the binder knows, so i could
use its interface but using object also works. I wonder though just how
this works because essentially don't they have to do the same thing. I
WAS able to get CallByName to work instead as well and maybe that is
what they are really doing underneath.

Example:
dim MyControl as MySpecialControl = directcast(MyBinding.Control,
MySpecialControl)
'Problem here is that it could be one of many derived classes so i need
to cast it to that particular class
'Using Object solved this or CallByName but i am still bummed why
DirectCast can't do it
debug.print MyControl.SpecialProperty

Troy B. Stauffer
Jack of all trades, master of none :)
 
Troy,
It sounds like you do "know" the type of variables you are working with.

I normally use the TypeOf Is operator, something like:

Dim ctl As Control = myBinding.Control
If TypeOf ctl Is TextBox then
Dim text As TextBox = DirectCast(ctl, TextBox)
ElseIf TypeOf ctl Is RadioButton Then
Dim radio As RadioButton = DirectCast(ctl, RadioButton)

ElseIf TypeOf ctl Is MySpecialControl Then
dim MyControl as MySpecialControl = directcast(MyBinding.Control,
MySpecialControl)
ElseIf...
End If
'Using Object solved this or CallByName but i am still bummed why
DirectCast can't do it
As I show above, DirectCast can do it!

Hope this helps
Jay
 
Thanks, i guess i just hate doing that because if you ever add new ones
into the fold, you have to code them specifically, whereas a truly
dynamic solution would just pick up on it. Not only are my controls not
know but which property to look at is not known. So a truly dynamic
solution would just warm my heart. Thanks for the post back anyhow

Troy B. Stauffer
Jack of all trades, master of none :)
 
Troy,
You have the "truly dynamic solution": Late Binding, Reflection or
CallByName! (which are all based on Reflection).

DirectCast & CType are not intended for "truly dynamic solutions".
DirectCast is intended for where you know the type of the object, but that
object is in a variable of a different type, normally this different type
will be a base type. CType is there for when you an object of one type and
you want to convert it to an object of a different type (think of CType as
Convert Type).

DirectCast & CType are there to avoid the "truly dynamic solution"! As the
"Truly dynamic solution" is expensive at runtime.

The other option to avoid the overhead of Reflection is to use an Interface,
which someone else suggested, which is the route I would normally take! The
interface avoids "you have to code them specifically" as it delegates that
code to each class (encapsulation). This of course assumes that each control
"binds" to a specific property of that control. Of course if each control
has multiple properties that could be bound to, this won't work as well...

Public Interface TheChanger
Public Property TheProperty As String
End Interface

Public Class SpecialControl1
Implements TheChanger

Public Property TheProperty() As String Implements
TheChanger.TheProperty
Get
Return Me.Text
End Get
Set(ByVal value As String)
Me.Text = value
End Set
End Property

End Class

Public Class SpecialControl2
Implements TheChanger

Public Property TheProperty() As String Implements
TheChanger.TheProperty
Get
Return Me.Widget.ToString
End Get
Set(ByVal value As String)
Me.Widget = Widget.FromString(value)
End Set
End Property

End Class

BTW: Is there a reason you are attempting on building your own binding
mechanism on top of the .NET binding mechanism. What are you needing that
the .NET binding does not provide?

Hope this helps
Jay
 
heh, interesting question. This whole debacle started because i wanted
to create and automatically bound control. This control is based on
something i called IOBasePoint. You can then derive and IOAnalogPoint
and an IODigitalPoint from it. DigitalPoint has a value which is type
boolean and AnalogPoint has value of single. Now add binding into the
picture. First of all, if the value is ever null coming from the bind
then it blows chunks. So i finally figured out that by trapping the
Parse and Format functions of the binding that i could help before the
actual binding happens. Well it doesn't stop there. In the cast that
the value coming in is bogus (i.e. a string for the AnalogValue) then i
want to be able to avoid the bind. From everything i can figure out,
there is no way to STOP the binding from happening. All you can do is
change the value. (Is there a cancel ???) Anyhow, in the case where the
new value is bogus, i want to set it to the old value, which i need to
them interrogate the control in question and get the value of the bound
property and set the bind value to that giving the apppearance that the
value didn't change. Sounds like a mess huh. I think it is.
One last thing. I also have a binding condition i put on which says to
only allow the bind to happen if the condition is true. For this i
actually build an expression evaulator which uses the codedom, spits out
code and compiles to an In Memory DLL which i then call. If that fails,
then i also have to keep the value the same (again in need of a cancel
here)

THoughts of a better way??

Troy B. Stauffer
Jack of all trades, master of none :)
 
Troy,
I would consider something like:

Public MustInherit Class IOBasePoint
Public MustOverride Property BoundValue() As Object
End Class

Public Class IODigitalPoint
Inherits IOBasePoint

Public Value As Boolean

Public Overrides Property BoundValue As Object
Get
Return Value
End Get
Set(ByVal value As Object)
If Value is Nothing Then
' handle the nothing case
End If
' any other validation Analog Point may need
Me.Value = CType(value, Boolean)
End Set
End Property

End Class

Public Class IOAnalogPoint
Inherits IOBasePoint

Public Value As Single

Public Overrides Property BoundValue As Object
Get
Return Value
End Get
Set(ByVal value As Object)
If Value is Nothing Then
' handle the nothing case
End If
' any other validation Analog Point may need
Me.Value = CType(value, Single)
End Set
End Property

End Class

Then I would bind to IOBasePoint.BoundValue, each derived class has the any
validation needed in its BoundValue setter.

Is IOBasePoint a control on the screen or a "data" object that you are
binding to?

Using the above I'm not so sure you would need the Parse & Format events...

Hope this helps
Jay
 
Back
Top