using getter-setter - c#

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!

Related

How can I create a method polymorphism?

I learn programming oriented object, and I want to do this code but I got those problems, I think I don't really know the concept of virtual.
View image 1
Code:
class Joueur
{
private string _nom;
private string _prenom;
private DateTime _dateDeNaissance;
private string _position;
private bool _reserve;
public string nom
{
get { return _nom; }
set { _nom = value; }
}
public string prenom
{
get { return _prenom; }
set { _prenom = value; }
}
public DateTime dateDeNaissance
{
get { return _dateDeNaissance; }
set { _dateDeNaissance = value; }
}
public string position
{
get { return _position; }
set { _position = value; }
}
public bool reserve
{
get { return _reserve; }
set { _reserve = value; }
}
public Joueur()
{
}
public Joueur(string nom, string prenom, DateTime dateDeNaissance, string position, bool reserve)
{
this.nom = nom;
this.prenom = prenom;
this.dateDeNaissance = dateDeNaissance;
this.position = position;
this.reserve = reserve;
}
public double virtual CalculerPrime(int joues, int gagnes)
{
double Prime;
if (reserve == false)
Prime = 10000 * (gagnes / joues);
else
Prime = ((10000 * (gagnes / joues)) / 2);
return Prime;
}
}
You need to do like this in your code:
public virtual double CalculerPrime(int joues, int gagnes)
{
double Prime;
if (reserve == false)
Prime = 10000 * (gagnes / joues);
else
Prime = ((10000 * (gagnes / joues)) / 2);
return Prime;
}

Why Query returns some null values to my list Object using dapper

