我似乎无法正确编写此代码。它涉及代表

时间:2017-05-23 01:30:29

标签: vb.net visual-studio delegates

所以我的提示是我必须编写我的代码在这里: 编写一个具有类形状的程序。 SquareArea()和RectangleArea()是分别计算正方形和矩形区域的两种方法。创建一个名为Area的委托,在委托的帮助下调用方法,并添加两个方法生成的结果。我希望这个对你有用。

问题:我收到了四个错误。 1)' Sub Main'没有在'模块1'中找到 - 我试图通过在模块模块1之后添加Sub Main()来解决这个问题,但这并没有解决任何问题。 2)' FnAreaSquare,FnAreaRectangle和FnAreaBoth'没有宣布。在我做过的其他任务中,我们就是这样做的。我无法弄清楚如何解决这些问题。所以,如果有人有时间,你能查看我的代码,并指出我可能犯的任何错误吗?谢谢。

这是我的代码:

Module Module1

    Public Delegate Sub Area()
    Sub Delegates()
        Dim Fns As Area
        Fns = New Area(AddressOf FnAreaSquare)
        Fns()
        Fns = New Area(AddressOf FnAreaRectangle)
        Fns()
        Fns = New Area(AddressOf FnAreaBoth)
        Fns()
    End Sub

    Public Class Shapes
        Dim SqSide, RectSide1, RectSide2 As Double

        Sub FnAreaSquare()

            SqSide = 6
            Console.WriteLine("The area of the square is " & (SqSide) ^ 2)
            Console.ReadKey()

        End Sub

        Sub FnAreaRectangle()

            RectSide1 = 7
            RectSide2 = 3
            Console.WriteLine("The area of the rectangle is " & (RectSide1 * RectSide2))
            Console.ReadKey()

        End Sub

        Sub FnAreaBoth()

            Console.WriteLine("The area of the both shapes added is " & (((SqSide) ^ 2) + (RectSide1 * RectSide2)))
            Console.ReadKey()

        End Sub

    End Class



End Module

这个问题真的让我感到害怕,因为我花了很多时间。知道那些可能对我有帮助的人是很棒的。谢谢你们!

更新:这是我的新代码。我有一切正常,但我无法弄清楚如何运行形状类中的内容。我必须在我的项目中包含Shapes类,所以我想我会在其中添加Answer和Answer 1。如果有更好的方法我可以在不改变程序结构的情况下将Shapes类实现到我的程序中,那将会有所帮助。感谢。

Module Module1
Dim SquareSide As Integer = 2
Dim RectangleLength As Integer = 2
Dim RectangleWidth As Integer = 3
Dim Answer As Integer
Dim Answer2 As Integer
Public Delegate Sub Area()

Sub Main()

    Dim Del As Area
    Del = New Area(AddressOf SquareArea)
    Del()

    Del = New Area(AddressOf RectangleArea)
    Del()

End Sub

Sub SquareArea()
    Answer = SquareSide ^ 2
    Console.WriteLine(Answer)
    Console.ReadKey()
End Sub

Sub RectangleArea()
    Answer2 = RectangleLength * RectangleWidth
    Console.WriteLine(Answer2)
    Console.ReadKey()
End Sub

Public Class Shapes

    Sub Shapes()

        Console.WriteLine(Answer + Answer2)

    End Sub


End Class

End Module

1 个答案:

答案 0 :(得分:0)

这是一个适合你的解决方案,我已经全面评论过,所以你可以看到发生了什么。此外,这将询问用户输入,如果您不需要它将其删除并硬编码您想要的值。只有一种计算方法,因为签名必须与委托匹配,您可以创建两个方法和两个委托,但您不需要。另外值得一提的是,可以创建一个函数,在将属性设置为另一个属性时进行计算,这样就不必在最后执行计算。

 Module Module1

'Create instance of Shapes class
Public shape As New Shapes

'Delegate to use we are going to invoke
Delegate Sub Area(ByVal x As Shapes)

Sub Main()

    AskForInput()

End Sub

''' <summary>
''' Asks user for input.
''' </summary>
Public Sub AskForInput()
    If Integer.TryParse(InputBox("Enter side size", "Square Size"), shape.SquareSide) Then
        If Integer.TryParse(InputBox("Enter rectangle length", "Rectangle Length"), shape.RectangleLength) Then
            If Integer.TryParse(InputBox("Enter rectangle width size", "Rectangle Width"), shape.RectangleWidth) Then
                'we are good to go now!

                ' Create an instance of the delegate.  
                Dim a As Area = AddressOf shape.CalculateArea

                'Call the method now.
                a.Invoke(shape)

            Else
                If MsgBox("Invalid Size", MsgBoxStyle.YesNo, "Continue?") = MsgBoxResult.Yes Then
                    AskForInput()
                End If
            End If
        Else
            If MsgBox("Invalid Size", MsgBoxStyle.YesNo, "Continue?") = MsgBoxResult.Yes Then
                AskForInput()
            End If
        End If
    Else
        If MsgBox("Invalid Size", MsgBoxStyle.YesNo, "Continue?") = MsgBoxResult.Yes Then
            AskForInput()
        End If
    End If
End Sub

Public Class Shapes

    Public Property SquareSide As Integer = 0
    Public Property RectangleLength As Integer = 0
    Public Property RectangleWidth As Integer = 0

    ''' <summary>
    ''' Calculates area of a square and rectangle.
    ''' </summary>
    ''' <param name="shape"></param>
    Public Sub CalculateArea(ByVal shape As Shapes)
        Console.WriteLine("Area of the square is: " & CStr(shape.SquareSide ^ 2))
        Console.WriteLine("Area of the rectangle is: " & CStr(shape.RectangleLength * shape.RectangleWidth))

        Console.Read()
    End Sub


End Class

End Module