1

I'm learning C# and I'm currently playing with Monogame. What I'm trying to do is declare a public instance of the System.Random class called "randVar" in my Game1 class and then access it from my Testz (derived from Sprite) class. This is not working, it tells me "The name 'randVar' does not exist in the current context.' I'm scratching my head trying to figure out why, because randVar is public I thought I should be able to access it.

My sourcecode is https://gist.github.com/4225880.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Scott
  • 385
  • 1
  • 3
  • 16

4 Answers4

1

To be able to access the randVar member of the Game1 class, you have to first have an instance of the class. It's the same as any other public member of a class. It currently doesn't exist within the scope of the Testz class. If you're just wanting a random number, you should just do Random randVar = new Random() inside your Testz class.

PiousVenom
  • 6,888
  • 11
  • 47
  • 86
  • randVar is an instance of the System.Rand class. – Scott Dec 06 '12 at 17:15
  • Right, but it's a public member of the `Game1` class. To access it, you'll need to create an instance of `Game1`, and then call the `randVar`. Something like `Game1 game = new Game1(); game.randVar.Next();` – PiousVenom Dec 06 '12 at 17:17
0

You cannot reference instance variables defined in one class from another without instantiating the first class in the second. You can do the following in your TestZ class:

Game1 oGame1 = new Game1();

Then you can do:

location.X = oGame1.randVar.Next(0,100) * .01f * screenBounds.Width;
Sid Holland
  • 2,871
  • 3
  • 27
  • 43
0

You define randVar in in Game1 but use it TestZ. Just move the definition into TestZ or better yet Sprite.

Also, you should know that Random is not thread safe. So its better to have each class keep its instance.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • +1 I like all the comments in your code. So I countered the -1 vote on your question. – Richard Schneider Dec 06 '12 at 17:19
  • When dealing with Sprites, one typically has multiple threads controlling the sprite(s). A static Random variable as some people have recommended will get the code to compile, but will NOT work correctly. Google turn this http://www.albahari.com/threading/ up. – Richard Schneider Dec 06 '12 at 17:49
  • Thank you very much. Yay for learning. I moved the creation of the random object into my sprite class, but now all three testz sprites exhibit the exact same "random" behavior, it looks like they're all using the same random numbers. – Scott Dec 06 '12 at 18:30
  • As usual Jon Skeet has the answer (http://stackoverflow.com/questions/1785744/how-do-i-seed-a-random-class-to-avoid-getting-duplicate-random-values). Basically your spites all havethe same random seed. Use Jon's RandomHelper to initialise the sprite's randVar. – Richard Schneider Dec 06 '12 at 18:48
  • Thank you so much, that link was very informative. You're incredibly helpful! I've refactored and it's working beautifully. – Scott Dec 06 '12 at 19:40
  • No worries. Accepting my answer is thee accepted way of saying thanks. – Richard Schneider Dec 07 '12 at 00:21
-1

You have to add the static modifier to your variable's declaration:

public static Random randVar = new Random();
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78