我正在尝试使用从我的Web表单上的下拉列表中选择的值创建到期日期,但是我无法连接Month变量值和Year变量值.我收到错误:错误运算符'&'未定义类型'String'和System.Web.UI.WebControls.ListItem'.我也尝试使用"+"但得到同样的错误.
这是我的代码:
Dim Month = monthDropDownList.SelectedValue
Dim Year = yearDropDownList.SelectedItem
Dim MonthYear = Month & Year
Dim ExpirationDate As Date = MonthYear
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
你不想要SelectedItem.你想要的SelectedValue.您还应该明确声明您的变量.您也无法以这种方式创建日期.你需要使用整数.
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)
Run Code Online (Sandbox Code Playgroud)
作为一种轻微的"清洁"方式,我会使用:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
328 次 |
| 最近记录: |