|
 |
aspx_beginners thread: Help with code/ local varaibles
Message #1 by "dave" <dougwood@a...> on Fri, 2 Aug 2002 20:14:44
|
|
HI, can anyone help me:
here is my code:
<script Language = "C#" runat="server">
void ClickHandler(object sender, EventArgs e)
{
if (btnSubmit.Text == "Call")
{
string name;
switch (number.SelectedItem.Value)
{
case "5555555":
name = "Doug";
break;
case "6666666":
name="Fred";
break;
case "7777777":
name = "mark";
break;
}
btnSubmit.Text="Disconnect";
message.Text = "You are calling" + name; //*ERROR*
}
else
{
message.Text = "";
btnSubmit.Text = "Connect";
number.Enabled= true;
}
}
</script>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form runat="server">
<asp:dropdownlist id="number" runat="server"/>
<asp:listitem runat="server" id="option1" Text="5555555"/>
<asp:listitem runat="server" id="option2" Text="6666666"/>
<asp:listitem runat="server" id="option" Text="7777777"/>
</asp:dropdownlist>
<BR>
<asp:button id="btnSubmit" Text="Call" runat="server"
onclick="ClickHandler"/><BR><BR>
<asp:label id="message" runat="server"/>
</form>
</body>
</html>
I keep getting this error message:
" Use of unassigned local variable 'name' "
but I HAVE assigned the local varaible 'name'!!
I don't understand.
Thanks!!
Message #2 by "Christopher Reed" <CReed@m...> on Fri, 02 Aug 2002 14:56:59 -0500
|
|
I believe the problem is with the placement of your assignment for name.
Since you placed inside the if block, it is only scoped within the if
block. Try this:
<script Language = "C#" runat="server">
void ClickHandler(object sender, EventArgs e)
{
string name;
if (btnSubmit.Text == "Call")
{
switch (number.SelectedItem.Value)
etc., etc., etc.
Hope this helps!
Chris
Christopher Reed
Application Analyst
Web Development Coordinator
Information Technology
City of Lubbock
creed@m...
"The oxen are slow, but the earth is patient."
>>> dougwood@a... 8:14:44 PM 8/2/2002 >>>
HI, can anyone help me:
here is my code:
<script Language = "C#" runat="server">
void ClickHandler(object sender, EventArgs e)
{
if (btnSubmit.Text == "Call")
{
string name;
switch (number.SelectedItem.Value)
{
case "5555555":
name = "Doug";
break;
case "6666666":
name="Fred";
break;
case "7777777":
name = "mark";
break;
}
btnSubmit.Text="Disconnect";
message.Text = "You are calling" + name; //*ERROR*
}
else
{
message.Text = "";
btnSubmit.Text = "Connect";
number.Enabled= true;
}
}
</script>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form runat="server">
<asp:dropdownlist id="number" runat="server"/>
<asp:listitem runat="server" id="option1" Text="5555555"/>
<asp:listitem runat="server" id="option2" Text="6666666"/>
<asp:listitem runat="server" id="option" Text="7777777"/>
</asp:dropdownlist>
<BR>
<asp:button id="btnSubmit" Text="Call" runat="server"
onclick="ClickHandler"/><BR><BR>
<asp:label id="message" runat="server"/>
</form>
</body>
</html>
I keep getting this error message:
" Use of unassigned local variable 'name' "
but I HAVE assigned the local varaible 'name'!!
I don't understand.
Thanks!!
Message #3 by "dave" <dougwood@a...> on Sat, 3 Aug 2002 00:19:33
|
|
Thanks for the reply Chris,
well it turned out that the 'name' declaration was in the right place, but
because I didn't use a 'default:' in my 'switch' statement, i guess there
was no declaration for name.
Once I put in:
default:
name="<unknown>";
break;
in my switch statement, it worked!
bizarre!
can anyone exlain why that is?
Message #4 by "Mrinal Srivastava" <mrinal.srivastava@p...> on Mon, 5 Aug 2002 15:08:01 +0530
|
|
Well, It's not so bizzare afterall. The C# code within script tag doesn't
have to know all the possible options for number.SelectedItem.Value.
Although there are only three possible values and you are checking against
all of them in your switch statement......... Only YOU, the developer knows
this. The switch statement doesn't have to know where the values are coming
from (why should it know that it's always comparing against a value coming
from a list box).
For the situation where number.SelectedItem.Value has a value other than
those three, "name" shall be an uninitialzed value and you'll be attempting
to use it while assigning to message.Text.
It's always a good safety net to write a default case, and thorow
anexception/output some message in cases where the control shouldn't have
come to 'default'.
-----Original Message-----
From: dave [mailto:dougwood@a...]
Sent: Saturday, August 03, 2002 12:20 AM
To: aspx_beginners
Subject: [aspx_beginners] Re: Help with code/ local varaibles
Thanks for the reply Chris,
well it turned out that the 'name' declaration was in the right place, but
because I didn't use a 'default:' in my 'switch' statement, i guess there
was no declaration for name.
Once I put in:
default:
name="<unknown>";
break;
in my switch statement, it worked!
bizarre!
can anyone exlain why that is?
|
|
 |