在多个子中使用变量及其值

时间:2014-05-08 19:57:28

标签: vb.net variables console-application

这是我的代码。

Module Module1
    Dim a, x, y As Integer
Public Delegate Sub Areas()
Sub Main()
    Dim Square As Areas
    Dim Rectangle As Areas
    Dim Add As Areas
    Dim Area As [Delegate]
    Square = New Areas(AddressOf areasquare)
    Rectangle = New Areas(AddressOf arearect)
    Add = New Areas(AddressOf addition)
    Area = [Delegate].Combine(Square, Rectangle, Add)
    Area.DynamicInvoke()
    Console.WriteLine("the combined areas are equal to " & x * y + a * a)
    Console.ReadKey()
End Sub
Class SqareArea
    Public Function mult(ByVal a As Integer)
        Return a * a
    End Function
End Class
Class RectangleArea
    Public Function mult(ByVal x As Integer, ByVal y As Integer)
        Return x * y
    End Function
End Class
Public Sub areasquare()
    Dim a As Integer
    Dim objSqareArea As New SqareArea()
    Console.WriteLine("What is the measurement of one side of the square?")
    a = Int32.Parse(Console.ReadLine)
    Console.WriteLine("The area of the square is " & objSqareArea.mult(a))
End Sub
Sub arearect()
    Dim x, y As Integer
    Dim objRectangleArea As New RectangleArea()
    Console.WriteLine("What is the length of the rectangle?")
    x = Int32.Parse(Console.ReadLine)
    Console.WriteLine("What is the width of the rectangle?")
    y = Int32.Parse(Console.ReadLine)
    Console.WriteLine("The area of the rectangle is " & objRectangleArea.mult(x, y))
End Sub
Public Sub addition()
    Console.WriteLine("the combined areas are equal to " & x * y + a * a)
End Sub
End Module

我使用来自用户的变量乘以两组数字。 然后我想拿两个操作的产品并将它们加在一起。 所以我想使用其他潜艇的变量。我该怎么做?

1 个答案:

答案 0 :(得分:1)

取出

Dim a As Integer
Public Sub areaSquare()

中的

Dim x, y As Integer
Sub areaRect()中的

Module Module1
    Dim a, x, y As Integer
    Public Delegate Sub Areas()
    Sub Main()
        Dim Square As Areas
        Dim Rectangle As Areas
        Dim Add As Areas
        Dim Area As [Delegate]
        Square = New Areas(AddressOf areasquare)
        Rectangle = New Areas(AddressOf arearect)
        Add = New Areas(AddressOf addition)
        Area = [Delegate].Combine(Square, Rectangle, Add)
        Area.DynamicInvoke()
        Console.WriteLine("the combined areas are equal to " & x * y + a * a)
        Console.ReadKey()
    End Sub
    Class SqareArea
        Public Function mult(ByVal a As Integer)
            Return a * a
        End Function
    End Class
    Class RectangleArea
        Public Function mult(ByVal x As Integer, ByVal y As Integer)
            Return x * y
        End Function
    End Class
    Public Sub areasquare()

        Dim objSqareArea As New SqareArea()
        Console.WriteLine("What is the measurement of one side of the square?")
        a = Int32.Parse(Console.ReadLine)
        Console.WriteLine("The area of the square is " & objSqareArea.mult(a))
    End Sub
    Sub arearect()

        Dim objRectangleArea As New RectangleArea()
        Console.WriteLine("What is the length of the rectangle?")
        x = Int32.Parse(Console.ReadLine)
        Console.WriteLine("What is the width of the rectangle?")
        y = Int32.Parse(Console.ReadLine)
        Console.WriteLine("The area of the rectangle is " & objRectangleArea.mult(x, y))
    End Sub
    Public Sub addition()
        Console.WriteLine("the combined areas are equal to " & x * y + a * a)
    End Sub
End Module

您正在声明类级变量,但在运行时会减少它们的值。

我的意见: 方块:5 矩形长度:10 矩形宽度:4

结果: 广场面积为25 矩形区域为40 合并面积等于65 组合面积等于65