use of an unassigned variable 'format', "attr1", "attr2", "attr3"

All we need is an easy explanation of the problem, so here it is.

How can we pass the values from the return of other method?
I wanted to get the values of format, attr1, attr2, attr3 from the GetFormat() method. But somehow I am unable to get it. What is it that I am missing?
However I did initialize them, but it didn’t work.

public static bool GetFormat()
{
    string format, attr1, attr2, attr3 = string.Empty;
    try
    {
        string globalFormat = GetGlobalConfigStringValue(GLOBAL_CONFIG_ADVANCED_FULL_NAME);

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(globalFormat.ToString());
        XmlElement root = doc.DocumentElement;
        XmlNodeList nodes = root.SelectNodes("//DisplayName");
        format = nodes[0].SelectSingleNode("Format").InnerText.ToString();
        attr1 = nodes[0].SelectSingleNode("Atrribute1").InnerText.ToString();
        attr2 = nodes[0].SelectSingleNode("Atrribute2").InnerText.ToString();
        attr3 = nodes[0].SelectSingleNode("Atrribute3").InnerText.ToString();
        if (string.IsNullOrWhiteSpace(globalFormat) || string.IsNullOrWhiteSpace(attr1) || string.IsNullOrWhiteSpace(attr2))
        {
            return false;
        }
    }
    catch (Exception)
    {
        return false;
    }

    return true;
}
 
public static string GetProfileDisplayName(string profileUID)
{
    string format, attr1, attr2, attr3 = string.Empty;
    if (GetFormat())
    {
        using (var context = GetAccessEntitiesContext())
        {
            var user = context.vw_Profile.Where(x => x.ProfileUID.Equals(profileUID)).FirstOrDefault();

            return string.Format(format, user.GetType().GetProperty(attr1).GetValue(user), user.GetType().GetProperty(attr2).GetValue(user), user.GetType().GetProperty(attr3).GetValue(user));
        }
    }
    else
    {
        format = "{0} {1}";
        attr1 = "FirstName";
        attr2 = "LastName";
        using (var context = GetAccessEntitiesContext())
        {
            var user = context.vw_Profile.Where(x => x.ProfileUID.Equals(profileUID)).FirstOrDefault();

            return string.Format(format, user.GetType().GetProperty(attr1).GetValue(user), user.GetType().GetProperty(attr2).GetValue(user));
        }
    }
}

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

There are multiple ways to acheive this, you have to refactor your GetFormat method.

Way 1) Make a DTO and return that DTO from your method –

public static GetFormatModel GetFormat()
{
    GetFormatModel model = new GetFormatModel();
    try
    {
        string globalFormat = GetGlobalConfigStringValue(GLOBAL_CONFIG_ADVANCED_FULL_NAME);

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(globalFormat.ToString());
        XmlElement root = doc.DocumentElement;
        XmlNodeList nodes = root.SelectNodes("//DisplayName");
        model.format = nodes[0].SelectSingleNode("Format").InnerText.ToString();
        model.attr1 = nodes[0].SelectSingleNode("Atrribute1").InnerText.ToString();
        model.attr2 = nodes[0].SelectSingleNode("Atrribute2").InnerText.ToString();
        model.attr3 = nodes[0].SelectSingleNode("Atrribute3").InnerText.ToString();
        
        if (string.IsNullOrWhiteSpace(globalFormat)
        || string.IsNullOrWhiteSpace(model.attr1) 
        || string.IsNullOrWhiteSpace(model.attr2))
        {
            model.isSuccess = false;
            return model;
        }
    }
    catch (Exception)
    {
        model.isSuccess = false;
        return model;
    }

    model.isSuccess = true;
    return model;
}

// DTO
public class GetFormatModel
{
    public bool isSuccess { get; set; }
    public string format { get; set; } = string.Empty;
    public string attr1 { get; set; } = string.Empty;
    public string attr2 { get; set; } = string.Empty;
    public string attr3 { get; set; } = string.Empty;
}

Way 2) Return a generic Tuple

