Mizuchi Mizuchi - 3 months ago 13
C# Question

How to access the Form Tools from Other Classes

I'm new to programming and theres something i cant do and cant find from the internet about the problem i have.

I'm making a basic Rullet game for practice.

I want to make the Calculations in a separate Class for the sake of "Clean Coding".

But i cant manage to control things like radioButton, CheckBox, TextBox from the Calculation Class i made.

How can i access them?

For example i want to check if the Rulet number if Even or Odd from another class.

// form.cs

public static int ruletNumber;

private void btnPlay_Click(object sender, EventArgs e)
{
Random rnd = new Random ();
ruletNumber = rnd.Next(0,37);
Calculation.Calc();

}



//Calculation.cs
class Calculation
{
public static void Calc()
{
// this is where i must see if the radioButton is Checked or Not, but it dosent see it from this Class...
if (ruletNumber / 2 == 0 && rdbEven.Checked == true)
{
//some code...
}
}

}

Answer

Pass those parameters to your Calc() method like

ruletNumber = rnd.Next(0,37);
Calculation.Calc(ruletNumber, rdbEven.Checked); 

In which case you will have to change the method signature in your class like below. Also, I believe your condition ruletNumber / 2 == 0 should be ruletNumber % 2 == 0 since you are trying to check whether it's odd/even number

class Calculation
{
        public static void Calc(int ruletNumber, bool radiostate)
        {
        if (ruletNumber % 2 == 0 && radiostate)
        {
            //some code...
        }
    }
Comments