c# - Why do the references for my Form1 object and my instantiated class Guess not exist in the current context? -


i'm new windows forms , i'm having trouble making program work form1 reference 'form' spits out error: "the name 'form' not exists in current context" every use of , reference variable 'guess' instantiates guess class giving me error: "the type or namespace name 'guess' not found (are missing using directive or assembly reference?)" every use. i'm sure i'm making simple mistake can't figure out. in advance.

guess class

using system; namespace guessinggame {   public class guess {     private int guess;     private int guesscount;     private random r = new random();     private int number;      public int guess { get; set; }     public int guesscount { get; set; }     public int number { get; set; }      public guess()     {         this.guesscount = 0;         number = r.next(0, 101);     }      public guess(int num)     {         this.guess = num;         this.guesscount++;         number = r.next(0, 101);     }      public void addnumber(int num)     {         this.guess = num;         this.guesscount++;     }      public bool ishigher()     {         if (guess > number)             return true;         else         {             return false;         }     }      public bool isequal()     {         if (guess == number)             return true;         else             return false;     }      public void reset()     {         number = r.next(0, 101);         guess = 0;         guesscount = 0;     }  } } 

form1.cs:

// form1.cs  using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;  namespace guessinggame { public partial class form1 : form {     public form1()     {         initializecomponent();     }      private void button2_click(object sender, eventargs e)     {         guess.reset();         txtbxenternumber.text = "";         txtbxguesscount = guess.guesscount;         form.backcolor = system.drawing.color.gray;     }      private void button3_click(object sender, eventargs e)     {      }      private void textbox2_textchanged(object sender, eventargs e)     {      }      private void btnguess_mouseclick(object sender, mouseeventargs e)     {         string invalue;         int number;         while (int.tryparse(txtbxenternumber.text, out number) == false)         {             messagebox.show("enter integer only");             txtbxenternumber.text = "";             txtbxenternumber.focus();         }          guess guess = new guess(number);         txtbxguesscount.text = guess.guesscount;         if (guess.isequal())         {             form.backcolor = system.drawing.color.red;             btnreset.focus();         }         else if (guess.ishigher())         {             form.backcolor = system.drawing.color.blue;             txtbxenternumber.text = "";             txtbxenternumber.focus();         }         else         {             form.backcolor = system.drawing.color.lime;             txtbxenternumber.text = "";             txtbxenternumber.focus();         }     } } } 

and program.cs, reason not automatically created when made windows forms project created myself. perhaps contributing problem.

using system; namespace guessinggame { static class program {     static void main()     {         application.enablevisualstyles();         application.setcompatibletextrenderingdefault(false);         form1 form = new form1();         application.run(form);          guess guess = new guess();     } } } 

it's because created them in context of main() method. classes totally different context. within form1 method, "this" refers form1 instance you're in. if want form1 able use "guess" instance, must pass in , store somehow. try this:

in main:

guess guess = new guess(); form1 form = new form1(guess); 

in form1 class:

private guess guess;  public form1(guess guess) {     initializecomponent();      this.guess = guess; }  private void button2_click(object sender, eventargs e) {     this.guess.reset();     this.backcolor = system.drawing.color.gray; } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -