 |
BOOK: Beginning ASP.NET 4 : in C# and VB
 | This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

September 8th, 2010, 12:32 PM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Wrong use of int
Hi, I decided to go back over beg asp.net before going onto pro, and I ran into a wall when I got to the caluclator in class files. Chaper 5. My calculator is in its class file all fine and dandy with Add and Stubtract methods. When I use it in code behind I get the error:
Use of unassigned local variable 'name'
Here is the code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Person myPerson = new Person();
if(Label2.Text.Length > 0 && Label3.Text.Length > 0)
{
int a = Convert.ToInt32(Label2.Text);
int b = Convert.ToInt32(Label3.Text);
int myResult;
switch(DDL1.SelectedValue)
{
case "+":
myResult = myPerson.Add(a, b);
break;
case "-":
myResult = myPerson.Subtract(a, b);
break;
}
Label1.Text = myResult.ToString();
}
}
}
Hope you can help. Thanks . ryan
|
|

September 8th, 2010, 12:51 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
|
|
It literally thinks you're trying to use a variable "name" without assigning a value to it. Since that doesn't appear in this code, it must be an issue in the code which this depends on. If custom errors are off (I recommend using them in production to throw generic errors, but even there you can turn them off in your web.config for a short amount of time to do testing. That will give you the file and line number where "it" thinks the problem is, and it's usually simple to track things down from there. If you can get us that snippet, we should be able to help. I usually start just by copying the error, stack trace, and file as a block for these kinds of problems when I ask for help.
__________________
-------------------------
Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe
When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper
Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
|
|

September 8th, 2010, 01:54 PM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Hi, sorry
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Class1
/// </summary>
public class Person {
public int Add(int a, int b)
{
return a + b;
}
public int Subtract(int a, int b)
{
return a - b;
}
}
Thanks again!
|
|

September 9th, 2010, 03:15 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi ryanburnett,
In the second code snippet you posted, I still see no variable called name.
Maybe the error comes from somewhere else? Where do you get this error exactly? Do you see it in Visual Studio's Error List? If so, double-click the error to go to the file / line where the error occurs.
Cheers,
Imar
|
|

September 9th, 2010, 01:58 PM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Calculator
Hey, sorry for slow reply I was at school!  I have re-coded the calculator now and here is what I have:
~/Default.aspx
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body { font-family: Arial; font-size: 12px; color: #000000; }
.error { background: #8C1D2D; display: block; padding: 3px 5px 3px 5px; color: #FFFFFF; border: 1px solid #C2B5C7; }
.output { font-size: 34px; font-family: Calibri; }
</style>
</head>
<body>
<form id="form1" runat="server">
<!-- Calculator -->
<!-- Gather User Input -->
<div>
<asp:TextBox ID="IntOne" runat="server" />
<asp:DropDownList ID="Operator" AutoPostBack="true" runat="server">
<asp:ListItem>+</asp:ListItem>
<asp:ListItem>-</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="IntTwo" runat="server" />
<!-- End of Gather User Input -->
<!-- Execute User Input -->
<asp:Button ID="Execute" Text="Calculate" OnClick="Execute_Click" runat="server" />
</div>
<!-- End of Execute User Input -->
<!-- Output Result -->
<div>
<asp:Label ID="Output" CssClass="output" runat="server" />
<asp:Label ID="OutputError" Visible="false" Text="Error! Please enter two valid intergers!" CssClass="error" runat="server" />
</div>
<!--- End of Output Result -->
<!-- End of Calculator -->
</form>
</body>
</html>
~/Default.aspx.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Execute_Click(object sender, EventArgs e)
{
if (IntOne.Text.Length > 0 && IntTwo.Text.Length > 0) // execute code if the text boxes are not empty
{
int valueOne = Convert.ToInt32(IntOne.Text); // interger to hold the value of the first text box
int valueTwo = Convert.ToInt32(IntTwo.Text); // interger to hold the value of the second text box
int outputResult = 0; // interger to hold the value of the result
Calculator myCalculator = new Calculator();
switch (Operator.SelectedValue)
{
case "+":
outputResult = myCalculator.Add(valueOne, valueTwo);
break;
case "-":
outputResult = myCalculator.Add(valueOne, valueTwo);
break;
}
Output.Text = outputResult.ToString(); // show output in output textbox
}
else
{
OutputError.Visible = true; // if nothing is entered show error box
Output.Visible = false; // and hide the output box
}
}
}
~/App_Code/Calculator.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Provide with two intergers and it will provide basic calculatons on them.
/// </summary>
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public int Subtract(int a, int b)
{
return a - b;
}
public Calculator()
{
//
// TODO: Add constructor logic here
//
}
}
web.config
Code:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
</configuration>
I did it in a blank site so thats all thats in it!
Sorry, after all of that it works... YAY! I belive the problem was me being a dumb ass and not initializing the interger (32) that stored the answer with 0. So its all fine and dandy now! I'm just going to make a fake youtube to test my skills and see if I'm able to progress onto ASP.net pro in C# and VB.net.
Is it me or is the VB.net syntax stupid? Also what host do you use?
Thanks again guys and the brilliant Imar!
|
|

September 10th, 2010, 04:00 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
YAY! I belive the problem was me being a dumb ass and not initializing the interger (32) that stored the answer with 0.
|
That certainly makes sense. Since it's possible that the switch block doesn't assign a valid value (e.g. you pass none of the supported operators), the result never gets a value. Assigning a default value fixes the warning. I think you out us in the wrong direction by saying that "name" was not given a value.
Quote:
|
Is it me or is the VB.net syntax stupid?
|
It's a matter of personal preference. It's not stupid, it's just slightly different. Knowing how to code in both languages is always very useful.
Quote:
|
Also what host do you use?
|
I host all of my sites myself.
Quote:
|
Thanks again guys and the brilliant Imar!
|
Thank you and you're welcome....
Imar
|
|
 |
|