Tim The Learner
Tim The Learner

Reputation: 75

Using a variable and its value across multiple subs

Here is my code.

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

I am multiplying two sets of numbers using variables from the user. Then I want to take the products of both operations and add them together. So I want to use the variables from other subs. How would I do that?

Upvotes: 0

Views: 229

Answers (1)

Mark C.
Mark C.

Reputation: 6450

Take out

Dim a As Integer

in Public Sub areaSquare()

AND

Dim x, y As Integer

in 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

You're declaring class level variables, but then diminishing their value during run-time.

My inputs: Side of square: 5 Length of rectangle: 10 Width of rectangle: 4

Results: Area of the square is 25 Area of the rectangle is 40 The combined areas are equal to 65 The combined areas are equal to 65

Upvotes: 1

Related Questions