Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 用visualbasic实现堆栈_Vb.net_Stack - Fatal编程技术网

Vb.net 用visualbasic实现堆栈

Vb.net 用visualbasic实现堆栈,vb.net,stack,Vb.net,Stack,我尝试在VB.NET中实现一个堆栈类。我遇到的问题是在以下行中出现错误:Console.WriteLine(Stack1.push(item,top,maxSize,s1(top))“ 错误内容为:“System.IndexOutOfRangeException:”索引超出了数组的界限” 感谢您的帮助,以下是完整的代码解决方案: Class Stack Public Function isEmpty(top As Integer) As Boolean If top =

我尝试在VB.NET中实现一个堆栈类。我遇到的问题是在以下行中出现错误:
Console.WriteLine(Stack1.push(item,top,maxSize,s1(top))
“ 错误内容为:“
System.IndexOutOfRangeException:”索引超出了数组的界限

感谢您的帮助,以下是完整的代码解决方案:

Class Stack
    Public Function isEmpty(top As Integer) As Boolean
        If top = -1 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function isFull(top As Integer, maxSize As Integer) As Boolean
        If top = maxSize Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function push(item As String, top As Integer, maxSize As Integer, ByVal ParamArray s() As String)
        If isFull(top, maxSize) = True Then
            Console.WriteLine("Stack is full")
        Else
            top += 1
            s(top) = item
        End If
        Return s(top)
    End Function

    Public Function pop(item As String, top As Integer, ByVal ParamArray s() As String)
        If isEmpty(top) Then
            Console.WriteLine("Stack is Empty")
        Else
            item = s(top)
            top -= 1
        End If
        Return top & item
    End Function

End Class

Sub Main()
    Dim Stack1 As Stack = New Stack()
    Dim maxSize As Integer = 5
    Dim s1(maxSize) As String
    Dim top As Integer = -1
    Dim item As String = "Erica"


    Console.WriteLine(Stack1.push(item, top, maxSize, s1(top)))

End Sub

如果您不知道,.NET有一个。它看起来像top=-1,所以第四个parm就是问题。
ParamArray
与数组不同,这就是问题所在。@BrianMStafford我知道.NET框架中内置了一个堆栈类,但我正在尝试实现一个,以查看是否可以重新创建它。谢谢您的建议,我将研究它第四个参数。@laancelot所以我需要把
byVal数组s()作为字符串
?在你的声明中,去掉
Array
(甚至是
byVal
),你应该很好。回答一个类似的问题。