Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
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
 
Old December 31st, 2010, 08:40 AM
Registered User
 
Join Date: Sep 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Red face 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.
 
Old January 1st, 2011, 12:01 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 13th, 2011, 09:40 PM
Registered User
 
Join Date: Sep 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old January 14th, 2011, 03:34 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 20th, 2011, 12:06 PM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

Quote:
Originally Posted by Macs313 View Post
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.
 
Old January 20th, 2011, 02:17 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 22nd, 2011, 03:53 PM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

Quote:
Originally Posted by Imar View Post
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 View Post
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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
datatype umeshtheone SQL Server 2000 2 May 16th, 2007 11:56 AM
DataType Mismatch webgen ASP.NET 1.0 and 1.1 Professional 3 November 17th, 2005 12:35 PM
DataType Conversion using WHERE IN ( ) Colonel Angus SQL Language 3 August 18th, 2004 07:43 AM
How Could I use C APIs with different DataType?? 6cet6 VS.NET 2002/2003 0 March 24th, 2004 09:20 PM
datatype ngang SQL Language 1 November 18th, 2003 09:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.