473,289 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

DirectCast with unkown type

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.Get Type()), 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?
Nov 20 '05 #1
11 7736
DirectCast is strictly for cases when the Source and Target Types are fixed
and also don't need coercion. CType an option ??
Nov 20 '05 #2
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
"Tubs" <ts*******@commandalkon.com> wrote in message
news:44**************************@posting.google.c om...
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.Get Type()), 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?

Nov 20 '05 #3
unfortunately not. same thing

Troy B. Stauffer
Jack of all trades, master of none :)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #4

Cant you do a select case and then say

Case . . . .
'cast your type

Case . . .

?
OHM

Tubs wrote:
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.Get Type()), 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?

Nov 20 '05 #5
In article <44**************************@posting.google.com >, Tubs wrote:
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.Get Type()), 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...

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #6
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 :)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #7
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
"Troy Stauffer" <ts*******@commandalkon.com> wrote in message
news:Or**************@TK2MSFTNGP10.phx.gbl... 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 :)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #8
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 :)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #9
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

"Troy Stauffer" <ts*******@commandalkon.com> wrote in message
news:u0**************@TK2MSFTNGP12.phx.gbl...
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 :)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #10
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 :)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #11
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

"Troy Stauffer" <ts*******@commandalkon.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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 :)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Crirus | last post by:
Can I use DirectCast to convert a object to it's base or should I use CType? I have a instance of a class as Object. The run-time tipe is a derived of a class, but I need to refer to that instance...
6
by: Ot | last post by:
I apparently have a bit to learn about Casting and Conversion. I have been thinking of them as the same but a discussion in another thread leads me to believe that this is wrong thinking. I...
6
by: Mark Nethercott | last post by:
I get the following failure when trying to access the builtin properties; An unhandled exception of type 'System.InvalidCastException' occurred in resultsoutput.dll Additional information:...
7
by: Brian Henry | last post by:
is there any speed diffrences between doing Ctype or directcast? I know about the inherite diffrences, but process usage time wise, does one take up more cycles then the other? thanks
5
by: Brian Henry | last post by:
Which is faster or considered the better way to do this. I have a object that holds a date... Convert.ToDate(object) or Directcast(object,DateTime) ? Thanks
1
by: iwdu15 | last post by:
can anyone explain the directcast code...ive tried using it and lookin it up but im lookin for an easy definition and how it works...ive tried using it before byut it throws errors saying it can...
5
by: Matt | last post by:
Does anyone know or care to find out the speed difference between a directcast call and a direct call? for instance, i might have these lines of code: dim j as collections.arraylist ...
3
by: =?Utf-8?B?TWlrZQ==?= | last post by:
If Visual Studio knows the type, why does the system-generated property use CType instead of DirectCast? DirectCast is more efficient right? For example - Here's what we have for a setting...
9
by: kimiraikkonen | last post by:
Hi, I'm trying to learn the function of DirectCast and CType which are similiar. Could you explain and sample a simple code that shows what they exactly do and have differences? Thanks.
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.