簡體   English   中英

在VB中使用參數和類遇到麻煩

[英]Having trouble with arguments and classes in VB

我在使類工作並為這些類傳遞參數時遇到麻煩。 我已經找出了我自己遇到的其他大多數錯誤,但是確實有一個錯誤困擾着我。 我得到的錯誤是“ objAttendee”的。 錯誤顯示為“未為公共Sub New(ByVal strFirstName作為字符串,ByVal strLastName作為字符串,ByVal strCourses作為字符串,ByVal intDays作為字符串)的公共Sub New的參數” strFirstName”指定參數的參數”。 我完全不確定如何解決此問題。

有人可以幫我嗎?

Option Strict On

Public Class Attendee
    Dim objAttendee As New Attendee

    'class variables
    Private _strFirstName As String
    Private _strLastName As String
    Private _strCourses As String
    Private _intDays As Integer
    Private _decTotal As Decimal
    Private _decCostPerDay As Decimal = 350D
    Private _decPreConferenceCost As Decimal = 675D

    Sub New(ByVal strFirstName As String, ByVal strLastName As String, ByVal strCourses As String, ByVal intDays As String)
        'Contructor


        _strFirstName = strFirstName
        _strLastName = strLastName
        _strCourses = strCourses
        _intDays = Convert.ToInt32(intDays)

    End Sub

    Function ComputeCourseCosts() As Decimal

        'Calculates the cost if a pre-conference course is not selected
        _decTotal = _intDays * _decCostPerDay

        Return _decTotal
    End Function

    Function ComputePreConferenceCosts() As Decimal

        'Calculates the cost if a pre-conference course is selected
        _decTotal = _intDays * _decCostPerDay + _decPreConferenceCost

        Return _decTotal
    End Function
End Class

“未為參數指定參數“ .....

Plutonix告訴您問題的根源:

您正在您的Attendee類中聲明一個Attendee對象(這沒有任何意義)

Public Class Attendee
    Dim objAttendee As New Attendee ' <- offending code :P

但是該類的構造函數聲明如下:

    Sub New(ByVal strFirstName As String, ByVal strLastName As String, ByVal strCourses As String, ByVal intDays As String)

而且你沒有沒有像這樣的任何參數的構造函數:

    Public Sub New() ' No parameter required
        ' ...
    End Sub

並且您不必創建/聲明Attendee構造函數的這種變體,否則, 必須_strFirstName_strLastName_strCourses_intDays 定義默認值

認為 ...你在沖動測試與會者類,並宣布它里面的與會者變量)。

不,您應該 使用適當的參數在類外部(例如,在程序的主塊中Dim objAttendee As New Attendee(...)Dim objAttendee As New Attendee(...)聲明Dim objAttendee As New Attendee(...)

    Private Sub ShowMyClass(sender As Object, e As EventArgs) Handles Button1.Click
        Dim objAttendee As New Attendee("Simpson", "Homer", "Courses", "48")

        MessageBox.Show(objAttendee.ComputeCourseCosts().ToString())
    End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM