473,654 Members | 3,042 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(myBi nding.Control,m yBinding.Contro l.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?
Nov 20 '05 #1
11 7757
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*******@comm andalkon.com> wrote in message
news:44******** *************** ***@posting.goo gle.com...
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(myBi nding.Control,m yBinding.Contro l.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?

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(myBi nding.Control,m yBinding.Contro l.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?

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(myBi nding.Control,m yBinding.Contro l.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...

--
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 MySpecialContro l = directcast(MyBi nding.Control,
MySpecialContro l)
'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.Speci alProperty

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.Contr ol
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 MySpecialContro l Then
dim MyControl as MySpecialContro l = directcast(MyBi nding.Control,
MySpecialContro l)
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*******@comm andalkon.com> wrote in message
news:Or******** ******@TK2MSFTN GP10.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 MySpecialContro l = directcast(MyBi nding.Control,
MySpecialContro l)
'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.Speci alProperty

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.TheP roperty
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.TheP roperty
Get
Return Me.Widget.ToStr ing
End Get
Set(ByVal value As String)
Me.Widget = Widget.FromStri ng(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*******@comm andalkon.com> wrote in message
news:u0******** ******@TK2MSFTN GP12.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

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

Similar topics

13
1896
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 as to the base class Ex: Class BaseCl Class DerivCl Inherits BaseCl 'Run-time
6
10825
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 found this in the VB Language reference: <quote> The DirectCast keyword introduces a type conversion operation. You use it the same way you use the CType keyword.... Both keywords take an expression to be converted as the first argument, and
6
2128
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: Specified cast is not valid. In general terms the code is constructed as follows; ModuleA Public xlWb As Excel.Workbook
7
2711
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
2567
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
4100
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 convert it -- -iwdu15
5
2606
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 j.add("string") directcast(j,collections.arraylist).add("string2")
3
2185
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 named 'StandardWebsiteUserRoleID'... Public ReadOnly Property StandardWebsiteUserRoleID() As Integer Get Return CType(Me("StandardWebsiteUserRoleID"),Integer) End Get End Property
9
5920
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.
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.