简体   繁体   中英

C# - How can I call a variable in a method from another in the same class

public class MathProblem
{
    public virtual void setproblem()
    {
        Random random = new Random();
        int num1 = random.Next(100);
        int num2 = random.Next(100);
        int sum = num1 + num2;
    }

    public virtual void askuserforanswer()
    {
        int answer;

        Console.WriteLine("Enter your answer");
        answer = Convert.ToInt32(Console.ReadLine());
    }

    public virtual void displayproblem()
    {

        Console.WriteLine( num1.ToString() + " + " + num2.ToString() + " = ?");

    }

}

I want to call num1 and num2 from setproblem method to displayproblem method. And I don't know how to do that.

Help please...

You can't. Variables declared inside a method are only usable inside that method. To be able to use them throughout the class you must declare them at class level:

 public class MathProblem
{
    private Random random = new Random();
    private int num1, num2, sum;

    public virtual void SetProblem()
    {
        num1 = random.Next(100);
        num2 = random.Next(100);
        sum = num1 + num2;
    }

    public virtual void AskUserForAnswer()
    {
        int answer;

        Console.WriteLine("Enter your answer");
        answer = Convert.ToInt32(Console.ReadLine());
    }

    public virtual void DisplayProblem()
    {

        Console.WriteLine( num1.ToString() + " + " + num2.ToString() + " = ?");

    }       
}

Also, note I've changed the method names to Pascal casing according to Microsoft's Capitalization Conventions.

You have to use fields.

 public class MathProblem
{
 int num1;
 int num2;

public virtual void setproblem()
{
    Random random = new Random();
    num1 = random.Next(100);
    num2 = random.Next(100);
    int sum = num1 + num2;
}

public virtual void askuserforanswer()
{
    int answer;

    Console.WriteLine("Enter your answer");
    answer = Convert.ToInt32(Console.ReadLine());
}

public virtual void displayproblem()
{

    Console.WriteLine( num1.ToString() + " + " + num2.ToString() + " = ?");

}

}

You can use fields like at class level:

public class MathProblem
{
    int num1;
    int num2;

    ... rest of code unchanged
}

If you would need these outside your class you can use properties like:

public class MathProblem
{
    public int num1 { get; private set;}
    public int num2 { get; private set;}

    ... rest of code unchanged
}

Or if you want to be able to set them from outside the class:

public class MathProblem
{
    public int num1 { get; set;}
    public int num2 { get; set;}

    ... rest of code unchanged
}

Also some tips:

  • if using properties use CamelCasing (eg Num1 instead of num1)
  • also change your method names in CamelCasing (eg SetProblem, AskUserForAnswer, DisplayProblem).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM