Vs2005 & C#...

Hi,

Just trying to get to grips with C# and vs2005 - came across an error converting some code to C#.


Why do i get an error saying "Use of unassigned local variable 'EntryName'"

Clearly it is assigned!
 

protected void btn_Add_Click(object sender, EventArgs e)
    {
        string Entry = DDL_Titles.SelectedValue;
        string msg;
        string EntryName;
        int i = 0;
        foreach (ListItem li in lbx_SelectedTitles.Items)
        {
            if (li.Value == Entry)
            {
                EntryName = li.Text;
                i += 1;
            }
        }
        if (i == 0)
        {
            lbl_msg.Visible = false;
            lbx_SelectedTitles.Items.Add(new ListItem(DDL_Titles.SelectedItem.ToString(), Entry.ToString()));
        }
        else
        {
            msg = EntryName + " is already in the list.";
            lbl_msg.Visible = true;
            lbl_msg.Text = msg;
        }
    }
LVL 11
Ramesh SrinivasTechnical ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pgloorCommented:
The C# compiler does not allow the use of uninitialized variables. If the compiler detects the use of a variable that might not have been initialized, it generates CS0165.

In your code it's not guaranteed that EntryName will get a value assigned. What if lbx_SelectedTitles does not contain any items or none of the items matches the Entry string?

To solve the problem simply assign a value to the EntryName when you create it:

        string EntryName = "";

Peter
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Arthur_WoodCommented:
<DO NOT ACCEPT AS ANSWER>

In fact, it is a best practice to ALWAYS initialize EVERY variable that you declare in your code, like this:

protected void btn_Add_Click(object sender, EventArgs e)
    {
        string Entry = DDL_Titles.SelectedValue;
        string msg = "";
        string EntryName = "";
        int i = 0;
        foreach (ListItem li in lbx_SelectedTitles.Items)
        {
            if (li.Value == Entry)
            {
                EntryName = li.Text;
                i += 1;
            }
        }
        if (i == 0)
        {
            lbl_msg.Visible = false;
            lbx_SelectedTitles.Items.Add(new ListItem(DDL_Titles.SelectedItem.ToString(), Entry.ToString()));
        }
        else
        {
            msg = EntryName + " is already in the list.";
            lbl_msg.Visible = true;
            lbl_msg.Text = msg;
        }
    }

</DO NOT ACCEPT AS ANSWER>

AW
0
nehayaCommented:
At least  put :
 = null;
!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.