substring和indexOf函数

时间:2011-10-09 05:50:08

标签: .net vb.net debugging

  

可能重复:
  substring and the indexOf method

我有这个问题,我正在圈子里。错误消息是“无法访问可以使用这些参数调用IndexOf”。

我的教授说“要搜索字符串洛杉矶和子字符串将从文本框返回它,但它需要知道什么位置开始返回一个单词,这就是IndexOf进来的地方。”这是否意味着子串和IndexOf一起使用?这就是我所做的,可能是问题所在。这是代码:

Private Sub btnOrder_Click(ByVal sender As Object, ByVal e As EventArgs)     Handles    btnOrder.Click
    'Declare variables
    Dim Price As Decimal
    Dim txtAddress As String = "Los Angeles"
    Dim DialogResults As String

    'Begin If Statements to determine whether value is a number

    If IsNumeric(txtPrice.Text) Then
        MessageBox.Show("Please enter a nummeric value.", "Error Message")
    End If
    If IsNumeric(txtQuantity.Text) Then
        MessageBox.Show("Please enter a numeric value.", "Error Message")
    End If
    Try
        'Condition for Pickup days
        If radPickUp.Checked = True Then
            Price = CDec(txtPrice.Text)
        ElseIf radNextDay.Checked = True Then
            Price = CDec(CDbl(txtPrice.Text) * 0.01)
        ElseIf radDays.Checked = True Then
            Price = CDec(CDbl(txtPrice.Text) * 0.05)
        End If
        'Condition for Weekdays or Weekends pickup
        If CDbl(Str(cboDays.Text)) = -1 Then
            Price = CDec(CDbl(txtPrice.Text) * 0.0925)
        End If
        If CBool(Int(txtAddress.Substring(0, 10))) Then
            Str(txtAddress.IndexOf(11, 0))
            DialogResults = CStr(MessageBox.Show("Your order is $ " & CDbl(txtPrice.Text) - 0.05))
        Else : DialogResults = CStr(MessageBox.Show("You order is $ " & CDbl(txtPrice.Text)))

        End If
    Catch ex As InvalidCastException
        MessageBox.Show("Please enter a valid numeric value to continue.")
    End Try    
End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    'Reset controls
    txtPrice.Clear()
    txtQuantity.Clear()
    txtName.Clear()
    txtAddress.Clear()
    cboDays.Text = String.Empty
    txtPrice.Focus()
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    'Quit the application
    Me.Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    MessageBox.Show("Hello", "A greeting.")
End Sub

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

Documentation是你的朋友

你打电话

Str(txtAddress.IndexOf(11, 0))

哪种语法无效。正确的语法是

string.IndexOf(searchString)

返回找到的字符串的索引位置

如果找不到,则

-1。

0如果为空