Uniform(...) method

  • Thread starter Thread starter Curious
  • Start date Start date
C

Curious

Hi,

I am writing a class Distribution that inherits from Random class

public class Distribution : System.Random
{
public int Uniform(int min, int max)
{
return this.Uniform(min, max);
}

}

Now I wrote the new Uniform method, and I'm acessing the base method
having this name.

When I execute the application, a runtime error is given:
StackOverflowEception.

Distribution d = new Distribution();
d.Uniform(0, 5);

Can someone give some help regarding this method, because I cannot find
anything about it on the net.

Thanks in Advance
 
Hello,

In case you want to make this work then you need to put some logic in the
Uniform method to exit out of the recursive loop. Example you could use a
counter to let this run for a certain number of times and then exit out of
the loop.

Hope this helps...
Ramneek
 
Curious said:
I am writing a class Distribution that inherits from Random class

public class Distribution : System.Random
{
public int Uniform(int min, int max)
{
return this.Uniform(min, max);
}

}

Now I wrote the new Uniform method, and I'm acessing the base method
having this name.

No you're not - you're accessing the method you're calling it from. I
suspect you meant:

return base.Uniform(min, max);
 
Jon Skeet said:
No you're not - you're accessing the method you're calling it from. I
suspect you meant:

return base.Uniform(min, max);

That was what I thought he wanted also, except that I cannot find a Uniform
method on the System.Random class (either in 1.1 or 2.0), so it is *very*
unclear what the OP was trying to do!
 
Hi,
you are calling a recursive function infinitely so it gives stackoverflow
message. this.Uniform means the Uniform function of the class you are in
right now, and since u r already in Uniform function a recursive call to it
starts, which goes on untill stack overflows. You should fix this.

Ab.
http://joehacker.blogspot.com
 
Back
Top