473,378 Members | 1,389 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.

Dynamic type conversion

I am writing a generic property table as part of my application

property_name
property_type
property_value

All information is stored in the database as strings.

In my application that maintains this data, I want to ensure that the
user cannot type in a value that cannot be converted to the specified
property_type. There is no list of allowable property types. Any type
or enum that exists in the .Net Framework, or a custom type or enum
derived from the .Net framework is fair game in my application.

I can't seem to figure out how to verify this.

For example:

property_name = "RadiobuttonList.RepeatLayout"
property_type = "System.Web.UI.WebControls.RepeatLayout"
property_value = "Flow"

What code can I write to verify that "Flow" can be converted to the
type specified by the string "System.Web.UI.WebControls.RepeatLayout"?

Here is some of the code that I have tried

Dim strPropertyType As String
Dim strPropertyValue As String
Dim objType As System.Type

strPropertyValue = "Flow"
strPropertyType = "System.Web.UI.WebControls.RepeatLayout"

objType = System.Type.GetType(strPropertyType)
Try
System.Convert.ChangeType(strPropertyValue, objType)
Catch objException As System.Exception
ErrorMessage = "Invalid Property value specified"
End Try

This seems to work for types like String, Int32, etc, but not for Enums
(at least not for System.Web.UI.WebControls.RepeatLayout).

Help!

Remember, at design time, I know NOTHING about the type information I
will be working with.

Thanks

Mark Olszowka

May 18 '06 #1
4 1812
Unfortunately, there is much more to dynamic conversion in the .Net
Framework.
The Convert class only supports "base types" which msdn lists as Int64,
boolean etc (value types). The method you have listed won't work on
enumerations. Enumerations are considered a special form of a value type,
which has a name, an underlying type and a set of fields. What you need is a
little bit of reflection with the System.Reflection namespace.

For instance, you need to devise a coding strategy to interrogate the type
and different ways of validating the value. In the case of enumerations you
could check the type to see if it is an enumeration

example: type.IsEnum

Once you have determined if the type is an enumeration you could validate the
value this way.
This assumes the value is a string which can be converted to a Int32, byte,
UInt64. This is also assuming the user of your class is passing in the
underlying type and not the string representation of the name of the
enumeration.

Dim fieldinfo() As FieldInfo = type.GetFields

For x = 1 To fieldinfo.GetUpperBound(0)
if fieldinfo(x).GetValue(fieldinfo(x)) = value then
valid = true
end if
next

If your type is a value type and not an enumeration you can use the code you
have listed.
You can check to see if the type is a value type by using type.IsValueType.

Now there also might be a problem with using just strings for the values. Not
all "objects" have string representations that can be converted. If you
somehow limit the use of your class to value types and possibly enumerations
this all may work.

Hope this helps.

smc750
http://www.certdev.com

ma***********@gmail.com wrote:
I am writing a generic property table as part of my application

property_name
property_type
property_value

All information is stored in the database as strings.

In my application that maintains this data, I want to ensure that the
user cannot type in a value that cannot be converted to the specified
property_type. There is no list of allowable property types. Any type
or enum that exists in the .Net Framework, or a custom type or enum
derived from the .Net framework is fair game in my application.

I can't seem to figure out how to verify this.

For example:

property_name = "RadiobuttonList.RepeatLayout"
property_type = "System.Web.UI.WebControls.RepeatLayout"
property_value = "Flow"

What code can I write to verify that "Flow" can be converted to the
type specified by the string "System.Web.UI.WebControls.RepeatLayout"?

Here is some of the code that I have tried

Dim strPropertyType As String
Dim strPropertyValue As String
Dim objType As System.Type

strPropertyValue = "Flow"
strPropertyType = "System.Web.UI.WebControls.RepeatLayout"

objType = System.Type.GetType(strPropertyType)
Try
System.Convert.ChangeType(strPropertyValue, objType)
Catch objException As System.Exception
ErrorMessage = "Invalid Property value specified"
End Try

This seems to work for types like String, Int32, etc, but not for Enums
(at least not for System.Web.UI.WebControls.RepeatLayout).

Help!

Remember, at design time, I know NOTHING about the type information I
will be working with.

Thanks

Mark Olszowka


--
smc750
www.certdev.com

Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...neral/200605/1
May 19 '06 #2
For enums you can use Enum.Parse() which will convert it for you .. you can
also use Enum.GetValues() if you want to view the available items (say for
displaying a drop down for the user to choose from).

