困难的一个..请帮忙 [英] Difficult one.. please help

查看:62
本文介绍了困难的一个..请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我已经尝试解决这个问题,但我无法理解为什么它不起作用。


我想用我传递的类型动态地转换变量truogh一个字符串。

我收到一个值,想要在将它保存到XML之前检查类型是否正确。

我从xsd获取Type并传递这是一个将newvalue转换为正确类型的函数。

我想避免使用if语句检查类型,然后尝试转换它。


这是我的代码示例。


Dim strType as string =" System.Int32"

Dim val as object


Dim type As Type =(type.GetType(strType))


val = CType(val,Type)


我也试过传递一个''Type'类型的不同对象。


这可能吗?


请帮助

Hi I have trying to solve this but I can''t understand why it''s not working.

I want to cast a variable dynamically with a type that I pass truogh a string.
I am receiving a value and want to check if the type is correct before saving it to the XML.
I am getting the Type from xsd and pass this to a function to convert the newvalue to the proper type.
I want to avoid doing if statement to check for the type and then try to convert it.

This is an example my code.

Dim strType as string = "System.Int32"
Dim val as object

Dim type As Type = (type.GetType(strType))

val = CType(val, Type)

I have tried also passing a different object of type ''Type''

Is this possible?

Please help

推荐答案

Helllo神秘,


你是在发明轮子吗,一切都已经内置了。


一些例子

Dim a As Integer = 1

Dim b As String =" 2"

Dim c As Decimal = 10.1D

Dim d As Integer

Dim f As Decimal

Dim g As String

Dim h As Object

d = CInt(b)

g = c.ToString

f = CDec(a)

h = a

d = CInt(h)


你需要把它投到你想要的类型,尤其是VBNet

非常强大。 (我只向你展示了一小部分方法来

这样做)


Cor


我想要使用我传递给一个类型的动态转换一个变量一个

字符串。

我收到一个值并想在

将其保存到XML。

我从xsd获取Type并将其传递给函数以将

newvalue转换为正确的类型。

我想避免做if语句来检查类型,然后尝试将
转换成它。


这是一个例子我的代码。


Dim strType as string =" System.Int32"

Dim val as object

Dim type As Type =(type.GetType(strType))

val = CType(val,Type)

我也试过传递一个类型''Type''的另一个对象/>
这可能吗?


请帮助
Helllo mysterious,

Are you inventing the wheel, all is already build in.

Some examples
Dim a As Integer = 1
Dim b As String = "2"
Dim c As Decimal = 10.1D
Dim d As Integer
Dim f As Decimal
Dim g As String
Dim h As Object
d = CInt(b)
g = c.ToString
f = CDec(a)
h = a
d = CInt(h)

You need to cast it to the type you want and especially in that is VBNet
very powerfull. (I only showed you only a very small selection of methods to
do that)

Cor

I want to cast a variable dynamically with a type that I pass truogh a
string.
I am receiving a value and want to check if the type is correct before
saving it to the XML.
I am getting the Type from xsd and pass this to a function to convert the
newvalue to the proper type.
I want to avoid doing if statement to check for the type and then try to
convert it.

This is an example my code.

Dim strType as string = "System.Int32"
Dim val as object
Dim type As Type = (type.GetType(strType))
val = CType(val, Type)
I have tried also passing a different object of type ''Type''
Is this possible?

Please help


我想你错过了他的观点。他想在运行时进行转换,并在字符串中使用

typename来确定对象将被转换为什么类型。


-


OHM(特里伯恩斯)

。 。 。单手人。 。

如果你需要我的电子邮件,请问我


当你不知道自己在做什么时,过得很快


" Cor Ligthert" <无********** @ planet.nl>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
I think your missing his point. He wants to cast at runtime, and use the
typename in a string to determine what type the object will be cast to.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don''t know what you''re doing

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Helllo神秘,

你是在发明轮子,一切都已经内置。

一些例子
Dim a As Integer = 1
Dim b As String = " 2"
Dim c As Decimal = 10.1D
Dim d As Integer
Dim f As Decimal
Dim g As String
Dim h As Object d = CInt(b)
g = c.ToString
f = CDec(a)
h = a
d = CInt(h)
你需要把它转换成你想要的类型,特别是VBNet
非常强大。 (我只向你展示了一小部分方法
来做到这一点)

我希望动态地使用我传递的类型来转换变量truogh a
string。
我收到一个值,想要在将它保存到XML之前检查类型是否正确。
我从xsd获取Type并传递这是一个将
newvalue转换为正确类型的函数。
我想避免使用if语句检查类型,然后尝试转换它。

Dim strType as string =" System.Int32"
Dim val as object
Dim type As Type =(type.GetType( strType))
val = CType(val,Type)
我也试过传递一个''Type'类型的不同对象。
这可能吗?

请帮助
Helllo mysterious,

Are you inventing the wheel, all is already build in.

Some examples
Dim a As Integer = 1
Dim b As String = "2"
Dim c As Decimal = 10.1D
Dim d As Integer
Dim f As Decimal
Dim g As String
Dim h As Object
d = CInt(b)
g = c.ToString
f = CDec(a)
h = a
d = CInt(h)

You need to cast it to the type you want and especially in that is VBNet
very powerfull. (I only showed you only a very small selection of methods to do that)

Cor

I want to cast a variable dynamically with a type that I pass truogh a
string.
I am receiving a value and want to check if the type is correct before
saving it to the XML.
I am getting the Type from xsd and pass this to a function to convert the
newvalue to the proper type.
I want to avoid doing if statement to check for the type and then try to
convert it.

This is an example my code.

Dim strType as string = "System.Int32"
Dim val as object
Dim type As Type = (type.GetType(strType))
val = CType(val, Type)
I have tried also passing a different object of type ''Type''
Is this possible?

Please help



特里,


reas可能很多人都希望使用来自脚本

语言的Var,但是当你在一个对象中放置一个值时,没有任何需要




您可以通过命名接收值的类型来获取它。


Cor
Terry,

I know however I wrote inventing the wheel, why would that be needed, one
reason can be that a lot of people are looking to use the Var from scripting
languages, however when you put a value in a object there is not any need
for that.

You can get it back by just naming the type of the receiving value.

Cor


这篇关于困难的一个..请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