This should be simple, but somehow it is not. The messagebox displays zero's for both product quantity and product total price. Not quite sure why. Here is the code thus far:
Product Page
//will add the qty of the tshirts to the shop page. will catch non-integers
protected void linkTShirtAdd_Click(object sender, EventArgs e)
{
int intOutput = 0;
lblTShirtWarning.Visible = false;
//determine that the value is parsable - if it is, assign values. Else, display error
if (int.TryParse(txtTShirtQty.Text, out intOutput))
{
ProductClass.productName = "T-Shirt";
ProductClass.productPrice = 10;
ProductClass.productQty = Int16.Parse(txtTShirtQty.Text);
//price of tshirts
int totalTShirtPrice = ProductClass.productPrice * ProductClass.productQty;
//display summary of the order
MessageBox.Show (new Form {TopMost = true},
"ORDER REVIEW" + "\n_______________________\n"
+ ProductClass.productName + "\n"
+ "Quantity: " + ProductClass.productQty +"\n"
+ "Total Price: " + totalTShirtPrice);
//Response.Redirect("./Shop.aspx");
}
else
{
lblTShirtWarning.Visible = true;
lblTShirtWarning.Text = "Please enter a valid number";
txtTShirtQty.Text = "";
}
}
Product Class (to store the values)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ProductClass
/// </summary>
public static class ProductClass
{
//obvious variables
private static String product;
private static int price;
private static int quantity;
//get and set the name of the product
public static String productName
{
get
{
return product;
}
set
{
product = value;
}
}
//get and set the price of the product
public static int productPrice
{
get
{
return price;
}
set
{
price = value;
}
}
//get and set the quantity of the product
public static int productQty
{
get
{
return quantity;
}
set
{
price = value;
}
}
}
The getter and setter of ProductCode.productQty use different backing fields:
public static int productQty
{
get
{
return quantity;
}
set
{
price = value;
}
}
Obviously, in the setter, price = value; is wrong, you probably meant quantity = value;.
Related
I'm getting the error:
cannot implicitly convert type decimal to string
when I'm setting the value for a property and I'm not sure how to remedy this.
There are two forms. I'm passing information from one form to the other to display it. The error is in Form Price
Form (Calculator):
public partial class Calculator : Form
{
decimal dorm = 0;
decimal meal = 0;
public Calculator()
{
InitializeComponent();
}
public decimal _price
{
get { return _price; }
}
private void getPriceButton_Click(object sender, EventArgs e)
{
decimal price = 0;
getInput();
price = dorm + meal;
Price myPrice = new Price();
myPrice._priceLabel = _price;
myPrice.ShowDialog();
}
private void getInput()
{
if(allenRadioButton.Checked)
{
dorm = 1500;
}
if(pikeRadioButton.Checked)
{
dorm = 1600;
}
if(farthingRadioButton.Checked)
{
dorm = 1800;
}
if(universityRadioButton.Checked)
{
dorm = 2500;
}
if(sevenRadioButton.Checked)
{
meal = 600;
}
if(fourteenRadioButton.Checked)
{
meal = 1200;
}
if(unlimitedRadioButton.Checked)
{
meal = 1700;
}
}
Form (Price):
public partial class Price : Form
{
public Price()
{
InitializeComponent();
}
public decimal _priceLabel
{
set { priceLabel.Text = value; } // error
}
priceLabel.Text = price.ToString("c"); //this is messed up too
}
}
You cannot assign a String value to decimal. You need to convert it.
Try this:
public decimal _priceLabel
{
set { priceLabel.Text = decimal.Parse(value); } // error
}
You can use the Convert helper class defined here:
Convert
Takes a string and converts to the decimal value
For reverse try
Convert.ToString(decimalValue) Convert
So this is what the final product needs to look like.
I have to use a getter and setter which I don't really understand how to use.
Once the User hits Calc Payroll it will show up in the listbox to the right, and then when the user clicks show list, the label down below will update with just the name and the gross pay.
private void calcButton_Click(object sender, EventArgs e)
{
double rate = 0;
double hours = 0;
double withhold = 0;
if (this.nameTextBox.Text != "")
{
infoListBox.Items.Add("Name: " + this.nameTextBox.Text);
}
if (this.hoursTextBox.Text != "")
{
infoListBox.Items.Add("Hours: " + this.hoursTextBox.Text);
}
if(this.rateTextBox.Text != "")
{
infoListBox.Items.Add("Rate: " + this.rateTextBox.Text);
}
if (this.withHoldingTextBox.Text != "") ;
{
infoListBox.Items.Add("Withholding Amount: " + this.withHoldingTextBox.Text);
}
}
}
}
So basically I just printed all the information from the user to the list box
here is the new class so far
class Employees
{
//Fields
private double _hours;
private double _rate;
private double _withhold;
private string _name;
// Constructor
public Employees(double rate, double hours, double withhold, string name)
{
_hours = hours;
_rate = rate;
_withhold = withhold;
_name = name;
}
//Rate Property
public double rate
{
get { return _rate; }
set { _rate = value; }
}
public double hours
{
get { return _hours; }
set { _hours = value; }
}
public double withhold
{
get { return _withhold; }
set { _withhold = value; }
}
//get gross pay
public double grosspay
{
get { return _hours * _rate + _withhold; }
}
}
}
Try to bind your Properties (getter and setter) to the content of your controls.
Like this:
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
And in your xaml code you use:
<Label Name="Name" Content="{Binding Name}" />
you have to bind the name of your property in the content as shown above.
If you need more information about Databinding look here:
http://www.codeproject.com/Articles/140621/WPF-Tutorial-Concept-Binding
In this context, you may have a look at the MVVM-Pattern:
http://www.codeproject.com/Articles/100175/Model-View-ViewModel-MVVM-Explained
Let me know if this helps you!
I am new with C# and i have a problem. Actually it's my first year in college and in programming and i have a problem with arrays. I've made a class with 3 constructors and 1 method in Windows Form Application. The problem is that i want to store data from three textBoxes - that the user is typing- into an array of 10 using a button. and i don't know how to do it.
public class Employee
{
private int idnum;
private string flname;
private double annual;
public Employee()
{
idnum = 0;
flname = "";
annual = 0.0;
}
public Employee(int id, string fname)
{
idnum = id;
flname = fname;
annual = 0.0;
}
public Employee(int id, string fname, double ann)
{
idnum = id;
flname = fname;
annual = ann;
}
public int idNumber
{
get { return idnum; }
set { idnum = value; }
}
public string FLName
{
get { return flname; }
set { flname = value; }
}
public double Annual
{
get { return annual; }
set { annual = value; }
}
public string Message()
{
return (Convert.ToString(idnum) + " " + flname + " " + Convert.ToString(annual));
}
}
First of all you should add on this form 3 textboxe elements and name it in a next manner textBoxId, textBoxFLName, textBoxAnnual
Also you have to add a button. Let's call it btnSave
Write an event OnClick for this button. In this method we must read all data which user fill in on the form.
List<Employee> allEmployees = new List<Employee>();
private void buttonSave_Click(object sender, EventArgs e)
{
//read user input
int empId = Int32.Parse(textBoxId.Text);
string empFlName = textBoxFLName.Text;
double empAnnual = double.Parse(textBoxAnnual.Text);
// create new Employee object
Employee emp = new Employee(empId, empFlName, empAnnual);
// add new employee to container (for example array, list, etc).
// In this case I will prefer to use list, becouse it can grow dynamically
allEmployees.Add(emp);
}
And you can also rewrite your code in a little bit shortest manner:
public class Employee
{
public int IdNum { get; set; }
public string FlName { get; set; }
public double Annual { get; set; }
public Employee(int id, string flname, double annual = 0.0)
{
IdNum = id;
FlName = flname;
Annual = annual;
}
public override string ToString()
{
return (Convert.ToString(IdNum) + " " + FlName + " " + Convert.ToString(Annual));
}
}
Hi i have a list of objects and i need to find out if the id i have is already in the list. In the object class i have set the id and i just want to find out if the one that is entered in the Ui is already in use.
The Class
class Product
{
private string name = "";
private int id = 0;
private decimal Pvalue = 0.00m;
private static int lastId = 1;
public Product(string namep, decimal valuep)
{
this.name = namep;
this.id = lastId++;
this.Pvalue = valuep;
}
public override string ToString()
{
return name + " " + id + " "+ Pvalue;
}
public bool Equals(Product p)
{
return (p.id == this.id);
}
}
Me trying to work it out:
int id;
bool test = int.TryParse(textBoxId.Text, out id);
if(test)
{
if(Inv.Contains(id))
{
label2.Text = "you already have this id";
}else
{
label2.Text = "you can use this id";
}
}
If any one has a idea on why this is not working or a better way it would save my back side thank you.
Change private int id = 0; to public int Id { get; set; }. Also, change all the references from id to Id.
Add a using System.Linq to your business logic file.
Change if (Inv.Contains(id)) to if (Inv.Any(x => x.Id == id))
Okay I'm making some sort of database where I have 4 classes program.cs, athlete.cs, event.cs and venue.cs. One of the methods they want me to include is to search for the athlete, as well as displaying the event name and venue name. This has to be done with all the variables being private.
So I was wondering if there is anyway to have the event name variable in the athlete class connect to the event name in the event class.
Athlete Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Athlete
{
private string firstname, lastname, address, phonenumber;
private List<Event> eventlist = new List<Event>();
private Event athleteEvent;
public string athleteFirstname
{
get
{
return firstname;
}
set
{
firstname = value;
}
}
public string athleteLastname
{
get
{
return lastname;
}
set
{
lastname = value;
}
}
public string athleteAddress
{
get
{
return address;
}
set
{
address = value;
}
}
public string athletePhonenumber
{
get
{
return phonenumber;
}
set
{
phonenumber = value;
}
}
public Event eventA
{
get
{
return athleteEvent;
}
set
{
athleteEvent = eventlist[0].eventName;
}
}
}
Event Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Event
{
private string name, date, time, venueEvent;
private double fee;
private List<Athlete> athletes = new List<Athlete>();
public void addAthlete(Athlete a)
{
athletes.Add(a);// adds the athletes towards this class
}
public void displayAthletes()// method used for displaying athlete when requested
{
foreach (Athlete a in athletes) //Constructor
{
Console.WriteLine(a.athleteFirstname);
Console.WriteLine(a.athleteLastname);
}
}
public string eventName
{
get
{
return name;
}
set
{
name = value;
}
}
public string eventDate
{
get
{
return date;
}
set
{
date = value;
}
}
public string eventTime
{
get
{
return time;
}
set
{
time = value;
}
}
public double eventFee
{
get
{
return fee;
}
set
{
fee = value;
}
}
public string eventVenue
{
get
{
return venueEvent;
}
set
{
venueEvent = value;
}
}
}
This is my search method
#region Search Athlete
static void searchAthlete()
{
Console.WriteLine("Please enter which athlete you would like to find");
string searchChoice = Convert.ToString(Console.ReadLine());
for(int i = 0; i < athleteClass.ToArray().Length;i++) // goes through the athletes class to the end
{
if (searchChoice == athleteClass[i].athleteFirstname) // checks to see what has been entered matches any of the data in the athlete class
{
Console.WriteLine("Athlete First Name: " + athleteClass[i].athleteFirstname);
Console.WriteLine("Athlete Last Name: " + athleteClass[i].athleteLastname);
Console.WriteLine("Event Name: " + athleteClass[i].eventA);
Console.WriteLine("Venue Name: " + eventClass[i].eventVenue);
}
else
{
Console.WriteLine("No record was found");
}
//for (int e = 0; e < 2; e++)
//{
// if (athleteClass[e].eventA == eventClass[e].eventName) // checks
// {
// Console.WriteLine("Event name: " + eventClass[e].eventName);
// }
// for (int v = 0; v < 2; v++)
// {
// if (eventClass[v].eventVenue == venueClass[v].venueName)
// {
// Console.WriteLine("Venue Name: " + venueClass[v].venueName);
// }
// }
//}
}
}
#endregion
For every Athlete you have a list of Events as denoted by your code. When they add a new Athlete they should be able to add a list of Events like {event1, event2...etc} at the console. In your search you just have to call the Athlete object, Athlete.GetEventsList() and print out each event associated with this Athlete. I'll do pseudocode so you learn on your own.
Edit
First of all you need to save Events for each Athlete. So you need a Set/Get method to save incoming Events from the console for the particular Athlete. Once you have a list of Events for the Athlete then you can do this.
for each Athlete a in ListOfAthletes
{
get eventList for a
for each event in eventList
get event Name
//print out athlete Name and event Name
}