2016-07-10 88 views
-4

我需要检查输入是否为空/空。如何检查输入是否为空/空

If input <= 100 And input > 0 And Not input = "" Then 

    subjectsInt.Add(subjects(i), input) 
    check = True 

End If 

我知道在该代码我检查它作为字符串,但我已经做了Not input = NothingNot input = 0,我得到一个错误:

Run-time exception (line -1): Conversion from string "" to type 'Integer' is not valid.

Stack Trace:

[System.FormatException: Input string was not in a correct format.]

[System.InvalidCastException: Conversion from string "" to type 'Integer' is not valid.]

有什么建议?

编辑

这里是你想看看在情况下,代码:https://dotnetfiddle.net/0QvoAo

+1

它必须是'AndAlso',你必须先测试它。所以'如果没有输入=“”AndAlso(输入> 0并且输入<= 100)然后'。并且还提供* short-circuit *表达式的评估。总是一个好主意,使用Option Strict On编程一段时间后,尼采会输入“asdf”来查看失败,并使用TryParse()。 –

+0

@HansPassant检查编辑部分:https://dotnetfiddle.net/0QvoAo – NietzscheProgrammer

+0

为什么我收到downvote?你们只是滥用这种功能。我的问题出了什么问题? – NietzscheProgrammer

回答

3

那么,我已经看了你的代码,并且有很多问题,这些都是VB.NET允许你将Option Strict设置为Off时数据类型的自由方法导致的。在这种情况下,VB.NET允许您将Console.ReadLine的返回值分配给Integer,以帮助您添加输入的隐式转换。当然,如果用户键入ABCD,则这种隐式转换没有其他方式可以通知您,而不是触发异常。所以我真的推荐你使用Option Strict设置为On(MSDN Option Strict

现在使用Option Strict在你的代码中有很多错误,我已经重写了你的代码,然后在注释中解释了这些改变。

Option Strict On 
Imports System 
Imports System.Collections.Generic 

Public Module Module1 
    Public Sub Main() 
     Console.WriteLine("Hello World") 

     ' I use a List of strings instead of an untyped array list 
     Dim subjects As New List(Of String)() From 
     { 
      "Math", 
      "English", 
      "German" 
     } 
     Dim subjectsInt As New System.Collections.Generic.Dictionary(Of String, Integer) 
     Dim i, input As Integer 
     Dim check As Boolean 

     For i = 0 To subjects.Count - 1 
      check = False 
      Do 
       Console.WriteLine(subjects(i) & ": ") 

       ' The input is passed to a string 
       Dim temp = Console.ReadLine() 

       ' Check if this string is really a number 
       If Int32.TryParse(temp, input) Then 
        If input <= 100 And input > 0 Then 
         subjectsInt.Add(subjects(i), input) 
         check = True 
        End If 
       End If 
      Loop While check = False 
     Next 
     For i = 0 To subjects.Count - 1 
      Console.WriteLine(subjects(i) & ": " & subjectsInt(subjects(i))) 
     Next 
     Console.ReadLine() 
    End Sub 
End Module 
1

的错误是告诉您input是一个整数,你不能用比较整数这正是发生的事情。

即使字符串只包含数字,类型系统也不会将其转换为来自运算符的转换。

在你的例子中,input = ""输入永远不能是“”。您应该将输入类型更改为字符串,然后在将其转换为整数之前进行检查。

这将是一个解决方案:

Dim integerInput As Integer  
Dim stringInput = Console.ReadLine() 
If Integer.TryParse(stringInput, integerInput) AndAlso integerInput <= 100 And integerInput > 0 Then 

要使用<> =一个整数,你需要将字符串转换:

If Integer.Parse(input) <= 100 And Integer.Parse(input) > 0 And Not input = "" Then 

Integer.Parse将做的工作。

很明显,如果你的输入是一个字符串,并不能保证它只包含数字,你需要先做一些检查或使用Integer.TryParse

最接近你可以得到一个值类型的空检查是EqualityComparer(Of Integer).Default.Equals(id, Nothing);除非您开始使用可为空,但是如果输入非数字字符串时代码将在input = Console.ReadLine()上失败,那就太迟了。

+1

'Integer.Parse会做这个工作',直到用户输入“我喜欢派” – Plutonix

+0

@Plutonix我认为很明显你需要做一个检查或使用TryParse。我会添加额外的评论。但是这篇文章并没有提到“输入”的来源。甚至可以保证它是一个整数。在发布的代码中没有用户输入。 –

+1

其实他的输入变量是Integer类型的。检查他的小提琴。 –