我遇到了一个“荒谬”的问题。
我试图将字符串转换为 int16(我被迫在 int16 中而不是在 int32/integer 中执行此操作)。
我的第一个想法是尝试:
convertedVal = Convert.ToInt16(newVal)
Run Code Online (Sandbox Code Playgroud)
抛出异常:值对于 UInt16 来说太大或太小。
但是我的字符串是“10”,所以它在 minValue 和 maxValue 之间。
我使用以下方法解决了我的问题:
convertedVal = Int16.Parse(newVal) 'TryParse works also
Run Code Online (Sandbox Code Playgroud)
虽然我解决了我的问题,但我不明白我做错了什么。
有人可以向我解释为什么会发生这种情况吗?
谢谢你的时间
我有一个xml蚂蚁我试图chcke如果一个元素存在,如果是,那么如果它有一个值
xml示例:
<Attributes Version="1.0.2012">
<OpenAtStart>True</OpenAtStart>
<RefreshTime>60</RefreshTime>
</Attributes>
Run Code Online (Sandbox Code Playgroud)
所以我想检查OpenAtStart是否存在,然后我想检查它是否有值:所以我构建了函数,如下
Private Function existsOrEmpty(ByVal type As Type, ByVal node As XmlNode, ByVal defaultValue As Object) As Object
Dim myObj As Object = Nothing
Try
Cursor.Current = Cursors.WaitCursor
If node IsNot Nothing Then
Select Case type
Case GetType(Integer)
If Integer.TryParse(node.InnerText, myObj) = False Then
myObj = defaultValue
End If
Case GetType(Double)
If Double.TryParse(node.InnerText, myObj) = False Then
myObj = defaultValue
End If
Case GetType(Boolean)
If Boolean.TryParse(node.InnerText, myObj) = False Then
myObj = defaultValue
End …Run Code Online (Sandbox Code Playgroud)