 |
| ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 4 General Discussion 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
|
|
|
|

December 31st, 2010, 08:40 AM
|
|
Registered User
|
|
Join Date: Sep 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i need Help with datatype conversion
am relative new into C# programming and am just setting my foot on OOP and am hiting wall continually with Visual studio.
i need help with the code below:
class file
Public Class Employee
{
private decimal _wages;
}
Public void SetWages( decimal a)
{
_wages = a;
}
Public decimal GetWages()
{
return _wages;
}
in the Program files
{
Employee person = new Employee()
person.SetWages = decimal.Parse(wagesTextBox.Text);
}
when i debugged, i will recieve a message that " string not in the correct formant" person.SetWages = decimal.Parse(wagesTextBox.Text);
i think maybe i am having problem with type conversion from decimal to string, i don't know
please i need help to correct this.
when i run the build solution, there will be no error but when i debug there will be error.
|
|

January 1st, 2011, 12:01 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Is this the real code you're using? I doubt it, so can you please post your exact code? Otherwise, it's a bit difficult to debug. And can you post the exact error message?
Also, I doubt this works:
Code:
person.SetWages = decimal.Parse(wagesTextBox.Text);
SetWages is a method and should be called like this:
Code:
person.SetWages(wagesTextBox.Text);
A few other notes:
1. Why are you using Set and Get methods? Just as an exercise? Otherwise, properties are more suitable for this task
2. A yellow font on a light grey background is not very easy to read...
3. What do you enter in the wagesTextBox control?
Cheers,
Imar
|
|

January 13th, 2011, 09:40 PM
|
|
Registered User
|
|
Join Date: Sep 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Here is full programe Mr. Imar
namespace EmployeeData
{
public class Employee
{
public string FirstName { get; set; }
public decimal HourlyRate { get; set; }
public short Hours { get; set; }
public short Id { get; set; }
public string Lastname { get; set; }
public decimal Wages { get; set; }
public void GrossWages()
{
Wages = Hours * HourlyRate;
}
using System;
using System.Windows.Forms;
namespace EmployeeData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addEmployeeButton_Click(object sender, EventArgs e)
{
var person = new Employee();
person.Id = Convert.ToInt16(iDTextBox.Text);
person.FirstName = firstNameTextBox.Text;
person.Lastname = lastNameTextBox.Text;
person.Hours = Convert.ToInt16(hoursTextBox.Text);
person.HourlyRate = Convert.ToDecimal(hourlyRateTextBox.Text);
// (Input string was not in a correct format) for this line when i debug.[/COLOR]
MessageBox.Show(String.Format("No: {0} {1} {2}worked{3}hours", person.Id, person.FirstName, person.Lastname, person.Hours));
}
}
}
your reply will be greatly appreciated.
your sincerly
Macdonald
|
|

January 14th, 2011, 03:34 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
You haven't responded to some of the other questions I asked, so I can't help much until you do...
My guess is that what you enter in the TextBox cannot be converted to a decimal. Could be a regional settings thing, but it's only a wild guess.
Cheers,
Imar
|
|

January 20th, 2011, 12:06 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
|
|
Quote:
Originally Posted by Macs313
namespace EmployeeData
{
public class Employee
{
public string FirstName { get; set; }
public decimal HourlyRate { get; set; }
}
}
using System;
using System.Windows.Forms;
namespace EmployeeData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addEmployeeButton_Click(object sender, EventArgs e)
{
var person = new Employee();
person.Id = Convert.ToInt16(iDTextBox.Text);
person.FirstName = firstNameTextBox.Text;
person.Lastname = lastNameTextBox.Text;
person.Hours = Convert.ToInt16(hoursTextBox.Text);
person.HourlyRate = Convert.ToDecimal(hourlyRateTextBox.Text);
// (Input string was not in a correct format) for this line when i debug.[/color]
MessageBox.Show(String.Format("No: {0} {1} {2}worked{3}hours", person.Id, person.FirstName, person.Lastname, person.Hours));
}
}
|
We can at least offer a couple comments at this point. One, you can see how this code sample makes a lot more sense than the first one you posted. Your HourlyRate is a typical property so it makes sense to assign it a value in your runtime code, rather than calling a function and passing the text in as a parameter.
Two, you're using namespaces which is an excellent practice especially as you look at maintaining websites as they get a little bigger. Using namespaces organizes your classes simply and functionally (.cs files go in the App_Code folder and I create folders inside that mirror my namespace conventions). It's a good practice to get into.
Three, it looks like you're writing your C# into the FileName.aspx? I would recommend writing your C# into a code behind (FileName.aspx.cs), VS and VWD express will do this for you automatically if you check the box that says Place code in a separate file.Long term you definitely want the convenience and organization of having the C# in code behind and there are a lot of subtle differences to programming in the .cs file vs. using a <script runat="server"> tag in the .aspx page. I think it's a lot easier to learn C# if you just start from the code behind (unless you're coming from an ASP / PHP background in which case I'd recommend using the code behind, but I can see how you might feel more comfortable writing code inline like you're used to).
__________________
-------------------------
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.
|
|

January 20th, 2011, 02:17 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
Three, it looks like you're writing your C# into the FileName.aspx? I would recommend writing your C# into a code behind (FileName.aspx.cs),
|
I think it's a Windows Forms application with a post in the wrong forum category.... ;-)
Imar
|
|

January 22nd, 2011, 03:53 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
|
|
Quote:
Originally Posted by Imar
I think it's a Windows Forms application with a post in the wrong forum category.... ;-)
Imar
|
:O That makes so much more sense out of...
Quote:
Originally Posted by Macs313
public Form1()
{
InitializeComponent();
}
|
I had meant to ask what he was using it for and it was vaguely familiar (I did some winforms classes a few years back), but couldn't place it at the time.
Thanks for the refresh, Imar. :)
__________________
-------------------------
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.
|
|
 |