Home > DeveloperSection > Forums > How to Call Public Variable/Method From One Class To MainForm.Cs?
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    October-27-2014 12:14 AM

 .NET C#  .NET 
Ratings:


 1 Reply(s)
 707  View(s)
Rate this:
I am trying to call:
public double lose;
public void LoseWeight()
{
        lose = weight - 400;
}
to
listBox.Items.Add("calories : " + lose); //on MainForm.cs

But for some reason, I'm getting error: The name "lose" does not exist in the current context.
I thought all public variables and methods can be shared.
Is there a special rule for getting things from class -> MainForm?


Pravesh Singh

Total Post:411

Points:2881
Posted on    October-27-2014 12:17 AM

hi
you shuld declare a variable of the class your lose variable in first, like
class LoseClass
{
   public double lose;
   //...
}
in file MainForm.cs
LoseClass loseClass = new LoseClass();
listBox.Items.Add("calories" + loseClass.lose);

Don't want to miss updates? Please click the below button!

Follow MindStick