0

I have a error trying to do the following code

Random r = new Random(); r.Next(10, 100); 

But I have a error:

Error 1 'ChaseRP_Admin_Control.AdminCP2.Random' does not contain a definition for 'Next' and no extension method 'Next' accepting a first argument of type 'ChaseRP_Admin_Control.AdminCP2.Random' could be found (are you missing a using directive or an assembly reference?)

C:\Users\Someone\documents\visual studio 2013\Projects\ChaseRP Admin Control\ChaseRP Admin Control\AdminCP2\Random.cs 24 39 ChaseRP Admin Control

2
  • Uhm, did you create a class named Random? Commented Dec 11, 2013 at 21:54
  • 3
    You appear to have defined your own Random class (in Random.cs). You're not using System.Random from the .Net framework. Commented Dec 11, 2013 at 21:54

3 Answers 3

7

You have another class named Random in your assembly. It doesn't have a Next() method, like System.Random. You need to either change the name or specify System.Random explicitly, e.g.:

var r = new System.Random(); //look at the difference. r.Next(10, 100); 
Sign up to request clarification or add additional context in comments.

Comments

5

ChaseRP_Admin_Control.AdminCP2.Random

You made your own Random class, which doesn't have a Next() method.
Either rename that class, or qualify the original one with its namespace (System.Random)

Comments

3

From the error, you have a class in your namespace ChaseRP_Admin_Control.AdminCP2 that has the name Random that doesn't have a Next() method. You can change the name of the class.

Alternatively, You can put the System namespace in front of the random to tell the compiler that you want the random from the system namespace not the one in your class.

System.Random r = new System.Random(); 

7 Comments

"You should change the name of the class" --- namespaces were invented to not rename a class as soon as you have a collision with someone else's class name
@zerkms, but in this particular case, I think the OP should change the name of class, I would never like any of my class name to be same as something provided by .Net framework. IMHO.
@zerkms While that's true, it's still generally a good idea to avoid using names of types in the system namespace, because odds are high users of your class will have a using for that namespace, thus causing a name collision. When the colliding type is in a namespace unlikely to be used in conjunction with yours, it's not an issue.
@Servy: it's a good idea to give the semantically meaningful names. If there are collisions - you can always give a different alias with using (or use FQN)
@zerkms - on other hand explicitly creating collisions with well known system classes is questionable practice from my point of view. Indeed it is entertaining to see someone hit this issue... even more fun would be to define Random.Next to return 4 but it may be too cruel.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.