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
Related
Im trying to create a method to update an input string with another input string. for example you set accountHoldername to james then you enter another acountHolderNAme matty. the update accountHolderName method should return back matty
public class BankAccount
{
private String accountHolderName;
public BankAccount ( double balance, String accountHolderName, long accountNumber)
{
this.accountHolderName = accountHolderName;
}
public long AccountNumebr
{
get
{
return accountNumber;
}
set
{
this.accountNumber = value;
}
}
public String AccountHolderName
{
get
{
return accountHolderName;
}
set
{
this.accountHolderName = value;
}
}
public double Balance
{
get
{
return balance;
}
set
{
this.balance = value;
}
}
public String UpdateAccountHolderName()
{
}
}
If you really want that method, use private setters for your property:
public class BankAccount
{
public string AccountHolderName { get; private set; }
//Note there's really no need for a return value here
public void UpdateAccountHolderName(string newAccountHolderName)
{
AccountHolderName = newAccountHolderName;
}
}
Granted, this is functionally equivalent to:
public class BankAccount
{
public string AccountHolderName { get; set; }
}
An update method makes more sense here if you're updating multiple properties with it.
Edit
You can also do this if you really want:
public class BankAccount
{
public string AccountHolderName { get; private set; }
public string UpdateAccountHolderName(string newAccountHolderName)
{
AccountHolderName = newAccountHolderName;
return AccountHolderName;
}
}
Assuming you meant to have it so that you couldn't set the various properties on the Bank Account publicly then it would look something like this:
public class BankAccount
{
private string accountHolderName;
private long accountNumber;
private double balance;
public BankAccount (double balance, String accountHolderName, long accountNumber)
{
this.accountHolderName = accountHolderName;
this.accountNumber = accountNumber;
this.balance = balance;
}
public long AccountNumber
{
get
{
return accountNumber;
}
}
public string AccountHolderName
{
get
{
return accountHolderName;
}
}
public double Balance
{
get
{
return balance;
}
}
public string UpdateAccountHolderName(string accountHoldername)
{
this.accountHolderName = accountHolderName;
return AccountHolderName;
}
}
You can test this with a console app like this:
class Program
{
static void Main(string[] args)
{
var ba = new BankAccount(10.00, "Bob", 123456);
ReadDetails(ba);
var newName = ba.UpdateAccountHolderName("Frank");
Console.WriteLine("New Name: " + newName);
ReadDetails(ba);
}
static void ReadDetails(BankAccount ba)
{
Console.WriteLine("Balance: " + ba.Balance + ", Name: " + ba.AccountHolderName + ", Number: " + ba.AccountNumber);
Console.ReadLine();
}
}
Results:
It's worth noting that monetary amounts are normally done using decimal, so you might want to reconsider using double for the balance.
UPDATE
Now updated to return the new name as part of the method
What Im trying to achieve is a Budget Calculator. I have am using SQLite so all my database work needed to be in string format. below is my Save routine to show you guys...
void AddToBudget_Clicked(object sender, System.EventArgs e)
{
Budget Budget = new Budget()
{
OutgoingProductName = OProductName.Text,
OutgoingMonthlyCostS = OMonthlyCost.Text
};
using (SQLiteConnection conn = new SQLiteConnection(App.FilePath))
{
conn.CreateTable<Budget>();
int rowsAdded = conn.Insert(Budget);
}
DisplayAlert("Added!", "Your Monthly Item has been Added to Budget!", "OK");
BindingContext = new Budget();
OnAppearing();
}
I want all OutgoingMonthlyCostS items to add up before I subtract on an entry box in which the user would enter their Monthly Salary in to. Below is the code for my OutgoingMonthlyCost & OutgoingMonthlyCostS...
private decimal outgoingMonthlyCost { get; set; }
public decimal OutgoingMonthlyCost
{
get => outgoingMonthlyCost;
set
{
outgoingMonthlyCost = value;
OnPropertyChanged("OutgoingMonthlyCost");
}
}
private string outgoingMonthlyCostS { get; set; }
public string OutgoingMonthlyCostS
{
get => outgoingMonthlyCostS;
set
{
outgoingMonthlyCostS = value;
OnPropertyChanged("OutgoingMonthlyCostS");
}
}
public void outgoingMonthlyCostFin()
{
outgoingMonthlyCostS = outgoingMonthlyCost.ToString();
TotalBudget();
}
This is currently how I have it laid out but something isnt right as my app keeps crashing...
private decimal totalMonthlyCost { get; set; }
public decimal TotalMonthlyCost
{
get => totalMonthlyCost;
set
{
totalMonthlyCost = value;
OnPropertyChanged("totalMonthlyCost");
TotalBudget();
}
}
private string totalMonthlyCostFin { get; set; }
public string TotalMonthlyCostFin
{
get => totalMonthlyCostFin;
set
{
totalMonthlyCostFin = value;
OnPropertyChanged("totalMonthlyCostFin");
}
}
public void TotalBudget()
{
var Conversion = default(decimal);
if (OutgoingMonthlyCost != 0)
{
Conversion += decimal.Round((Convert.ToDecimal(OutgoingMonthlyCostS)), 2, MidpointRounding.AwayFromZero);
TotalMonthlyCostFin = "£" + (TotalMonthlyCost - Conversion).ToString();
}
else
{
TotalMonthlyCostFin = "£0";
}
}
As shown in the first class displayed, I need to cast Activité to Réunion (Réunion extends Activité) but the compiler tells me that I can't. Why? I'll put a scheme so you can better understand my classes structure and also all my other classes. Thank you.
class Employé<T>
{
private string nom;
private Local bureau;
private LinkedList<Activité<T>> activités;
public Employé(string nom, Local bureau)
{
this.nom = nom;
this.bureau = bureau;
}
public void AjouteActivité(params Activité<T>[] activités)
{
foreach(Activité<T> activité in activités)
{
if (activité as Réunion != null)
// here's the problem !!! ((Réunion)activité).EmployéConvoqués = activité;
}
}
}
Here's the scheme of my classes structure:
And here are the other classes:
abstract class Activité<T>
{
private string label;
private DateTime début, fin;
private T lieu;
private readonly int id;
private static int CPT = 0;
public Activité(string label, DateTime début, DateTime fin, T lieu)
{
this.label = label;
this.début = début;
this.fin = fin;
this.lieu = lieu;
this.id = ++CPT;
}
public override string ToString()
{
return $"{id} : {label}(de {début} à {fin}) - {DescriptionLieu()}";
}
public double Duree()
{
return fin.Subtract(début).TotalMinutes;
}
public int Id
{
//tester get; seulement
get
{
return id;
}
}
public T Lieu
{
get
{
return lieu;
}
}
public abstract string DescriptionLieu();
}
class ActivitéExtérieure : Activité<string>
{
public ActivitéExtérieure(string label, DateTime début, DateTime fin, string lieu) : base(label,début,fin,lieu) { }
public override string DescriptionLieu()
{
return Lieu;
}
}
class ActivitéInterne : Activité<Local>
{
public ActivitéInterne(string label, DateTime début, DateTime fin, Local lieu) : base(label,début,fin,lieu)
{
lieu.AjouteActivité(this);
}
public override string DescriptionLieu()
{
return $"local :: {Lieu.NumComplet}";
}
}
class Employé<T>
{
private string nom;
private Local bureau;
private LinkedList<Activité<T>> activités;
public Employé(string nom, Local bureau)
{
this.nom = nom;
this.bureau = bureau;
}
public void AjouteActivité(params Activité<T>[] activités)
{
foreach(Activité<T> activité in activités)
{
if (activité as Réunion != null)
((Réunion)activité).EmployéConvoqués = activité;
}
}
}
class Local
{
private int etage;
private int numero;
private bool possedeWifi;
private Dictionary<int, ActivitéInterne> historiquesActivités;
public int Numero
{
get
{
return numero;
}
set
{
if (value < 0 || value > 99)
throw new IndexOutOfRangeException();
else
numero = value;
}
}
public int NumComplet
{
get
{
return etage * 100 + numero;
}
}
public bool PossedeWifi
{
get
{
return possedeWifi;
}
}
public Local(int etage, bool possedeWifi, int numero)
{
this.etage = etage;
this.possedeWifi = possedeWifi;
Numero = numero;
}
public Local(int etage, int numero) : this(etage, true, numero) { }
public Local(int local, bool possedeWifi) : this(local / 100, possedeWifi, local % 100) { }
public void AjouteActivité(ActivitéInterne a)
{
historiquesActivités.Add(a.Id, a);
}
}
class Réunion : ActivitéInterne
{
private HashSet<Employé<Local>> employésConvoqués;
public Réunion(string label, DateTime début, DateTime fin, Local lieu) : base(label, début, fin, lieu) { }
public Employé<Local> EmployéConvoqués
{
set
{
employésConvoqués.Add(value);
}
}
}
The error message says that "cast is redundant". This is because you have already tested for "activité as Réunion != null". The compiler figures out that in the 'if' clause this condition is already true, therefore the cast is not meaningful. On the other hand you cannot access activité.EmployéConvoqués because the static type of activité is not Réunion.
All you have to do is introduce a new variable when testing the type. Like this:
if (activité is Réunion réunion) {
réunion.EmployéConvoqués = activité;
}
However if you try this you will see that the assignment cannot be done because you are trying to assign an activity to an Employé<Local>. These are not compatible types. Perhaps you meant something like
foreach (Activité<T> activité in activités) {
if (activité is Réunion réunion && this is Employé<Local> employéLocal) {
réunion.EmployéConvoqués = employéLocal;
}
}
Comment: in the definition of Réunion you are adding to HashSet<Employé<Local>> employésConvoqués when setting the property Employé<Local> EmployéConvoqués. From a style point of view this is strange because people generally expect a property of type Employé<Local> will represent a single Employé<Local> rather than a collection of Employé<Local>. I would suggest that you remove the setter and instead define
public void Ajoute( Employé<Local> employéConvoqué) {
this.employésConvoqués.Add(employéConvoqué);
}
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.
I am new to C# and was asked to create two class definitions (customer and order) using partial code and with the suggested class names, methods, contructors and following an example. I am not sure why I am getting so many errors when I build/debug?
After this is finished, I need to create another program that builds onto this one. Our instructor also asked us not to use validation...
Some of my most common errors are:
expected: ; (in a place in my code where I believe there should not be a semi-colon and
Error "Expected class, delegate, enum, interface, or struct.
Here is my code:
public class clsOrder
{
//declare class variables
protected string cstrDescription;
protected int cintQuantity;
protected decimal cdecPrice;
protected decimal cdecExtendedPrice;
//shared variables
static decimal cdecTotalPrice;
static int cintTotalCount;
//declare constructors
public clsOrder();
}
public clsOrde r(string strDescription,
int intQuantity, decimal decPrice)
}
//declare property methods
{
this.Description = string strDescription;
this.Quantity = int intQuantity;
this.Price = decimal decPrice;
//declare read-only properties
public decimal ExtendedPrice
}
public string Description
{
get
{
return strDescription;
}
set
{
strDescription = value;
}
}
public int Quantity
{
get
{
return intQuantity;
}
set
{
intQuantity = value;
}
}
public decimal Price
{
get
{
return decPrice;
}
set
{
decPrice = value;
}
}
get
{
return cdecExtendedPrice;
}
}
//declare Shared (static) ReadOnly Properites
public static decimal TotalPrice
{
get
{
return cdecTotalPrice;
}
}
public static int TotalCount
{
get
{
return cintTotalCount;
}
}
//declare supporting methods
public void calcExtendedPrice()
{
cdecExtendedPrice = cintQuantity * cdecPrice;
}
public void accumulateTotals()
{
cdecTotalPrice += cdecExtendedPrice;
cintTotalCount += 1;
}
public static void resetTotals()
{
cdecTotalPrice = 0;
cintTotalCount = 0;
}
}//end of Class
}//end of namespace
And
public class clsCustomer
{
//declare class variables
private string cstrName;
private string cstrStreet;
private string cstrCity;
private string cstrState;
private string cstrZip;
//declare constructors
public class clsCustomer()
}
public clsCustomer(string strName,
string strStreet, string strCity,
string strState, string strZip)
}
//declare property methods
{
this.Name = cstrName;
this.Street = cstrStreet;
this.City = cstrCity;
this.State = cstrState;
this.Zip = cstrZip;
}
public string Name
{
get
{
return cstrName;
}
set
{
cstrName = value;
}
}
public string Street
{
get
{
return cstrStreet;
}
set
{
cstrStreet = value;
}
}
public string City
{
get
{
return cstrCity;
}
set
{
cstrCity = value;
}
}
public string State
{
get
{
return cstrState;
}
set
{
cstrState = value;
}
}
public string Zip
{
get
{
return cstrZip;
}
set
{
cstringZip = value;
}
}
Any help would be very much appreciated, thank you.