Results 1 to 9 of 9

Thread: Variable 'Random Number' is used before it has been assigned a value.

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    9

    Variable 'Random Number' is used before it has been assigned a value.

    Variable 'RandomNumber' is used before it has been assigned a value. A new reference exception could result at runtime.

    Code:
    Code:
        Sub RandomizeConLandLaunch(ByRef con As Integer)
            Dim RandomNumber(,) As Integer
            ConLandLaunch(con) = RandomNumber(1, 30000)
        End Sub
    what should i do ?

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Variable 'Random Number' is used before it has been assigned a value.

    Have you run that code?... what happen when you do?
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Variable 'Random Number' is used before it has been assigned a value.

    Have you run that code?... what happen when you do?

    this line
    Code:
    Dim RandomNumber(,) As Integer
    creates a 2 dimensional array of integers, but since you did not supply values to indicate the size of the array, the array does not exist. When you do this...

    Code:
    ConLandLaunch(con) = RandomNumber(1, 30000)
    you are assigning the value that resides at the 1,30000 position of the RandomNumber array, but that value does not exist, so your code will (should) error

    What are you trying to do?
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    9

    Re: Variable 'Random Number' is used before it has been assigned a value.

    > What are you trying to do?
    ----------------------------
    Fix that warning out...?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Variable 'Random Number' is used before it has been assigned a value.

    Quote Originally Posted by kebo View Post
    this line
    Code:
    Dim RandomNumber(,) As Integer
    creates a 2 dimensional array of integers
    Actually it doesn't, and that's the problem. Your implication is correct but your terminology is not. That code just declares a variable that CAN refer to an array but it doesn't actually create any array. As such the variable is Nothing, i.e. it doesn't refer to an object. Variables and objects are two different things. Variables are declared and objects are created.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Variable 'Random Number' is used before it has been assigned a value.

    Unless I'm mistaken, it looks like you are trying to get a random value between 1 and 30000. If that's the case, then use an instance of the Random class and call Next.

  7. #7
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Variable 'Random Number' is used before it has been assigned a value.

    Quote Originally Posted by jmcilhinney View Post
    Actually it doesn't, and that's the problem. Your implication is correct but your terminology is not. That code just declares a variable that CAN refer to an array but it doesn't actually create any array. As such the variable is Nothing, i.e. it doesn't refer to an object. Variables and objects are two different things. Variables are declared and objects are created.
    thanks for the clarification
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    9

    Re: Variable 'Random Number' is used before it has been assigned a value.

    I asking, because i got "1 WEEK OF EXPERIENCE" in VB 6.0, but i really didn't like VB 6.0, so i try to fix the code by the very useful help of "VBUC", from VB 6.0 to Microsoft Visual Studio 2008.
    Now the code are "fixed", but not at 100% ..
    I got this warning and just ask how to fix it..
    If any1 knows, just post:
    ------------------------
    Dude, your code:
    Code:
    THE CODE
    IS WRONG ..

    You have to change it like this:
    Code:
    THE RIGHT CODE
    Im absolute beginner, so i need really basic explanation =))
    Thanks.
    Last edited by neothod; Aug 25th, 2009 at 08:49 PM.

  9. #9
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Variable 'Random Number' is used before it has been assigned a value.

    The first thing you need to answer is what you are trying to do, and I don't mean 'get rid of the warning'. Are you trying to generate a random number? Do you actually want a multi-dimensional array of that length?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width