I have this functional query where I only used fictive tables names for security concerns :
SELECT
h.CENID,
h.BMHFMC,
h.BMHDONEMIDASSTEP1,
h.BMHDONEMIDASSTEP2,
h.LMIID,
h.BMHHOLD,
h.BMHBATCHMIDAS,
h.BMHFMCVALUEDATE AS HeaderValueDate,
h.SUNID,
h.BRAID,
d.BMHID,
d.BMDRUBRIQUE,
d.BMDCLIENT,
d.BMDSEQUENCE,
d.BMDDATE,
d.BMDDEVISE,
d.BMDMONTANT,
d.BMDTYPE,
d.BMDNOTE,
d.BMDENTRYNBRE,
v.DEVDECIMAL ,
NVL(t.TYPVERIFCOMPTEMIDAS, 0) AS TYPVERIFCOMPTEMIDAS
FROM dbo.TableOne h
INNER JOIN dbo.Tabletwoo d
ON h.BMHID = d.BMHID
INNER JOIN dbo.tableThree v
ON d.BMDDEVISE = v.DEVID
LEFT JOIN dbo.TableFour t
ON t.TYPID=h.BMHFMC
WHERE d.BMDMONTANT != 0
AND h.BMHDONEMIDASSTEP1 = 0
AND h.BMHDONEMIDASSTEP2 = 0
AND h.LMIID = 0
AND h.BMHHOLD = 0
And I made a class in order to bind every fields
public class Batch :BaseRepository ,IList<Batch>
{
public Batch()
{
}
private string cendid;
private string bmhfmc;
private double bmhdonemidasstep1;
private double bmhdonemidasstep2;
private double lmiid;
private double bmhhold;
private double bmhbatchmidas;
private DateTime headervaluedateordinal;
private double sunid; //
private string bradid; //
private double bmhid;
private string bmdrubirique; //
private string bmdclient;
private string bmdsequence;
private DateTime bmddate;
private string bmddevise;
private double bmdmontant;
private string bmdtype;
private string bmdnote;
private string bmdentrynbre; //
private double devdecimalordinal;
private double typverifcomptemidasordinal;
public Batch(string cendid, string bmhfmc, double bmhdonemidasstep1, double bmhdonemidasstep2, double lmiid, double bmhhold, double bmhbatchmidas, DateTime headervaluedateordinal, double sunid, string bradid, double bmhid, string bmdrubirique, string bmdclient, string bmdsequence, DateTime bmddate, string bmddevise, double bmdmontant, string bmdtype, string bmdnote, string bmdentrynbre, double devdecimalordinal, double typverifcomptemidasordinal)
{
this.cendid = cendid;
this.bmhfmc = bmhfmc;
this.bmhdonemidasstep1 = bmhdonemidasstep1;
this.bmhdonemidasstep2 = bmhdonemidasstep2;
this.lmiid = lmiid;
this.bmhhold = bmhhold;
this.bmhbatchmidas = bmhbatchmidas;
this.headervaluedateordinal = headervaluedateordinal;
this.sunid = sunid;
this.bradid = bradid;
this.bmhid = bmhid;
this.bmdrubirique = bmdrubirique;
this.bmdclient = bmdclient;
this.bmdsequence = bmdsequence;
this.bmddate = bmddate;
this.bmddevise = bmddevise;
this.bmdmontant = bmdmontant;
this.bmdtype = bmdtype;
this.bmdnote = bmdnote;
this.bmdentrynbre = bmdentrynbre;
this.devdecimalordinal = devdecimalordinal;
this.typverifcomptemidasordinal = typverifcomptemidasordinal;
}
public string Cendid
{
get { return cendid; }
set { cendid = value; }
}
public string Bmhfmc
{
get { return bmhfmc; }
set { bmhfmc = value; }
}
public double Bmhdonemidasstep1
{
get { return bmhdonemidasstep1; }
set { bmhdonemidasstep1 = value; }
}
public double Bmhdonemidasstep2
{
get { return bmhdonemidasstep2; }
set { bmhdonemidasstep2 = value; }
}
public double Lmiid
{
get { return lmiid; }
set { lmiid = value; }
}
public double Bmhhold
{
get { return bmhhold; }
set { bmhhold = value; }
}
public double Bmhbatchmidas
{
get { return bmhbatchmidas; }
set { bmhbatchmidas = value; }
}
public DateTime Headervaluedateordinal
{
get { return headervaluedateordinal; }
set { headervaluedateordinal = value; }
}
public double Sunid
{
get { return sunid; }
set { sunid = value; }
}
public string Bradid
{
get { return bradid; }
set { bradid = value; }
}
public double Bmhid
{
get { return bmhid; }
set { bmhid = value; }
}
public string Bmdrubirique
{
get { return bmdrubirique; }
set { bmdrubirique = value; }
}
public string Bmdclient
{
get { return bmdclient; }
set { bmdclient = value; }
}
public string Bmdsequence
{
get { return bmdsequence; }
set { bmdsequence = value; }
}
public DateTime Bmddate
{
get { return bmddate; }
set { bmddate = value; }
}
public string Bmddevise
{
get { return bmddevise; }
set { bmddevise = value; }
}
public double Bmdmontant
{
get { return bmdmontant; }
set { bmdmontant = value; }
}
public string Bmdtype
{
get { return bmdtype; }
set { bmdtype = value; }
}
public string Bmdnote
{
get { return bmdnote; }
set { bmdnote = value; }
}
public string Bmdentrynbre
{
get { return bmdentrynbre; }
set { bmdentrynbre = value; }
}
public double Devdecimalordinal
{
get { return devdecimalordinal; }
set { devdecimalordinal = value; }
}
public double Typverifcomptemidasordinal
{
get { return typverifcomptemidasordinal; }
set { typverifcomptemidasordinal = value; }
}
Now when I execute the query into a list using dapper
Connection conn = new Connection();
OracleConnection connection = conn.GetDBConnection();
myList= connection.Query<Batch>(querySql).ToList();
Now,while debugging, all fields returns the expected values .But, I noticed those fields below are null in myList not empty but really null , but the problem is they aren't null in the database
Bmdrubirique , Sunid, Bmdentrynbre, Bradid ,Cenid
In oracle database those fields are like the following :
CENID is VARCHAR2(3 BYTE)`
Bmhid is VARCHAR2(3 BYTE)
Sunid is NUMBER(38,0)
Bradid is VARCHAR2(3 BYTE)
I don't get it , where did it go wrong? why other fields are properly loaded while those returns null value ?
My default assumption would be that there is a typo in the real code and the constructor is assigning a value from a field to itself. However, frankly: since you have a public Batch() {} constructor, I'm not sure what the benefit of the second one is - it just adds risk of errors. Likewise with the fields and manual properties.
So if this as me, where you currently have (simplified to two properties):
public class Batch
{
private string cendid;
private string bmhfmc;
public Batch() {}
public Batch(string cendid, string bmhfmc)
{
this.cendid = cendid;
this.bmhfmc = bmhfmc;
}
public string Cendid
{
get { return cendid; }
set { cendid = value; }
}
public string Bmhfmc
{
get { return bmhfmc; }
set { bmhfmc = value; }
}
}
I would have literally just:
public class Batch
{
public string Cendid {get;set;}
public string Bmhfmc {get;set;}
}
All of the rest of the code is just opportunities to make coding errors.
Now: the reason that Cendid is null is because: the column is CENID - only one d. This means that dapper isn't even using your custom constructor, because it isn't a perfect match between the constructor and the columns. Ditto the other fields like BRAID vs BRADID.
So the next thing to do is to fix the typos.

Need to Add 3 Day to current Transaction Date

I have a button that needs to add 3 days to the current transaction date already in ArrayList.
How to accomplish this?
The code is as follows:
private void btnCheckCleared_Click(object sender, EventArgs e)
{
foreach (Transaction item in tranArray)
{
if (What goes here?)
{
DateTime.Today.AddDays(3).ToLongDateString();
}
}
}
If you need any more code, please let me know.
So here is my Transaction.cs code:
namespace TEXT TEXT TEXT
{
public class Transaction
{
//data hiding (blackbox)
//visible only to class itself
//fields (variables)
//4 member variables (instantiate object)
private decimal decAmount;
private DateTime dteTransactionDate;
private string strCheckNumber;
private string strPayee;
private string strTypeTrans;
public bool CheckCleared;
public decimal Amount
{
get
{
return decAmount;
}
set
{
decAmount = value;
}
}
public string CheckNumber
{
get
{
return strCheckNumber;
}
set
{
strCheckNumber = value;
}
}
public string Payee
{
get
{
return strPayee;
}
set
{
strPayee = value;
}
}
public DateTime TransactionDate
{
get
{
return dteTransactionDate;
}
set
{
dteTransactionDate = value;
}
}
public TransactionType TypeTrans;
//constructor
public Transaction(string payee, decimal amount, TransactionType typeTrans, DateTime transactionDate)
{
this.Payee = payee; //assignment operator =
this.Amount = amount; //this is to qualify
this.TypeTrans = typeTrans;
this.TransactionDate = transactionDate;
}
public Transaction(string payee, decimal amount, TransactionType typeTrans, DateTime transactionDate, string checkNumber)
{
this.Payee = payee; //assignment operator =
this.Amount = amount; //this is to qualify
this.CheckNumber = checkNumber;
this.TypeTrans = typeTrans;
this.TransactionDate = transactionDate;
}
//public Transaction ()
public override string ToString()
{
return this.TransactionDate.ToShortDateString() + " " + this.Amount.ToString("C") + "\t" + this.TypeTrans;
}
}
}
You need something like that:
private void btnCheckCleared_Click(object sender, EventArgs e)
{
foreach (Transaction item in tranArray)
{
if (/*Whatever your condition looks like*/)
{
item.TransactionDate = item.TransactionDate.AddDays(3);
}
}
}
AddDays does not modify the given DateTime but returns a new DateTime, that's why it is assigned back to item.TransactionDate

My logical is wrong somehow

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;.

ObservableCollection CollectionChanged not helpful in WPF MVVM

I am using a DataGrid and is bind using an ObservableCollection in the ViewModel
private ObservableCollection<StockItem> _stockList;
public ObservableCollection<StockItem> StockList
{
get
{
return _stockList;
}
set
{
_stockList = value;
OnPropertyChanged("StockList");
}
}
The StockItem class contains its Properties which are the Column in DataGrid.
There is a column named Amount in DataGrid whose values changed with Quantity*Price Column of the same datagrid.
I have a Property called TotalAmount in the ViewModel which is calculated in the ObservableCollection CollectionChanged event like
void OnStockListChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
this.TotalAmount = this.StockList.Sum(t => t.Amount);
}
This values is only updated in the TextBox bind to TotalAmount when a new row is added to a DataGrid with some data. I want this TextBox of TotalAmount to be updated as soon as the Amount Column in the datagrid changes.
How can i do this.
StockItem Class
public class StockItem : ObservableObject, ISequencedObject
{
JIMSEntities dbContext = new JIMSEntities();
public StockItem()
{
var qs = dbContext.Stocks.Select(s => s.StockName);
_stocks = new CollectionView(qs.ToArray());
_stocks.CurrentChanged += new EventHandler(Stocks_CurrentChanged);
}
void Stocks_CurrentChanged(object sender, EventArgs e)
{
if (_stocks.CurrentItem != null)
StockName = _stocks.CurrentItem.ToString();
var qs = (from p in dbContext.Stocks
where p.StockName.Contains(StockName)
select new { Unit = p.Unit, UnitPrice = p.UnitPrice }).SingleOrDefault();
if (qs != null)
{
Unit = qs.Unit;
UnitPrice = (decimal)qs.UnitPrice;
}
}
private CollectionView _stocks;
public CollectionView Stocks
{
get
{
return _stocks;
}
set
{
_stocks = value;
OnPropertyChanged("Stocks");
}
}
private int _sNo;
public int SNo
{
get
{
return _sNo;
}
set
{
_sNo = value;
OnPropertyChanged("SNo");
}
}
private string _stockName;
public string StockName
{
get
{
return _stockName;
}
set
{
_stockName = value;
OnPropertyChanged("StockName");
}
}
private decimal _unitPrice;
public decimal UnitPrice
{
get
{
return _unitPrice;
}
set
{
_unitPrice = value;
OnPropertyChanged("UnitPrice");
OnPropertyChanged("Amount");
}
}
private string _unit;
public string Unit
{
get
{
return _unit;
}
set
{
_unit = value;
OnPropertyChanged("Unit");
}
}
private decimal _discount;
public decimal Discount
{
get
{
return _discount;
}
set
{
_discount = value;
OnPropertyChanged("Discount");
OnPropertyChanged("Amount");
}
}
private decimal _quantity;
public decimal Quantity
{
get
{
return _quantity;
}
set
{
_quantity = value;
OnPropertyChanged("Quantity");
OnPropertyChanged("Amount");
}
}
public decimal Amount
{
get
{
decimal total = Quantity * (UnitPrice - (UnitPrice * (Discount / 100)));
return total;
}
}
public override string ToString()
{
return StockName;
}
}
so, basically, what you are seeing is due to a common mis-conception about ObservableCollection. OC does NOT notify when objects that it contains are changed. It notifies when IT changes (take a look at INotifyCollectionChanged)--when items are added, removed, etc.
what you want to do is be notified when a StockItem that is contained in the OC changes. You are going to have to do a couple of things
1) make sure INotifyPropertyChanged (which you say you are already doing) is implemented on your StockItem
2) customize or find an implementation of ObservableCollection that will notify when an item contained in the collection changes (here is one)
You need to subscribe to PropertyChanged of all the items in the collection to also recalculate the value if the Amount changes on any of the items, it's a bit messy. Someone somewhere might have written utility classes for that...

Categories

Resources