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

T

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,lookUpListKey.getType())).ToString(
)
End Get
End Property
 
S

Steven Cheng[MSFT]

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=zh-CN&lr=&ie=UTF-8&oe=UTF-8&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
 
T

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.
 
S

Steven Cheng[MSFT]

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

-------------------------------------------------------------------------
 
Joined
Sep 25, 2007
Messages
1
Reaction score
0
I know this thread is over 3 years old, but for those who are still looking and are using Visual Studio 2005 with .NET 2.0, you can create a generic method. I use the following to retrieve a value from ApplicationState (which is stores as a dictionary of objects):

Code:
Private Shared Function GetValue(Of T)(ByVal key As String) As T
        Return CType(Application(key), T)
End Function

When you call this, declare the type you want to use:

Code:
Dim intVal As Integer =  GetValue(Of Integer)(c_ChangeIntervalPatient)

Neat and tidy, and you don't have to worry about explicitly casting since the method does it for you.
 
Joined
Sep 26, 2007
Messages
3
Reaction score
0
Don't work

I'm newbie on dotnet world, I have the same problem of ctype / gettype but when I try to put Application(key) that you posted the compiler says:

I see that the Compiler Error Message is BC30367:
Class 'MyApplication' cannot be indexed because it has no default property

What I can do? I searching how to do something like
Dataset.filter = me.myproperty.tostring & "=" & value of me.myproperty

Thanks!
Fabian Silva


ps2goat said:
I know this thread is over 3 years old, but for those who are still looking and are using Visual Studio 2005 with .NET 2.0, you can create a generic method. I use the following to retrieve a value from ApplicationState (which is stores as a dictionary of objects):

Code:
Private Shared Function GetValue(Of T)(ByVal key As String) As T
        Return CType(Application(key), T)
End Function

When you call this, declare the type you want to use:

Code:
Dim intVal As Integer =  GetValue(Of Integer)(c_ChangeIntervalPatient)

Neat and tidy, and you don't have to worry about explicitly casting since the method does it for you.
 
Joined
Sep 26, 2007
Messages
3
Reaction score
0
forgive it, It appears to work:
Dataset.filter = me.myproperty.tostring & "=" & me(myproperty) and it work
thanks for all :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top