【问题标题】:I just cant seem to write this code correctly. It involves Delegates我似乎无法正确编写此代码。它涉及到代表
【发布时间】:2017-05-23 01:30:28
【问题描述】:

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

问题:我收到四个错误。 1) 在“Module1”中找不到“Sub Main” - 我试图通过在 Module Module 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 类中也发生的事情。我必须在我的项目中包含 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

【问题讨论】:

  • 您没有解释您的代码遇到的问题,也没有提出具体问题(或任何问题,就此而言)。这不是 的网站 这是我的作业的副本/粘贴和我的代码转储到目前为止。有人可以帮我完成吗? 帖子。您的代码具体有什么困难,我们可以为您回答什么具体问题
  • @KenWhite 感谢您的快速回复。对不起,我会更清楚。我不知道为什么代码不能正常工作。不幸的是,我的课是在线的,并且含糊不清地涵盖了主题。我们被分配了这项任务,但很少有笔记可以提供帮助。您能否澄清一下我的程序可能有什么问题,以便我可以修复它?如果作业不是为我完成的,我会更愿意,这样我就可以自己学习如何去做。谢谢!
  • 不,我无法澄清你的项目出了什么问题,因为你没有解释你遇到了什么困难。 它不起作用 不是有用的问题描述。 具体而言是如何不工作?请edit您的帖子并提供具体细节。解释您希望代码做什么、它正在做什么或它做错了什么,并提出一个具体问题为什么它不起作用? 并不具体。我们随时为您提供帮助,但您需要努力清楚地解释困难并提出可以回答的问题。
  • 再一次,edit 您的帖子并在那里添加信息。如果它不在问题中,它就不存在。不要将细节埋在 cmets 中。把它们放在问题中。
  • @KenWhite 我已经添加了问题,感谢您的支持。

标签: vb.net visual-studio delegates


【解决方案1】:

这是为您提供的解决方案,我已对其进行了全面评论,以便您了解正在发生的事情。这也将要求用户输入,如果您不需要它,请将其删除并硬编码您想要的值。只有一种计算方法,因为签名必须与委托匹配,您可以创建两个方法和两个委托,但您不需要。值得一提的另一件事是,您可以创建一个函数,当属性设置为另一个属性时进行计算,这样您就不必在最后执行计算。

 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

【讨论】:

  • 非常感谢您的帮助!该代码对我来说似乎很有意义,但我相信它已经超越了我们迄今为止所学到的。我已经更新了我的代码,多亏了你的帮助,其中很多都得到了纠正,但我似乎被困在一个问题上。请阅读我的更新部分以获取更多信息。再次感谢!!!
  • 我不确定我在关注你吗?它是您需要的一类和您需要的代表。我提到如果您不需要输入,您可以根据需要将其删除。什么是困难的或者你提到的通过了你所学的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 2010-11-22
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
相关资源
最近更新 更多