System.StackOverflowException交集错误

时间:2018-01-08 11:54:48

标签: vb.net

我希望我的程序获取一个变量,并找到字母A-Z。我已将程序的这一部分放在一个模块中,以便在两种不同的形式之间共享。

变量从form1传递,由模块处理,然后再次发送回form1。问题是我认为代码中存在某种错误,但我无法识别它。

Public Function UPCASES(ByRef password1, points) As Boolean
  Dim intersection As IEnumerable(Of Char)
  intersection = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".Intersect(password1)  
  'System.StackOverflowException error ^^^^^^^^^^^^^^^^^^^^^^^^^

  If intersection.Count() = 1 Then
     points = 5
  Else
     points = 0
  End If

  Return UPCASES(password1, points)
End Function

1 个答案:

答案 0 :(得分:0)

您正在方法结束时调用方法本身,这会导致StackOverflowException

Return UPCASES(password1, points)

我想这个方法应该检查密码是否包含大写字母,然后使用:

Dim containsUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".Intersect(password1).Any()

因此,如果您需要一种方法,则无需为此单行创建方法:

Public Function ContainsUpperCaseLetter(password1 As String) As Boolean
    Return "ABCDEFGHIJKLMNOPQRSTUVWXYZ".Intersect(password1).Any()
End Function

旁注:您应该更改默认项目设置以使用Option Strict ON(不幸的是Off是默认值)。然后,在学习了很多关于.NET类型的代码之后,您将能够编写更安全,健壮且高性能的代码,因为您必须修复编译器错误。

相关问题