473,378 Members | 1,438 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,378 software developers and data experts.

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 2519
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: TS | last post by:
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...
2
by: Alex Shirley | last post by:
Hi Sorry this is (a sort of) repost. I am trying to trim all text boxes on my web form. I've got no problem with the trim function, I have a problem with casting. I am told that if I want to...
5
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
1
by: robear | last post by:
Hi All. I have been converting a C# to VB.NET Web Project and I have one last issue to contend with. I have included relevant code below and an explanation of the error.. Public Function...
4
by: Howard Kaikow | last post by:
For the code below, for both appWord and gappWord, I get the error "Public member 'GetType' on type 'ApplicationClass' not found" I realize the test for appWord is superflous as the parameter...
1
by: RickH | last post by:
..Cells(1,y).GetType ownly shows instead of .Value, .Copy, etc. The code below is derived from samples, it should work, but I've messed up somewhere... Imports System.Windows.Forms Imports...
6
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a...
2
by: Carlos Rodriguez | last post by:
I have the following function in C#public void Undo(IDesignerHost host) { if (!this.componentName.Equals(string.Empty) && (this.member != null)) { IContainer container1 = (IContainer)...
8
by: active | last post by:
Guess I'm looking for someone who likes to work difficult puzzles. I can't seem to ever retrieve a palette handle from the clipboard. Below is a simple test program that demonstrates the...
0
by: varnie | last post by:
hell-o !~ i faced weird problem during usage boost::pool_allocator. after i've changed: typedef boost::shared_ptr< param > ParamPtr; typedef std::vector< ParamPtr > ParamPtrVector; with:
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.