How to resolve this compilation error “Using unassigned local variable 'hak'” when using goto?

女生的网名这么多〃 提交于 2021-02-11 12:34:57

问题


I'm trying below code. It gives me a warning "Use of unassigned local variable 'hak' ". I guess I'm missing something here.

I want it to display "3 defa yanlış giriş yaptınız. Müşteri hizmetleri temsilcisiyle görüşünüz. İyi günler dileriz." when local variable "hak" equals to "0". But it always display "Hatalı Giriş Yaptınız. Lütfen tekrar deneyiniz. 2 hakkınız kalmıştır."

                   Console.Write("Seçmek istediğiniz 'Kullanıcı Adı'nı belirtiniz: ");
        string kullanici_adi = Console.ReadLine();
        FARKLISIFRE:
        Console.Write("Lütfen Şifrenizi giriniz: ");
        string sifre = Console.ReadLine();
        Console.Write("Girmiş olduğunuz şifreyi tekrar giriniz: ");
        string sifre2 = Console.ReadLine();

        int karsilastirma = String.Compare(sifre, sifre2);
        if (karsilastirma==0)
        {
            Console.WriteLine("Tebrikler! Kaydınız başarılı bir şekilde oluşturulmuştur.");
        }
        else
        {
            Console.WriteLine("Girmiş olduğunuz şifreler birbirinden farklıdır. Lütfen tekrar deneyiniz.");
            goto FARKLISIFRE;
        }

        Console.Write("Giriş yapmak için lütfen '1' seçeneğini giriniz: ");
        char komut = Convert.ToChar(Console.ReadLine());
        Console.Clear();
        if (komut=='1')
        {
            goto GIRIS;
        }

        else
        {
            Console.Write("Çıkış yaptınız. İyi günler dileriz.");
            goto END;
        }
        
        int hak = 3;

        GIRIS:
        Console.Write("Lütfen Kullanıcı Adınızı Giriniz: ");
        string kullanici_adi_giris = Console.ReadLine();
        Console.Write("Lütfen belirlemiş olduğunuz Şifrenizi giriniz: ");
        string sifre_giris = Console.ReadLine();

        int karsilastirma_k_adi = String.Compare(kullanici_adi, kullanici_adi_giris);
        int karsilastirma_sifre = String.Compare(sifre, sifre_giris);
        
        if (karsilastirma_k_adi == 0 && karsilastirma_sifre == 0)
        {
            Console.Write("Başarıyla Giriş Yaptınız. Hoşgeldiniz.");
        }
        else if (hak == 0)
        {
            Console.Write("3 defa yanlış giriş yaptınız. Müşteri hizmetleri temsilcisiyle görüşünüz. İyi günler dileriz.");
        }
        else
        {
            hak--;
            Console.WriteLine("Hatalı Giriş Yaptınız. Lütfen tekrar deneyiniz. {0} hakkınız kalmıştır.", hak);
            goto GIRIS;
        }
        
        END:
        Console.ReadKey();
    }

回答1:


You got this compile error because this line is never executed:

int hak = 3;

It is because of the GOTOs, the position of the LABELS and the test condition if...else above this line: you never pass this line.

To solve this compile error as well as the design problem which is thus generated, you need to refactor all that to remove all the GOTOs.

Indeed, when compiling, before talking about executing, the if ( komut == '1' ) ... else ..., the hak creation in never reached, going to GIRIS or END.

Thus after when you try to use hak, it never was assigned and the compiler know that and tell you that.

Just to get to compile, you need to write:

GIRIS:
int hak = 3;

But using GOTOs is trashy and not clean, a very bad smell, as well as being source of mistakes and weird behaviors.

Avoid goto more than one once per century, and use refactoring instead, please.

GOTO still considered harmful?

Spaghetti code

Example of a minimal solution using a local method

if ( komut == '1' )
{
  int hak = 3;
  GIRIS(ref hak);
}
else
{
  Console.Write("Çıkış yaptınız. İyi günler dileriz.");
}

Console.ReadKey();

void GIRIS(ref int hak)
{
  Console.Write("Lütfen Kullanıcı Adınızı Giriniz: ");
  string kullanici_adi_giris = Console.ReadLine();
  Console.Write("Lütfen belirlemiş olduğunuz Şifrenizi giriniz: ");
  string sifre_giris = Console.ReadLine();

  int karsilastirma_k_adi = String.Compare(kullanici_adi, kullanici_adi_giris);
  int karsilastirma_sifre = String.Compare(sifre, sifre_giris);

  if ( karsilastirma_k_adi == 0 && karsilastirma_sifre == 0 )
  {
    Console.Write("Başarıyla Giriş Yaptınız. Hoşgeldiniz.");
  }
  else if ( hak == 0 )
  {
    Console.Write("3 defa yanlış giriş yaptınız. Müşteri hizmetleri temsilcisiyle görüşünüz. İyi günler dileriz.");
  }
  else
  {
    hak--;
    Console.WriteLine("Hatalı Giriş Yaptınız. Lütfen tekrar deneyiniz. {0} hakkınız kalmıştır.", hak);
    GIRIS(ref hak);
  }
}



回答2:


The compiler is seeing that the line of code that will define a value for the variable hak is before the label GIRIS. It cannot be guaranteed that the line will be executed, because GIRIS could be reached by the line of code goto GIRIS.

Try changing this:

int hak = 3;
GIRIS:

to this:

GIRIS:
int hak = 3;


来源:https://stackoverflow.com/questions/65906189/how-to-resolve-this-compilation-error-using-unassigned-local-variable-hak-wh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!