none
Visual basic random number range selector RRS feed

  • Question

  • I am trying to make a mini game in my project were you have to solve the value of x which is randomly generated depending on the difficulty that the user chooses. Although when i try to do this and test it out by saying console.writeline(x) it says that x is inaccessible due to it's protection level. 

            If userinput = "a" Then
    
                Dim x As Integer = Rnd.Next(1, 10)
    
            ElseIf userinput = "b" Then
                Dim x As Integer = Rnd.Next(1, 20)
    
            ElseIf userinput = "c" Then
                Dim x As Integer = Rnd.Next(1, 30)
    
            End If
            Console.Clear()
            Console.WriteLine("Try and guess x.")
    
            userinput = Console.ReadLine
            Console.WriteLine(x)

    Does anybody know how to fix this?

    Monday, January 15, 2018 6:40 PM

All replies

  • It's because of 'scoping' where x needs to be declared before hand.

    Module Module1
    
        Sub Main()
            Dim userinput As String = ""
            Dim x As Integer
            Dim Rnd As Random = New Random()
    
    
            If userinput = "a" Then
    
                x = Rnd.Next(1, 10)
    
            ElseIf userinput = "b" Then
                x = Rnd.Next(1, 20)
    
            ElseIf userinput = "c" Then
                x = Rnd.Next(1, 30)
    
            End If
            Console.Clear()
            Console.WriteLine("Try and guess x.")
    
            userinput = Console.ReadLine
            Console.WriteLine(x)
    
        End Sub
    
    End Module
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by IronRazerz Monday, January 15, 2018 7:45 PM
    Monday, January 15, 2018 7:10 PM
    Moderator
  •  You may also want to set your Rnd.Next statements to be like below if you actually want it to generate a number from 1 to 10,  1 to 20,  and 1 to 30.  The method will generate a random number starting at the first number (1) and the last number minus 1.  So,  as you have it,  it would only generate a numbers from 1 to 9,  1 to 19, and 1 to 29.  Then again,  maybe that is what you want.

    Rnd.Next(1, 11)
    
    Rnd.Next(1, 21)
    
    Rnd.Next(1, 31)


    If you say it can`t be done then i`ll try it

    Monday, January 15, 2018 7:44 PM
  • I am trying to make a mini game in my project were you have to solve the value of x which is randomly generated depending on the difficulty that the user chooses. Although when i try to do this and test it out by saying console.writeline(x) it says that x is inaccessible due to it's protection level. 

            If userinput = "a" Then
    
                Dim x As Integer = Rnd.Next(1, 10)
    
            ElseIf userinput = "b" Then
                Dim x As Integer = Rnd.Next(1, 20)
    
            ElseIf userinput = "c" Then
                Dim x As Integer = Rnd.Next(1, 30)
    
            End If
            Console.Clear()
            Console.WriteLine("Try and guess x.")
    
            userinput = Console.ReadLine
            Console.WriteLine(x)

    Does anybody know how to fix this?

    The way that code you show is written, the variable "userinput" gets set AFTER it is tested to see if it is "a","b", or "c", so whet you test it, it is an empty string. The other issue has already been addressed. Don't forget that when the user tries to guess "x", which is a number, you should make sure they enter a number.

    Tuesday, January 16, 2018 2:12 AM
  • Hi Fred,

    Variables in VB.NET have a very particular scope, limiting their availability to various portions of your code depending on how and where they are declared.

    Your X variable has procedure-level scope, which means that it is available only within the procedure in which it was declared. In your case, it's declared in the If...else.., so it will only be available to code within that part.

    If, instead, you want to be able  to access the out of this If...else, you just need to declare this variable out of this If..else.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Wednesday, January 17, 2018 7:06 AM
    Tuesday, January 16, 2018 2:58 AM
    Moderator
  • I am trying to make a mini game in my project were you have to solve the value of x which is randomly generated depending on the difficulty that the user chooses. Although when i try to do this and test it out by saying console.writeline(x) it says that x is inaccessible due to it's protection level. 

            If userinput = "a" Then
    
                Dim x As Integer = Rnd.Next(1, 10)
    
            ElseIf userinput = "b" Then
                Dim x As Integer = Rnd.Next(1, 20)
    
            ElseIf userinput = "c" Then
                Dim x As Integer = Rnd.Next(1, 30)
    
            End If
            Console.Clear()
            Console.WriteLine("Try and guess x.")
    
            userinput = Console.ReadLine
            Console.WriteLine(x)

    Does anybody know how to fix this?

    Changing the scope of the variables.

        Private x As Integer
        Private Shared prng As New Random()
        Sub Main()
            Dim userinput As ConsoleKeyInfo
            Console.WriteLine("Enter difficulty(a, b, c).")
            userinput = Console.ReadKey
    
            Select Case userinput.Key
                Case ConsoleKey.A
                    x = prng.Next(1, 10)
    
                Case ConsoleKey.B
                    x = prng.Next(1, 20)
    
                Case ConsoleKey.C
                    x = prng.Next(1, 30)
    
                Case Else
                    'todo
    
            End Select
    
            Console.Clear()
            Console.WriteLine("Try and guess x.")
    
            Dim guess As String = Console.ReadLine
            Console.WriteLine(x)
    
        End Sub
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein


    • Edited by dbasnett Tuesday, January 16, 2018 2:04 PM
    Tuesday, January 16, 2018 2:03 PM