?
Solved

Vs2005 & C#...

Posted on 2006-04-21
3
Medium Priority
?
377 Views
Last Modified: 2008-03-10
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;
        }
    }
0
Comment
Question by:Ramesh Srinivas
3 Comments
 
LVL 5

Accepted Solution

by:
pgloor earned 200 total points
ID: 16509422
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16509930
<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
 
LVL 6

Expert Comment

by:nehaya
ID: 16512585
At least  put :
 = null;
!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question