none
C# use of unassigned local variable in if-else statement

    Question

  • Hello, as you see i'm used to programming in visual basic and i'm trying to convert my vb codes to c# codes. I'm having trouble doing this if-else statements because of this variable not being initialized once its outside of the scope. 

    Here is my original working excel vb code on one part of the program:

    Function nomhdigit(w As Double)
    Dim ones As String
    Dim tens As String
    Dim teen As String
    Dim hundreds As String
    Dim initial As String
    Dim bal As Integer
    If w >= 100 Then
        hundreds = nomunit(w \ 100)
        bal = w - (w \ 100) * 100
        initial = hundreds & " Hundred "
    Else
        bal = w
    End If
    If bal >= 10 And bal < 20 Then
        teen = nomteen(bal)
        nomhdigit = initial & teen & " "
    ElseIf bal > 0 And bal < 10 Then
        ones = nomunit(bal)
        nomhdigit = initial & ones & " "
    ElseIf bal = 0 Then
        nomhdigit = initial
    Else
        tens = nomtens(bal \ 10)
        ones = nomunit(bal - (bal \ 10) * 10)
        If bal - (bal \ 10) * 10 = 0 Then
            nomhdigit = initial & tens & " "
        Else
            nomhdigit = initial & tens & " " & ones & " "
        End If
    End If
    End Function

    Then this is the C# code of the vb function

    private string nomhdigit(double w)
            {
                string ones, tens, teen, hundreds; 
                string initial;
                int bal;
                if (w >= 100)
                {
                    hundreds = nomunit((int)(w / 100));
                    bal = (int)(w - (int)(w / 100) * 100);
                    initial = hundreds + " Hundred ";
                }
                else
                {
                    bal = (int)w;
                }
                if (bal >= 10 && bal < 20)
                {
                    teen = nomteen(bal);
                    return (initial + teen + " ");
                }
                else if (bal > 0 && bal < 10)
                {
                    ones = nomunit(bal);
                    return (initial + ones + " ");
                }
                else if (bal == 0)
                {
                    return (initial);
                }
                else
                {
                    tens = nomtens((int)(bal / 10));
                    ones = nomunit(bal - (int)(bal / 10) * 10);
                    if ((bal - (int)(bal / 10) * 10) == 0 )
                        return (initial + tens + " ");
                    else
                        return (initial + tens + " " + ones + " ");
                }
            }

    I always get an error saying "use of unassigned local variable 'initial'". Please help me. I couldn't find any other codes with the same problem so i really have no idea how to fix this.


    Sunday, October 07, 2012 5:10 AM

Answers

  • Hello, In your c# code, assign empty value to string

    string initial = string.Empty;

    HTH


    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    • Marked as answer by vixenine Sunday, October 07, 2012 3:26 PM
    Sunday, October 07, 2012 7:25 AM
  • Thank you. Worked like a charm. Though I still couldn't understand why it's necessary to assign string.empty at the very start of the declaration.

    In VB, variables are always initialized to their default values.

    In C# variables are considered not initialized. If w<100 you never put anything. I wouldn't put the initialization on the variable declaration though. Put it in the 'else' branch of the if:

    private string nomhdigit(double w)
    {
    string ones, tens, teen, hundreds; 
    string initial;
    int bal;
    if (w >= 100)
    {
        hundreds = nomunit((int)(w / 100));
        bal = (int)(w - (int)(w / 100) * 100);
        initial = hundreds + " Hundred ";
    }
    else
    {
        bal = (int)w;
        initial = string.Empty;
    }

    Monday, October 08, 2012 2:31 PM

All replies

  • Hello, In your c# code, assign empty value to string

    string initial = string.Empty;

    HTH


    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    • Marked as answer by vixenine Sunday, October 07, 2012 3:26 PM
    Sunday, October 07, 2012 7:25 AM
  • Thank you. Worked like a charm. Though I still couldn't understand why it's necessary to assign string.empty at the very start of the declaration.
    Sunday, October 07, 2012 3:29 PM
  • Thank you. Worked like a charm. Though I still couldn't understand why it's necessary to assign string.empty at the very start of the declaration.

    In VB, variables are always initialized to their default values.

    In C# variables are considered not initialized. If w<100 you never put anything. I wouldn't put the initialization on the variable declaration though. Put it in the 'else' branch of the if:

    private string nomhdigit(double w)
    {
    string ones, tens, teen, hundreds; 
    string initial;
    int bal;
    if (w >= 100)
    {
        hundreds = nomunit((int)(w / 100));
        bal = (int)(w - (int)(w / 100) * 100);
        initial = hundreds + " Hundred ";
    }
    else
    {
        bal = (int)w;
        initial = string.Empty;
    }

    Monday, October 08, 2012 2:31 PM