Stizz1e
Stizz1e

Reputation: 59

Concatenating variables

I'm trying to create an expiration date using values selected from drop down lists on my web form, however I am unable to concatenate the Month variable value and Year variable value. I get the error: Error Operator '&' is not defined for types 'String' and System.Web.UI.WebControls.ListItem'. I have tried to use the "+" also but get the same error.

Here is the code I have:

Dim Month = monthDropDownList.SelectedValue
Dim Year = yearDropDownList.SelectedItem
Dim MonthYear = Month & Year
Dim ExpirationDate As Date = MonthYear

Any help would be greatly appreciated.

Upvotes: 1

Views: 338

Answers (1)

Joel Etherton
Joel Etherton

Reputation: 37543

You don't want SelectedItem. You want SelectedValue. You should also explicitly declare your variables. You also can't create a Date in this manner. You need to use integers.

Dim Month As Integer= Convert.ToInt32(monthDropDownList.SelectedValue)
Dim Year as Integer = Convert.ToInt32(yearDropDownList.SelectedValue)
Dim ExpirationDate As Date = New Date(Year, Month, 1)

As a slight "cleaner" way to do this I would use:

Dim Month as Integer
Dim Year As Integer
Dim ExpirationDate As Date

Integer.TryParse(monthDropDownList.SelectedValue, Month)
Integer.TryParse(yearDropDownList.SelectedValue, Year)
If (Month > 0 AndAlso Year > 0) Then
    ExpirationDate = New Date(Year, Month, 1)
End If

Upvotes: 5

Related Questions