Results 1 to 15 of 15

Thread: [RESOLVED] Can't declare a new Random variable

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Resolved [RESOLVED] Can't declare a new Random variable

    In the following code

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim rand As New random
    
            lblRandom.Text = rand.next.tostring
        End Sub
    End Class
    I get the error refering to the Dim statement, with a blue squiggle under random
    "Type Expected"

    Do I need a Imports for a specific namespace? I thought this worked in the past.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Can't declare a new Random variable

    You should not be declaring a Random object at that scope anyways, you should be declaring it at the Form level and either initializing it at the form level or in the form's load event:
    Code:
    Public Class Form1
        Private r As Random
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            r = New Random
        End Sub
    
    End Class
    However, to address your namespace issue, the Random class is apart of the System namespace so it should be imported by default.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Can't declare a new Random variable

    If there is a squiggle under random, then there should also be a little spot to click for a dropdown with suggestions. If you need to import a namespace, then it will offer to import the correct namespace as the first option. However, Random is part of the System namespace, so I'm not sure you could have gotten this far if Random wasn't there.

    Did you happen to name a variable 'random' somewhere at a higher scope?

    In any case, you shouldn't be doing what you are doing. You really shouldn't have more than one random object, or else you could run into problems. Have one Random object at form scope and just use that. If you start creating multiple Random objects, you would have to ensure that there are never two created in the same second, or they will produce identical sequences.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Can't declare a new Random variable

    dday9

    I tried your code and I got the same error. So is there any guess why it is not being imported by default? and more importantly how to fix it?

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Can't declare a new Random variable

    huh... well, considering it didn't change the case from random to Random, I'd hazard a guess it's not finding it for some reason. Try changing it to Random() <- with the parens, and see if that helps.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Can't declare a new Random variable

    I just closed down VS2013 (I am using Premium) and created a new project with exactly the same results.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Can't declare a new Random variable

    VS changes Random to random by itself. The parentheses don't help - same error.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Can't declare a new Random variable

    If it's changing it to random(lower-cased) then what is probably happening is that you're having a namespace confliction where VS is finding more than one Random class and it's picking which one it thinks that it should be. Try changing it to:
    Code:
    Public Class Form1
        Private r As System.Random
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            r = New System.Random
        End Sub
    
    End Class
    And see if that works.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Can't declare a new Random variable

    agreed, if it's changing it to lowercase, you have an object conflict that's taking precedence over the class........ what's the name of your project? By chance is it.... "random"?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Can't declare a new Random variable

    OK,

    The error goes away if I write

    Code:
        Dim rand As New System.Random()
    For kicks, I tried
    Code:
    Imports System
    Dim rand As New random()
    But the error came back.

    While this is not how it is suppose to work, I do have a workaround, so I am marking this resolved.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Can't declare a new Random variable

    It was random. I tried a new app called R and it worked fine. Great guess, and thanks for the help.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [RESOLVED] Can't declare a new Random variable

    I'd strongly suggest that you look further into this. Case correcting is a wonderful thing, partly because it alerts you to issues, as this is doing. There is a reason why this is happening, though what it is will take some investigation. Whatever it is, you really should know about it.

    The first thing I would do would be to use Ctrl+F to seek all instances of the word random in the entire project. Look at each one of them. The most likely scenario is that you have used the word for something like a variable name somewhere (if it was used as a class name, then it would be a type, so the warning shouldn't be worded as it was).

    Second, are you importing any dlls (or is this code in a dll)? It would be MUCH harder for either of those to cause this kind of trouble. In fact, I'm not sure that it could by accident.

    EDIT: Nevermind, you posted as I was writing this. It looks like you found the culprit. The only thing I would say is this (again): Case correcting is a good thing. Add at least one capital letter to every name, then type everything in lower case. If the case isn't corrected, there is a problem that will bite you sooner or later. In this case it was sooner.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: [RESOLVED] Can't declare a new Random variable

    I agree with what you said. In my case I boiled down the app to just creating the random variable - what you saw was the whole code. Very plain vanilla to try to figure out what was going on. That is why it was so confusing to me. I did not realize that the name of the project could cause this problem. Again, thanks for your help.

  14. #14
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: [RESOLVED] Can't declare a new Random variable

    The reason is because the Application name become the Root Namespace for the application... for example the FQN for your form would have been random.Form1 ... it changing the R to r was the clue... I luckily guessed that the code was all there was, so the only reason that should happen is if there was a namespace conflict and the only thing that made sense was if the application namespace interfered, which would happen if it happened to be "random". Fortunately you named it random, rather than Random... or we might still be trying to figure it out!

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: [RESOLVED] Can't declare a new Random variable

    This is one reason when encountering the problem is to create a new project using the defaults to paste in some code.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

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