gem
gem

Reputation: 11

Passing data to objects via web form

I am trying to implement some VB.NET code that takes the credit card details from a web form and passes them into the payment object. I've done this for the personal details form on another page, which worked fine.

However, on this page I am getting the Too many arguments to Public Sub New() error for the following line:

Dim cardDetails As Payment = New Payment(Me.CardNumber.Text, Me.CardExpiryMonth.Text, Me.CardExpiryYear.Text)

This is the part that doesn't make any sense. It requires three arguments, and I'm sending it three arguments. There really isn't much more information to give. The below is both the code behind and the payment class.

In Payment.vb:

Imports Microsoft.VisualBasic
Public Class Payment
'declare variables, private
Private cardNo As String
Private expiryMth As String
Private expiryYr As String

''get and set for each variable
Private Property cardNumber() As String
    Get
        Return cardNo
    End Get
    Set(value As String)
        cardNo = value
    End Set
End Property
Private Property expiryMonth() As String
    Get
        Return expiryMth
    End Get
    Set(value As String)
        expiryMth = value
    End Set
End Property
Private Property expiryYear() As String
    Get
        Return expiryYr
    End Get
    Set(value As String)
        expiryYr = value
    End Set
End Property

'no argument constructor
Public Sub New()
End Sub

''sub methods/functions do stuff and return values
Public Overrides Function ToString() As String
    Return cardNo
End Function
Public Sub New(ByVal cardNumber As String, ByVal expiryMonth As String, ByVal expiryYear As String)
End Sub
End Class

And in Payment.aspx.vb I have:

Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click
'add the card details from the form to the object.
Dim cardDetails As Payment = New Payment(Me.CardNumber.Text, Me.CardExpiryMonth.Text, Me.CardExpiryYear.Text)

Upvotes: 1

Views: 92

Answers (2)

gem
gem

Reputation: 11

As I suspected... something not so obvious but simple. My site contains Payment.aspx which of course contains code behind Payment.aspx.vb with a Partial Payment Class. This was causing confusion over which Payment I was referring to when creating the object.

To fix I copied my code from Payment.vb into a new Class called Card.vb and changed the reference when creating the object to: Dim cardDetails As **Card**= New **Card**(Me.CardNumber.Text, Me.CardExpiryMonth.Text, Me.CardExpiryYear.Text)

Rebuilt the solution and the error is gone.

Upvotes: 0

Markus
Markus

Reputation: 22436

Your constructor should be:

Public Sub New(ByVal cardNumber As String, ByVal expiryMonth As String, ByVal expiryYear As String)
    Me.cardNumber = cardNumber
    Me.expiryMonth = expiryMonth
    Me.expiryYear = expiryYear
End Sub

If the problem is something else, please provide some more details.

Upvotes: 1

Related Questions