473,800 Members | 3,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(B yVal lookUpListName As
String, ByVal lookUpListKey As Object) As String
Get
Return CType(HttpConte xt.Current.Cach e(lookUpListNam e),
ListDictionary) .Item(Ctype(loo kUpListKey,look UpListKey.getTy pe())).ToString (
)
End Get
End Property
Nov 18 '05 #1
3 2567
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%40t kmsftngp04&rnum =6&prev=/groups%3Fq%3Dct ype%2520runtime %26hl%3
Dzh-CN%26lr%3D%26ie %3DUTF-8%26oe%3DUTF-8%26sa%3DN%26ta b%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().T oString()
Case "System.Str ing"
....
Case "System.Int 32"
...
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.m icrosoft.com> wrote in message
news:ZB******** ******@cpmsftng xa06.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%40t kmsftngp04&rnum =6&prev=/groups%3Fq%3Dct ype%2520runtime %26hl%3 Dzh-CN%26lr%3D%26ie %3DUTF-8%26oe%3DUTF-8%26sa%3DN%26ta b%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().T oString()
Case "System.Str ing"
...
Case "System.Int 32"
..
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
319
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 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...
2
10063
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 access the Text property, I need to have typed reference (castit to TextBox with CType and set the value to the property). I don't exactly know how to do this. Could somebody show me the code? Here is what I have so far.
5
2269
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 = "otherText" xmlSave("C:\folder\file.xml", mySettings) Here is the sub: Public Shared Sub xmlSave(ByVal path As String, ByVal config As
1
3687
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 LoadMenuFromFile(ByVal filePath As String) As MenuDataSet Dim cacheKey As String = filePath
4
2870
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 is passed in as a known type, but gappWord is has a scope of the class, so a test of the type is valid (making believe the sub does not know the pre-ordained type). TypeOf returns Word.Application. TypeName returns ApplicationClass.
1
2240
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 Office = Microsoft.Office.Core Imports Excel = Microsoft.Office.Interop.Excel Imports MSForms = Microsoft.Vbe.Interop.Forms ' Office integration attribute. Identifies the startup class for the
6
3553
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 base class that is common to many other classes. public class Base .... end class I have 2 seperate classes that inherit from base
2
2522
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) host.GetService(typeof(IContainer)); IComponent component1 = container1.Components; PropertyInfo info1 = component1.GetType().GetProperty(this.member.Name);
8
2506
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 problem. The program uses windows api to get a handle to a palette from the clipboard. The palette needs to be put there by another program - maybe Photoshop.
0
1841
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
9689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10495
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
10269
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...
0
10032
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
7573
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
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
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
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.