473,405 Members | 2,187 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,405 software developers and data experts.

Type Conversion Questions

JS
In my project, I am setting Options Strict on.
In one place, I have generic code to set values in my objects using a
configuration file, code which is trying to set properties of my classes
using Invokemember, using this code -

dim bindSETPROP As BindingFlags = BindingFlags.IgnoreCase Or
BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.SetProperty
Me.GetType.InvokeMember(strPropertyName, bindSETPROP, Nothing, Me, objParams)

Where strPropertyName is the property being set. The data is packaged into
objParams.
As part of the config file, it specifies the data type, so the objParams
information contains the correct type when setting the property value, so for
example, a line from the config file would read in t
property name value datatype
"FirstName" "Frank" "String"

so the object would package "Frank" as a string, and set the FirstName of
the object using InvokeMember. This works great until I get to properties
defined as an Enum. The data in the file is Integer data, but if it is not
cast to the proper Enum, the INvokeMember fails, as I have Option Strict On.
I am trying to figure out a way to dynamically cast an integer to a specific
Enum type when I have the name of the Enum. CType will not work , as it needs
the type itself to be passed in, which I need to do dynamically. Apparently,
calling CType( intVal, type.GetType("eenumtype")) is not valid.

So to make a long story short, is there any way to do a dynamic cast when I
only have the type name? (Short of building a big case statement)?
Jonathan Steinberg
BRCorp
Nov 21 '05 #1
1 1398
JS
I thought of a workaround:

Public Shared Function EnumConv(ByVal intIN As Integer, ByVal strEnumName As
String) As Object

Dim arrVals As Array

'Enum Type is "MyProj.eFooEnum"

arrVals = System.Enum.GetValues(System.Type.GetType(strEnumN ame))
For Each objVal As Object In arrVals
If intIN = CInt(objVal ) Then
Return objVal
End If
Next

End Function
This function will return an Object containing an enum, which I can pass
into InvokeMethod, which works. Bit of a kludge, though.

JS
"JS" wrote:
In my project, I am setting Options Strict on.
In one place, I have generic code to set values in my objects using a
configuration file, code which is trying to set properties of my classes
using Invokemember, using this code -

dim bindSETPROP As BindingFlags = BindingFlags.IgnoreCase Or
BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.SetProperty
Me.GetType.InvokeMember(strPropertyName, bindSETPROP, Nothing, Me, objParams)

Where strPropertyName is the property being set. The data is packaged into
objParams.
As part of the config file, it specifies the data type, so the objParams
information contains the correct type when setting the property value, so for
example, a line from the config file would read in t
property name value datatype
"FirstName" "Frank" "String"

so the object would package "Frank" as a string, and set the FirstName of
the object using InvokeMember. This works great until I get to properties
defined as an Enum. The data in the file is Integer data, but if it is not
cast to the proper Enum, the INvokeMember fails, as I have Option Strict On.
I am trying to figure out a way to dynamically cast an integer to a specific
Enum type when I have the name of the Enum. CType will not work , as it needs
the type itself to be passed in, which I need to do dynamically. Apparently,
calling CType( intVal, type.GetType("eenumtype")) is not valid.

So to make a long story short, is there any way to do a dynamic cast when I
only have the type name? (Short of building a big case statement)?
Jonathan Steinberg
BRCorp

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
11
by: RWC | last post by:
Hello, I'm having trouble converting code in Access XP / 2002. I have some code that declares an variable "as database" in Access 97, which is not recognized in Access XP. I've tried to find a...
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
1
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
29
by: aarthi28 | last post by:
Hi, I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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,...
0
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...

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.