I cannot access a variable outside a class

后端 未结 3 1517
甜味超标
甜味超标 2021-01-28 15:52

I\'m making a blackjack project..

I\'ve firstly created the Hit class for the Hit Button.

So on the form, when I click it, it will retr

相关标签:
3条回答
  • 2021-01-28 16:12

    You cannot access a variable defined inside a method. stRefID's scope is strictly inside GenerateID. Based on your class, if you want to access the value of stRefID, you need to call Hit.GenerateID, this will return the value of stRefID. Scope of variables is very important in C# (as with all programming languages). I suggest you take a look at C# Variable Scope

    0 讨论(0)
  • 2021-01-28 16:30

    Not sure if I understood completely.

    But I could see that stRefID is a variable local to GenerateID function.

    If you need to access it as Hit.stRefID then stRefID should be a static class member

    Try adding stRefID as static global Class variable.

    public class Hit
    {
        public static string stRefID = "";
    
        public static string GenerateID(int MinSize, int MaxSize)
        {
           //Your logic
        }
    
    0 讨论(0)
  • 2021-01-28 16:32

    what u have done is declared the stRefID variable inside the scope of the method GenerateID this is not gonna work so what u can do is put that variable within the scope of the class and then u can use it like this.

    namespace Blackjack
       {
        public class Hit
          {
           string stRefID = "";
    
           public static string GenerateID(int MinSize, int MaxSize)
            {
            Random random = new Random();
    
            int iChosenMaxSize = random.Next(0, 11);
    
            int two = 2;
            int three = 3;
            int four = 4;
            int five = 5;
            int six = 6;
            int seven = 7;
            int eight = 8;
            int nine = 9;
            int ten = 10;
            int jack = 10;
            int queen = 10;
            int king = 10;
            int ace = 11;
    
    
         for (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; 
    
        }
    

    this should work fine i guess

    0 讨论(0)
提交回复
热议问题