473,396 Members | 1,743 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,396 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 2524
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
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:
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.