public static Tuple<bool, string, string, string, string> GetFormat()
{
    Tuple<bool, string, string, string, string> tplGetFormat = default(Tuple<bool, string, string, string, string>);
    try
    {
        string globalFormat = GetGlobalConfigStringValue(GLOBAL_CONFIG_ADVANCED_FULL_NAME);

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(globalFormat.ToString());
        XmlElement root = doc.DocumentElement;
        XmlNodeList nodes = root.SelectNodes("//DisplayName");
        tplGetFormat.Item2 = nodes[0].SelectSingleNode("Format").InnerText.ToString();
        tplGetFormat.Item3 = nodes[0].SelectSingleNode("Atrribute1").InnerText.ToString();
        tplGetFormat.Item4 = nodes[0].SelectSingleNode("Atrribute2").InnerText.ToString();
        tplGetFormat.Item5 = nodes[0].SelectSingleNode("Atrribute3").InnerText.ToString();
        
        if (string.IsNullOrWhiteSpace(globalFormat)
        || string.IsNullOrWhiteSpace(tplGetFormat.Item3) 
        || string.IsNullOrWhiteSpace(tplGetFormat.Item4))
        {
            tplGetFormat.Item1 = false;
            return tplGetFormat;
        }
    }
    catch (Exception)
    {
        tplGetFormat.Item1 = false;
        return tplGetFormat;
    }

    tplGetFormat.Item1 = true;
    return tplGetFormat;
}

Way 3) Return out parameters –

public static bool GetFormat(
out string format, 
out string attr1, 
out string attr2, 
out string attr3
)
{
    format = attr1 = attr2 = attr3 = string.Empty;
    try
    {
        string globalFormat = GetGlobalConfigStringValue(GLOBAL_CONFIG_ADVANCED_FULL_NAME);

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(globalFormat.ToString());
        XmlElement root = doc.DocumentElement;
        XmlNodeList nodes = root.SelectNodes("//DisplayName");
        format = nodes[0].SelectSingleNode("Format").InnerText.ToString();
        attr1 = nodes[0].SelectSingleNode("Atrribute1").InnerText.ToString();
        attr2 = nodes[0].SelectSingleNode("Atrribute2").InnerText.ToString();
        attr3 = nodes[0].SelectSingleNode("Atrribute3").InnerText.ToString();
        if (string.IsNullOrWhiteSpace(globalFormat) || string.IsNullOrWhiteSpace(attr1) || string.IsNullOrWhiteSpace(attr2))
        {
            return false;
        }
    }
    catch (Exception)
    {
        return false;
    }

    return true;
}

// Usage
string format = attr1 = attr2 = attr3 = string.Empty;
GetFormat(out format, out attr1, out attr2, out attr3);

Method 2

If you want to ‘return’ multiple values from a method in C#, there’s few ways to do it. One of the simplest is to use multiple out parameters. That is, the parameters of the method are declared with the keyword out, and you call the method by passing parameters from outside with the same keyword. Then, inside the method when you assign a value to it, that value can be referenced from outside.

public static void GetValues(out int a, out int b, out int c)
{
    a = 1;
    b = 2;
    c = 3;
}

Call it like:

GetValues(out int valA, out int valB, out int valC);

And print:

Console.WriteLine("a: {0}", valA);
Console.WriteLine("b: {0}", valB);
Console.WriteLine("c: {0}", valC);

Output:

a: 1
b: 2
c: 3

Another idea is to define a class that will contain all the values you want to return as properties, and inside the method create an object of it and return it as a normal return value.

public class GetValueClass
{
    public int A { get; set; }
    public int B { get; set; }
    public int C { get; set; }
}

public static GetValueClass GetValues()
{
    var values = new GetValueClass() { A = 11, B = 22, C = 33 };
    return values;
}

Call and print values:

GetValueClass values = GetValues();
Console.WriteLine("A: {0}", values.A);
Console.WriteLine("B: {0}", values.B);
Console.WriteLine("C: {0}", values.C);

Output:

A: 11
B: 22
C: 33

NOTE

In this line of code:

string format, attr1, attr2, attr3 = string.Empty;

I think what you expects it to do is assign string.Empty to all 4 string variables. But unlike some other programming languages C# doesn’t do it that way; it only assigns string.Empty to attr3, and the others will be null.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply