c# - I cannot access a variable outside a class -


i'm making blackjack project..

i've firstly created hit class hit button.

so on form, when click it, retrieve value switch case scenario has randomized number/value within hit class.

i've tried making variable under class hit = strefid within generateid method failed well; public, i'm baffled.

thank time & assisting me this, it's first time posting here, use place time.

hit.cs:

using system; using system.collections.generic; using system.linq; using system.text;  namespace blackjack { public class hit {     public static string generateid(int minsize, int maxsize)     {          string strefid = "";          random random = new random();              int ichosenmaxsize = random.next(0, 11);              int 2 = 2;             int 3 = 3;             int 4 = 4;             int 5 = 5;             int 6 = 6;             int 7 = 7;             int 8 = 8;             int 9 = 9;             int ten = 10;             int jack = 10;             int queen = 10;             int king = 10;             int ace = 11;            (int x = 1; x <= ichosenmaxsize; x++)         {             int ichartype = random.next(0, 12);             switch (ichartype)             {                     case 0:                         strefid += two;                         break;                     case 1:                         strefid += three;                         break;                     case 2:                         strefid += four;                         break;                     case 3:                         strefid += five;                         break;                     case 4:                         strefid += six;                         break;                     case 5:                         strefid += seven;                         break;                     case 6:                         strefid += eight;                         break;                     case 7:                         strefid += nine;                         break;                     case 8:                         strefid += ten;                         break;                     case 9:                         strefid += ace;                         break;                     case 10:                         strefid += jack;                         break;                     case 11:                         strefid += queen;                         break;                     case 12:                         strefid += king;                         break;                  }              } return strefid;           }     } } 

and retrieve , set text box value retrieved:

using system; using system.collections; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms;  namespace blackjack { public partial class form1 : form {           private void hit_click(object sender, eventargs e)     {           this.test.text = hit.strefid;      } 

i've attempted making button public failed...

test text box output:

private void test_textchanged(object sender, eventargs e)     {      } 

kind regards, mike.

revised code:

namespace blackjack { public class hit {      public static string generateid(string strefid)      {          random random = new random();              int ichosenmaxsize = random.next(0, 11);              int 2 = 2;             int 3 = 3;             int 4 = 4;             int 5 = 5;             int 6 = 6;             int 7 = 7;             int 8 = 8;             int 9 = 9;             int ten = 10;             int jack = 10;             int queen = 10;             int king = 10;             int ace = 11;            (int x = 1; x <= ichosenmaxsize; x++)         {             int ichartype = random.next(0, 12);             switch (ichartype)             {                     case 0:                         strefid += two;                         break;                     case 1:                         strefid += three;                         break;                     case 2:                         strefid += four;                         break;                     case 3:                         strefid += five;                         break;                     case 4:                         strefid += six;                         break;                     case 5:                         strefid += seven;                         break;                     case 6:                         strefid += eight;                         break;                     case 7:                         strefid += nine;                         break;                     case 8:                         strefid += ten;                         break;                     case 9:                         strefid += ace;                         break;                     case 10:                         strefid += jack;                         break;                     case 11:                         strefid += queen;                         break;                     case 12:                         strefid += king;                         break;                  }           } return strefid;           }  } } 

i made generateid method strefid because return should go - whereby can call value button? public static string declaration works if have defaul value...

not sure if understood completely.

but see strefid variable local generateid function.

if need access hit.strefid strefid should static class member

try adding strefid static global class variable.

public class hit {     public static string strefid = "";      public static string generateid(int minsize, int maxsize)     {        //your logic     } 

Comments