Re: Dynamic type conversion



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





mark.olszowka@xxxxxxxxx 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/Forums.aspx/dotnet-general/200605/1
.



Relevant Pages

  • Re: Is this expected behavior or not
    ... I've got a million or so lines of Ada code; ... I don't care that an explicit type conversion is expensive -- you ... that there is a correlation between the "expense" of the ... It is never a good idea to mix types in a program: String shouldn't be mixed ...
    (comp.lang.ada)
  • Proposal: String::Format::General
    ... It provides format string parsing and output assembly, you provide the code that implements the individual conversion characters. ... Format syntax is kind of a cross between sprintf and strftime, but how close it is to each of these depends on the semantics implemented by the user. ... Note that the following is pre-alpha documentation; the interface to the output conversion code has changed since yesterday, ... conversion character, and contain a number of optional fields which may ...
    (comp.lang.perl.modules)
  • Re: Is this expected behavior or not
    ... create one for already existing string types, ... That is *independent* on whatever Ada did or not before. ... such cases in Claw, for example, which uses Ada 95 inheritance extensively. ... User-defined conversion defined, programmer's responsibility [Ada does ...
    (comp.lang.ada)
  • Re: what if (f)printf returns EINTR ?
    ... vsnprintf - formatted output conversion ... int fprintf; ... write to the character string str. ...
    (comp.unix.programmer)
  • Re: Sets and portability (was) Re: Is ISO Pascal compatible with J&W (original) Pascal ?
    ... > So library code would be better if it could handle huge sets. ... widestring, in the future maybe also string). ... Note that ansi->wide conversion is codepage sensitive. ... compiler links in correct conversion code or table). ...
    (comp.lang.pascal.misc)