468,784 Members | 1,557 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,784 developers. It's quick & easy.

Can't use getType() in 2nd param of ctype function?

TS
Is there a way to convert a variable from 1 type to another type that you
determine at runtime? I'm trying to pull a value from a ListDictionary's
key. The datatype of that key must be identical to pull that value. I have a
property to pull this value based on the key supplied, but I am using an
object type on my parameter. I want to do a ctype() on the lookUpListKey and
change it to the correct data type so that it's corresponding value will be
pulled. Since lookUpListKey could be a byte, integer, string, etc., I don't
know what to change it to unless I do a getType, but the Ctype function
doesn't allow there to be an expression in the 2nd parameter. Anyway around
this?

Ex: This does not work using the getType() in the 2nd parameter for ctype()
Public Shared ReadOnly Property ListItemValue(ByVal lookUpListName As
String, ByVal lookUpListKey As Object) As String
Get
Return CType(HttpContext.Current.Cache(lookUpListName),
ListDictionary).Item(Ctype(lookUpListKey,lookUpLis tKey.getType())).ToString(
)
End Get
End Property
Nov 18 '05 #1
3 2351
Hi TS,

From your description, you seems meet a existing problem on using the CType
or DirectCast operator to Convert a certain object to its actual type's
instance. From the documentation, the Second param of the CType(..)
convertion operator must be a data type or object class but not a instance
of System.Type that's why we failed when assign a Type's instance via the
unknown object's GetType() function.
Here is a former thread which has been discussing on this issue:

#Runtime Type Conversion...
http://groups.google.com/groups?hl=z...&threadm=ONsHH
f9jBHA.5884%40tkmsftngp04&rnum=6&prev=/groups%3Fq%3Dctype%2520runtime%26hl%3
Dzh-CN%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26sa%3DN%26tab%3Dwg

Also, I don't think such convert will is significant since the you convert
a unknowntype object via code such as CType(obj, obj.GetType()), then what
type of reference do you use to contain it or if its a function's return
value, that'll still be a unknown object , do you think so?

In addition, if you do want to perform a such convert, I think one
workaround is to provide a custom convertion wrapper, for example: use a
switch/ select case block to determine which convert statement to use like:
Select Case obj.GetType().ToString()
Case "System.String"
....
Case "System.Int32"
...
End Select

If you have anything else unclear, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #2
TS
thanks steven. Your solution is what I had in mind, just wanted to know if
there was a way to do what I wanted.

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:ZB**************@cpmsftngxa06.phx.gbl...
Hi TS,

From your description, you seems meet a existing problem on using the CType or DirectCast operator to Convert a certain object to its actual type's
instance. From the documentation, the Second param of the CType(..)
convertion operator must be a data type or object class but not a instance
of System.Type that's why we failed when assign a Type's instance via the
unknown object's GetType() function.
Here is a former thread which has been discussing on this issue:

#Runtime Type Conversion...
http://groups.google.com/groups?hl=z...&threadm=ONsHH f9jBHA.5884%40tkmsftngp04&rnum=6&prev=/groups%3Fq%3Dctype%2520runtime%26hl%3 Dzh-CN%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26sa%3DN%26tab%3Dwg

Also, I don't think such convert will is significant since the you convert
a unknowntype object via code such as CType(obj, obj.GetType()), then what
type of reference do you use to contain it or if its a function's return
value, that'll still be a unknown object , do you think so?

In addition, if you do want to perform a such convert, I think one
workaround is to provide a custom convertion wrapper, for example: use a
switch/ select case block to determine which convert statement to use like: Select Case obj.GetType().ToString()
Case "System.String"
...
Case "System.Int32"
..
End Select

If you have anything else unclear, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #3
Hi TS,

Thanks for your reply. Based on my knowlege, I haven't found any other
means so far. But I think we may wait for some other ones' suggestions on
this. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

-------------------------------------------------------------------------

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Alex Shirley | last post: by
5 posts views Thread by Matthew | last post: by
1 post views Thread by robear | last post: by
4 posts views Thread by Howard Kaikow | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.