12 Replies - 314 Views - Last Post: 12 October 2012 - 12:29 PM Rate Topic: -----

#1 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

need value for X from different sub

Posted 12 October 2012 - 11:42 AM

My value for x from the sub btn_new_click isn't being used in sub Btn_guess_click I'm not sure why, I'm learning to program. My code is:
Public Class Form1

    Private Sub Btn_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_new.Click
        Dim rng As New System.Random()
        Dim x = rng.Next(1, 1001)
    End Sub

    Private Sub Btn_guess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_guess.Click
        Dim guess As Integer
        txt_guess.Text = guess

        If guess = X Then
            MsgBox("Correct")
        ElseIf guess > X Then
            MsgBox("Too High")
        ElseIf guess < x Then
            MsgBox("too low")
        End If
    End Sub

    
End Class


Thank you please help.

Is This A Good Question/Topic? 0
  • +

Replies To: need value for X from different sub

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: need value for X from different sub

Posted 12 October 2012 - 11:45 AM

You went completely backward from another post on this topic. You need to declare x in your Class scope then you just set it in the btn_New.Click to your random result

Public Class Form1
Dim x as integer

Private Sub Foo()
 x = 100
End sub

Private Sub FooTwo()
'x still = 100 and you can use it here because you declared it at class level
End sub
End Class

This post has been edited by CharlieMay: 12 October 2012 - 11:47 AM

Was This Post Helpful? 1
  • +
  • -

#3 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Re: need value for X from different sub

Posted 12 October 2012 - 11:48 AM

So I put it above the first sub in the public class form 1 area.
  Dim rng As New System.Random()
    Dim x = rng.Next(1, 1001)

those two that is, then im not sure what you mean. Sorry.
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: need value for X from different sub

Posted 12 October 2012 - 11:51 AM

No,
Dim rng as New System Random()
Dim x as Integer


Then in your sub, you can use
x = rng.Next(1, 1001)

Now x can be used in your comparisons in the other sub.
Was This Post Helpful? 1
  • +
  • -

#5 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Re: need value for X from different sub

Posted 12 October 2012 - 11:53 AM

ok I understand what you mean now, but i'm still unsure as to how to only get it to generate a random number as variable X when the button is clicked if it's at class level.
Public Class Form1

    Dim rng As New System.Random()
    Dim x = rng.Next(1, 1001)

    Private Sub Btn_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_new.Click
I have put both lines up there in the class.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: need value for X from different sub

Posted 12 October 2012 - 11:57 AM

http://www.dreaminco...e-of-variables/

Because you declare the variable to the scope of the class. It is local throughout the entire class, including methods (subs & function) if you set it to a value anywhere in that class, it will retain its value.

Now, since you don't use rng anywhere but in your new button, it can be declared there but x is used in both your new button and your guess button so you have to declare it in a higher scope.

Check out the link I included and it may be a little more helpful.
Was This Post Helpful? 1
  • +
  • -

#7 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Re: need value for X from different sub

Posted 12 October 2012 - 12:05 PM

Ok I've done that but when X is generated in the new sub the guess sub is unable to access the value of x.
Public Class Form1

    Dim rng As New System.Random()
    Dim x As Integer

    Private Sub Btn_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_new.Click
        Dim x = rng.Next(1, 1001)
        lbl_test.Text = x
    End Sub

    Private Sub Btn_guess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_guess.Click
        Dim guess As Integer
        txt_guess.Text = guess

        If guess = x Then
            MsgBox("Correct")
        ElseIf guess > x Then
            MsgBox("Too High")
        ElseIf guess < x Then
            MsgBox("too low")
        End If
    End Sub


End Class


lbl_test.text = x was in the guess sub but staying blank when new was clicked. I moved it into the new sub and it's working now. each time you try to guess an answer it just says you're correct even though I know i'm not via the label.

I was reading about calling strings today while in class but I didn't really understand that, could that be used in this situation?
Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: need value for X from different sub

Posted 12 October 2012 - 12:14 PM

Because you are using Dim in the new sub

It's just X = rng.Next(0,1001)

Otherwise, you're changing the scope.
Was This Post Helpful? 1
  • +
  • -

#9 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Re: need value for X from different sub

Posted 12 October 2012 - 12:16 PM

Thanks now the things just saying too low for every guess.
Was This Post Helpful? 0
  • +
  • -

#10 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 288
  • Joined: 22-August 11

Re: need value for X from different sub

Posted 12 October 2012 - 12:18 PM

View PostLordAizen, on 12 October 2012 - 03:05 PM, said:

     Private Sub Btn_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_new.Click
        x = rng.Next(1, 1001)
        lbl_test.Text = x ' trying to put the value of x into a label here i'm guessing?
        ' lbl_test.Text = x.ToString("n0") ' have to convert the number into a string type. otherwise i think it's always zero.
    End Sub


Was This Post Helpful? 1
  • +
  • -

#11 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Re: need value for X from different sub

Posted 12 October 2012 - 12:21 PM

The label is working I was being even more stupid than usual trying to get it to display the number in the label when the new button was clicked while having the code in the guess sub =/
Was This Post Helpful? 0
  • +
  • -

#12 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: need value for X from different sub

Posted 12 October 2012 - 12:26 PM

Do you see why it's not working?

Why it's always saying too high?

It has to do with txt_Guess.Text and Guess and what you're doing with those two

The left side of an expression is set by the right side of the expression
Was This Post Helpful? 1
  • +
  • -

#13 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Re: need value for X from different sub

Posted 12 October 2012 - 12:29 PM

Thank you seems I forgot the destination = source rule the teacher taught me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1