helpkelp helpkelp - 20 days ago 5
C# Question

Trying to Define the Value of an Int By Passing It As a Parameter in an Instanced Method (whew)

The title sounds really complex. I have been getting better with C# but I definitely need some help here. I'm not even sure if what I want to do is possible in the way I am trying to do it.

I have instanced a method from Class A to Class B, but now I am trying to use that method to define the value of an int that is initialized in Class B.

CLASS A

class RandomGenerator
{
public void GetRandomNum(int placeholder)
{
Random random1 = new Random();
placeholder = random1.Next(123456789, 500000000);

}


}


CLASS B

public partial class NewEntryPage : Page
{
public NewEntryPage()
{
InitializeComponent();

}

private void finbutton(object sender, RoutedEventArgs e)
{


int ParseID;
int ParseAge;
int AcctNo;
int CheckNo;

RandomGenerator instance1 = new RandomGenerator();
instance1.GetRandomNum(AcctNo); <--- **//this is what I can't figure out**


I'm hoping this question makes sense. Thank you!

Answer

You need to have GetRandomNum return the value instead of having it passed in.

public int GetRandomNum()
{
    Random random1 = new Random();
    int placeholder = random1.Next(123456789, 500000000);
    return placeholder;
}

You can then use that value in your function.

    RandomGenerator instance1 = new RandomGenerator();
    AcctNo = instance1.GetRandomNum();