0

I would like to check a variable beforehand to see whether it is empty or null. If it is null or empty, it should take a character string defined by me. My code:

public string Ergebnis { get; set; }
public string Stufe { get; set; }
public string Niveau { get; set; }
public string Note { get; set; }
public DateTime? Datum { get; set; }

And the function:

public void EndEdit()
        {

            var newdate = Convert.ToDateTime(Datum).ToShortDateString();
            var client = new RestClient("MYDOMAIN");
            var request = new RestRequest("SOME THINGS", Method.POST)
                .AddUrlSegment("name", Name)
                .AddUrlSegment("kompetenzID", KompetenzID)
                .AddUrlSegment("ergebnis", Ergebnis)
                .AddUrlSegment("stufe", Stufe)
                .AddUrlSegment("niveau", Niveau)
                .AddUrlSegment("note", Note)
                .AddUrlSegment("datum", newdate)
                .AddUrlSegment("kuerzel", Kuerzel)
                .AddUrlSegment("background", Background)
                .AddHeader("Authorization", "Bearer " + ViewFingerPrint);

            IRestResponse restResponse = client.Execute(request);

            if (restResponse.StatusCode == HttpStatusCode.BadRequest)
            {
                MessageBox.Show("Der SuS ist nicht in der Datenbank vorhanden.");
            }
            else
            {

            }
        }

I thought I could do it with an if statement. However, my function then does not recognize the variable. My code:

if (string.IsNullOrEmpty(Ergebnis))
            {
                string Ergenisneu = "-";
            } else
            {
                string Ergebnisneu = Ergebnis;
            }

If I insert the result here:

.AddUrlSegment("ergebnis", Ergebnis)

It does not recognize the variable and says: The variable is never used. How else can I tell the variable that it has a string at null?

The same thing happens in this variant:

if (string.IsNullOrEmpty (Ergebnis))
                 string Ergebnis= "-";
2
  • 1
    You are declaring a new variable in a nested scope. Drop the string in front of the variable inside the if statement to use the already existing Ergebnis property. Commented Jul 13, 2021 at 17:01
  • 1
    Ergebnis = string.IsNullOrEmpty (Ergebnis) ? "-" : Ergebnis; Commented Jul 13, 2021 at 17:22

2 Answers 2

1

The problem is that you are redeclaring the variable Ergebnis inside the if statement, which you should not do, because it is then creating another variable with the same name in a different scope. Then what happens is you are assigning a value to that new variable, but it never gets used. Here's what you should do instead:

if (string.IsNullOrEmpty(Ergebnis))
{
    Ergebnis = "-";
}
Sign up to request clarification or add additional context in comments.

Comments

0

try this. you are declaring string inside if and else. declare above if condition and reassign value.

string Ergenisneu = string.Empty;

Ergenisneu = string.IsNullOrEmpty(Ergebnis) ? "-":Ergebnis

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.