Jason
Jason

Reputation: 75

Trying to learn how to use IndexOf in VB.net

I just started learning how to use VB.net and I'm trying to figure out how to use the IndexOf method. I thought that I would be able to use it to search for multiple elements within a string but I'm receiving an error that says "Argument matching parameter 'startIndex' narrows from 'String' to 'Integer'" and I'm not sure why. Here is the code I'm using.

Module Module1

Sub Main()
    Dim cont As Integer = 0
    Do Until cont = 1
        Dim PlayerChoice As String
        Dim ComputerChoice As String
        Dim ran As New Random
        Dim num As Integer = ran.Next(1, 4)
        Dim PlayerName As String
        Dim check As Integer
        Console.WriteLine("Welcome to Rock, Paper, Scissors!")
        Console.WriteLine("Please enter your name.")
        PlayerName = Console.ReadLine()
        Console.Clear()
            Console.WriteLine("Would you like to choose rock, paper, or scissors?")
        PlayerChoice = Console.ReadLine()
        Do Until check <> -1
            check = PlayerChoice.IndexOf("rock", "paper", "scissors")
            Console.Clear()
        Loop
        Select Case num
            Case 1
                ComputerChoice = "rock"
            Case 2
                ComputerChoice = "paper"
            Case 3
                ComputerChoice = "scissors"
        End Select
        Console.WriteLine(PlayerName & ": " & PlayerChoice)
        System.Threading.Thread.Sleep(750)
        Console.WriteLine("Computer: " & ComputerChoice)
        If PlayerChoice = "rock" Then
            If ComputerChoice = "rock" Then
                Console.WriteLine("There was a tie!")
            ElseIf ComputerChoice = "paper" Then
                Console.WriteLine("The computer wins!")
            ElseIf ComputerChoice = "scissors" Then
                Console.WriteLine("You win, " & PlayerName & "!")
            End If
        ElseIf PlayerChoice = "paper" Then
            If ComputerChoice = "rock" Then
                Console.WriteLine("You win, " & PlayerName & "!")
            ElseIf ComputerChoice = "paper" Then
                Console.WriteLine("There was a tie!")
            ElseIf ComputerChoice = "scissors" Then
                Console.WriteLine("The computer wins!")
            End If
        ElseIf PlayerChoice = "scissors" Then
            If ComputerChoice = "rock" Then
                Console.WriteLine("The computer wins!")
            ElseIf ComputerChoice = "paper" Then
                Console.WriteLine("You win, " & PlayerName & "!")
            ElseIf ComputerChoice = "scissors" Then
                Console.WriteLine("There was a tie!")
            End If
        End If
        Console.ReadLine()
        Console.Clear()
        Console.WriteLine("Would you like to play again?")
        Console.WriteLine("Enter 0 to play again.")
        Console.WriteLine("Enter 1 to quit.")
        Console.ReadLine()
        Console.Clear()
    Loop
End Sub

End Module

Upvotes: 0

Views: 269

Answers (1)

Fredou
Fredou

Reputation: 20140

you cannot call indexof like that.

if you read the documentation you will find out that you MUST only search 1 string at a time. in your case you will have to do it 3 time or until you find a match

you can implement an helper method like this

Imports System.Runtime.CompilerServices

Module Module1

    Sub Main()
        Dim test As String = "paper"
        Console.WriteLine(test.IndexOf("rock", "paper", "scissors"))

        test = "rock"
        Console.WriteLine(test.IndexOf("rock", "paper", "scissors"))

        test = "scissors"
        Console.WriteLine(test.IndexOf("rock", "paper", "scissors"))

        test = "foobar"
        Console.WriteLine(test.IndexOf("rock", "paper", "scissors"))

        Console.ReadKey()
    End Sub

End Module

Public Module helper

    <Extension()>
    Public Function IndexOf(ByVal value As String, ParamArray values() As String) As Integer
        Return Array.IndexOf(values, value)
    End Function
End Module

Upvotes: 2

Related Questions