I keep getting this when I go to debug:
"Error 1 Operator '=' is not defined for types 'Integer' and 'System.Random'. C:\Documents and Settings\All Users\Documents\College-C\IT\VB.Net\Assignments\16.11\GuessNumber.vb 16 10 GuessNumber"

I'm getting the same thing for the greater than and less than operators. I've been banging my head for an hour now trying to figure out what I'm doing wrong here. Any help would be fantastic.


Here is the code:

Public Class GuessNumberForm
   Dim randNum As Random = New Random
   Dim myGuess As Integer = Val(guessNumberTextBox.Text)
   Private Sub newGameButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGameButton.Click

      'gen randNum
      Dim randNumGenerated As Integer = randNum.Next
      enterButton.Enabled = True
      newGameButton.Enabled = False
      outputLabel.Text = ""

   End Sub
   Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click

      'compares user's guess to randNum
      If myGuess = randNum Then
         outputLabel.Text = "Correct!"
         enterButton.Enabled = False
         newGameButton.Enabled = True
      ElseIf myGuess < randNum Then
         outputLabel.Text = "Too low..."
      ElseIf myGuess > randNum Then
         outputLabel.Text = "Too high..."
      End If

   End Sub

End Class ' GuessNumberForm
If myGuess = randNum Then

here you compare between integer and class Random

to solve this

If myGuess = randNumGenerated Then

same thing in the "<" and ">"
but you need to define randNumGenerated as global variable I think..
any way your game is hard :S at least put max and min number when you generate the random number

Thanks a lot. I do have that fixed. Here is what I now have:

Public Class GuessNumberForm
   Dim randNum As Random = New Random
   Dim myGuess As Integer = Val(guessNumberTextBox.Text)
   Dim randNumGenerated As Integer
   Private Sub newGameButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGameButton.Click

      'gen randNum
      randNumGenerated = randNum.Next(1, 100)
      enterButton.Enabled = True
      newGameButton.Enabled = False
      outputLabel.Text = ""

   End Sub
   Private Sub enterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles enterButton.Click

      'compares user's guess to randNum
      If myGuess = randNumGenerated Then
         outputLabel.Text = "Correct!"
         enterButton.Enabled = False
         newGameButton.Enabled = True
      ElseIf myGuess < randNumGenerated Then
         outputLabel.Text = "Too low..."
      ElseIf myGuess > randNumGenerated Then
         outputLabel.Text = "Too high..."
      End If

   End Sub

End Class ' GuessNumberForm

When I try to start debugging, it keeps telling me this: "System.InvalidOperationException was unhandled
Message="An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object."
Source="GuessNumber"
StackTrace:
at GuessNumber.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
at GuessNumber.My.MyProject.MyForms.get_GuessNumberForm()
at GuessNumber.My.MyApplication.OnCreateMainForm() in C:\Documents and Settings\All Users\Documents\College-C\IT\VB.Net\Assignments\16.11\My Project\Application.Designer.vb:line 35
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at GuessNumber.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()"

I'm not sure what to make of this at all.

hi! cut and paste the declaration below in the enterButton_click sub just above the if statement:

Dim myGuess As Integer = Val(guessNumberTextBox.Text)

Alright, that worked! Thanks a lot.

I don't understand why that made a diff, though. I had declared it as a global variable. Why couldn't I do that instead of having to have it declared inside the click event?

This article has been dead for over six months. Start a new discussion instead.