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
Related
I was expected that constructor can go into the setter condition, I have done the following attempts.
static void Main(string[] args)
{
Iitem car = new Car(7000);
Console.WriteLine($"cost={car.cost}");//expect output "too expensive",but actually show 7000
car.cost = 7000;
Console.ReadLine();
}
public interface Iitem
{
int cost { get; set; }
string status {get;set; }
}
class Car:Iitem
{
private int mycost;
public int cost
{
get { return mycost; }
set
{
if (value > 5000)
{
mycost = 0;
Console.WriteLine("too expensive");
}
else
{
mycost = value;
}
}
}
public string status { get; set; }
public Car(int cost)
{
this.mycost = cost;
}
}
If I discard car.cost=7000 from Main() function, then I can't get the output of too expensive.
you are not getting the desired result because you are setting the value directly into the variable "mycost" in the constructor. Replace it with this.cost = cost;
Screenshot showing column & column entry
Hey all, I'm getting an error saying my column names are invalid. I know it's not the table that is wrong because it worked perfectly fine an hour ago. I tried different variations of the Id, issue, etc with no luck.
namespace Csis265.DAL
{
public class BugMapper : BaseMapper
{
public BugMapper(SqlDataReader rdr) : base(rdr)
{ }
public override object DoMapping()
{
logger.Debug("INSIDE BugMapper DoMapping() !!!");
int id = GetInteger("ID");
string issue = GetString("ISSUE");
string resolution = GetString("RESOLUTION");
int statusId = GetInteger("STATUS_ID");
int priorityId = GetInteger("PRIORITY_ID");
int softwareappId = GetInteger("SOFTWARE_APP_ID");
DateTime dateCreated = GetDateTime("DATE_CREATED");
DateTime dateResolved = GetDateTime("DATE_RESOLVED");
Bug rtnObj = new Bug(id, issue, resolution,
statusId, priorityId, softwareappId, dateCreated, dateResolved);
logger.Debug($"INSIDE BugMapper DoMapping() {rtnObj.ToString()}");
return rtnObj;
}
}
BUG CLASS - Nothing was touched besides the issue get/set. I originally had it as name instead of issue but have fixed that mistake
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
namespace Csis265.Domain
{
public class Bug : BaseObject
{
protected string issue;
protected string resolution;
protected int statusId;
protected int priorityId;
protected int softwareappId;
protected Status status;
protected Priority priority;
protected SoftwareApp softwareapp;
public Status Status
{
get { return status; }
set { status = value; }
}
public Priority Priority
{
get { return priority; }
set { priority = value; }
}
public SoftwareApp SoftwareApp
{
get { return softwareapp; }
set { softwareapp = value; }
}
public int Id
{
get { return id; }
set { id = value; }
}
public string Issue
{
get { return issue; }
set { issue = value; }
}
public string Resolution
{
get { return resolution; }
set { resolution = value; }
}
public string StatusName
{
get { return Status.Name; }
}
public string PriorityName
{
get { return Priority.Name; }
}
public string SoftwareAppName
{
get { return SoftwareApp.Name; }
}
public Bug(int id, string issuse, string resolution,
int statusId, int priorityId, int softwareappId,
DateTime dateCreated, DateTime dateResolved) : base(id, dateCreated)
{
SetIssue(issue);
SetResolution(resolution);
SetStatusId(statusId);
SetPriorityId(priorityId);
SetSoftwareAppId(softwareappId);
}
public void SetIssue(string issue)
{
this.issue = issue;
}
private void SetSoftwareAppId(int softwareappId)
{
this.softwareappId = softwareappId;
}
private void SetPriorityId(int priorityId)
{
this.priorityId = priorityId;
}
private void SetStatusId(int statusId)
{
this.statusId = statusId;
}
private void SetResolution(string resolution)
{
this.resolution = resolution;
}
public string GetIssue()
{
return issue;
}
public override string ToString()
{
return $"BUG ID:{id} ISSUE: {issue} DTC: {dateCreated}";
}
public string GetResolution()
{
return resolution;
}
public int GetStatusId()
{
return statusId;
}
public int GetPriorityId()
{
return priorityId;
}
public int GetSoftwareAppId()
{
return softwareappId;
}
}
}
line 20..
Change
int id = GetInteger("ID'");
to
int id = GetInteger("ID"); // Without the quote
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 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
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.