Cheers,

Greg Young
MVP - C#
<ma***********@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
I am writing a generic property table as part of my application

property_name
property_type
property_value

All information is stored in the database as strings.

In my application that maintains this data, I want to ensure that the
user cannot type in a value that cannot be converted to the specified
property_type. There is no list of allowable property types. Any type
or enum that exists in the .Net Framework, or a custom type or enum
derived from the .Net framework is fair game in my application.

I can't seem to figure out how to verify this.

For example:

property_name = "RadiobuttonList.RepeatLayout"
property_type = "System.Web.UI.WebControls.RepeatLayout"
property_value = "Flow"

What code can I write to verify that "Flow" can be converted to the
type specified by the string "System.Web.UI.WebControls.RepeatLayout"?

Here is some of the code that I have tried

Dim strPropertyType As String
Dim strPropertyValue As String
Dim objType As System.Type

strPropertyValue = "Flow"
strPropertyType = "System.Web.UI.WebControls.RepeatLayout"

objType = System.Type.GetType(strPropertyType)
Try
System.Convert.ChangeType(strPropertyValue, objType)
Catch objException As System.Exception
ErrorMessage = "Invalid Property value specified"
End Try

This seems to work for types like String, Int32, etc, but not for Enums
(at least not for System.Web.UI.WebControls.RepeatLayout).

Help!

Remember, at design time, I know NOTHING about the type information I
will be working with.

Thanks

Mark Olszowka

May 19 '06 #3
The problem I am having is getting

objType = System.Type.GetType(strPropertyType)

To execute successfully when strPropertyType is the string
representation of an enum, such as
System.Web.UI.WebControls.RepeatLayout. It errors on the GetType, and
therefore I do not have a type to call IsEnum on to determine if the
string representation represents an enum or not.

Can you get this line to work? Even using a different enum from the
framework?

May 19 '06 #4
I was able to get this to work with System.IO.FileAccess and System.DayOfWeek.
However, others will not work. Why is beyond me at this point. This is very
inconsistent behavior. There does not seem to be any pattern on why some
enumerations will work and others don't.

Sorry I could not help on this. I will let you know if I find anything out.

smc750
http://www.certdev.com

ma***********@gmail.com wrote:
The problem I am having is getting

objType = System.Type.GetType(strPropertyType)

To execute successfully when strPropertyType is the string
representation of an enum, such as
System.Web.UI.WebControls.RepeatLayout. It errors on the GetType, and
therefore I do not have a type to call IsEnum on to determine if the
string representation represents an enum or not.

Can you get this line to work? Even using a different enum from the
framework?


--
smc750
www.certdev.com

Message posted via http://www.dotnetmonster.com
May 20 '06 #5

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

Similar topics

4
by: Daniel Fudge | last post by:
I can't send a string from a dymanic array to a function. At the getpts function declarationon (line 14), I get the following error. "main.cpp(14) : error C2664: 'void __thiscall...
4
by: PengYu.UT | last post by:
Hi, Some dynamic polymorphism programs can be converted to the equavalent static polymorphism programs. I'm wondering if there are any generall procedures that I can use to do this conversion. ...
3
by: JDPope | last post by:
I have a situation which I cannot get a good lead on how to resolve. One of the applications I support uses the Hibernate software to generate SQL. The app is JAVA with JDBC. In testing the users...
6
by: Philipp Schumann | last post by:
Hi, I have a need for "dynamic type casting": in other words, in a "MyConvert" method I get passed an Object "value" and a Type "type" and the method should attempt to convert value into type. ...
3
by: Trev | last post by:
Hi, I have a series of functions which do the following: ValidateData( args ); //args if of type ArrayList // Work out if the data is valid or not, and work out the type of args - int, string,...
6
by: Nick Keighley | last post by:
Hi, Is this code fundamentally broken? class B { } class D: public B {
11
by: downwitch | last post by:
Hi, I'm using a 3rd-party app's back end which stores SQL statements in a table, so I have no choice but to use dynamic SQL to call them (unless someone else knows a workaround...) Problem...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
2
by: J. Peng | last post by:
Python's variable is dynamic type,is it? But why this can't work? Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unsupported operand type(s) for +: 'int' and 'str' ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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: 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
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?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.