The problem is in
num1 = Convert.ToDecimal(this.withBox.Text);
of bankAccount()
I am attempting to make it where you can type any decimal into the withBox textbox and when you click the button it will give you the amount in the aMtBox. I am unsure of what I am doing wrong.
Is gives me this error, but I do not know why?
What I am wanting, is for num1 to equal whatever I type into my withBox. That is my end goal.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
BankAccount a = new BankAccount();
public Form1()
{
InitializeComponent();
decimal iBa = 300.00m;
this.aMtBox.Text = iBa.ToString();
}
private void dep_Click(object sender, EventArgs e)
{
try
{
decimal num1 = 0.00m;
decimal iBa = 300.00m;
num1 = Convert.ToDecimal(this.depBox.Text);
decimal total = num1 + iBa;
this.aMtBox.Text = total.ToString();
}
catch
{
MessageBox.Show("ERROR", "Oops, this isn't good!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public void withdrawl_Click(object sender, EventArgs e)
{
this.aMtBox.Text = a.Balance.ToString();
}
public class BankAccount
{
decimal balance;
decimal iBa;
decimal num1;
public decimal Balance
{
get { return balance; }
}
public decimal IBa
{
get { return iBa; }
}
public decimal Num1
{
get { return num1; }
}
public BankAccount()
{
iBa = 300.00m;
num1 = Convert.ToDecimal(this.withBox.Text);
balance = iBa - num1;
}
}
private void withBox_TextChanged(object sender, EventArgs e)
{
}
}
}
this refers to the current instance of the class. this in your BankAccount constructor refers to the BankAccount.. not the Form. Therefore, you cannot access withBox from within BankAccount.
What you need to do.. is pass the textbox instance in. Somewhat like this:
public class BankAccount {
public BankAccount(TextBox withBox) { // Pass it in
iBa = 300.00m;
num1 = Convert.ToDecimal(withBox.Text);
balance = iBa - num1;
}
// ...the rest of the class
}
Then, in your Form.. create your BankAccount like this:
BankAccount a = new BankAccount(withBox);
withBox is not a member of the class BankAccount but you're trying to access it as such. I assume withBox is a member of class Form1
public BankAccount()
{
iBa = 300.00m;
// this.withBox is no good, no such property/member variable exists
num1 = Convert.ToDecimal(this.withBox.Text);
balance = iBa - num1;
}
Related
I'm having an issue with a program I am trying to create. This program is like a credit card and the issue is that the amount of money I put in the 'Credit' variable doesn't seem to affect the variable at all, it stays empty.
The following is the Credit Card class:
I have edited the code because I have just noticed I did not insert everything and have also included the program.cs! The outcome of this program when it runs: https://prnt.sc/jynpt1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CreditCardExample
{
public class CreditCard
{
private double credit;
public double Credit
{
get { return credit; }
set { credit = value; }
}
public CreditCard(double credit)
{
this.credit = Credit;
}
public bool enoughCredit (double creditAmt)
{
bool state = false;
if (Credit >= creditAmt)
{
Console.WriteLine("You have sufficient funds.");
state = true;
}
else
{
Console.WriteLine("You do not have sufficient amount");
state = false;
}
return state;
}
public double getAmtInCreditCard()
{
Console.WriteLine("Amount of Money in your CreditCard: ", Credit);
return Credit;
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CreditCardExample
{
class Program
{
static void Main(string[] args)
{
CreditCardExample.CreditCard c = new
CreditCardExample.CreditCard(20.0);
c.enoughCredit(12);
c.getAmtInCreditCard();
Console.ReadKey();
}
}
}
That's because in your constructor you are assigning the property back to itself and not the value being passed i.e.
this.credit = Credit // notice the casing?
Change to
this.credit = credit;
You have a bug in your code.
public CreditCard(double credit)
{
Credit = credit;
}
Test:
class CreditCard
{
private double credit;
public double Credit
{
get { return credit; }
set { credit = value; }
}
public CreditCard(double credit)
{
Credit = credit;
}
}
static void Main()
{
//Outputs "10"
Console.WriteLine(new CreditCard(10).Credit);
}
I'm learning C#, trying to get to grips with accessors at the moment.
I'm going nuts looking at this, I have no idea what I've done wrong:
class BankAccount
{
// *PROPERTIES*
private int _initialDeposit = 0;
// **ACCESSORS**
public int SavingsAccount
{
set
{
_initialDeposit = value;
}
get
{
return _initialDeposit;
}
}
}
The Form looks like this:
public partial class BankForm : Form
{
private BankAccount _myAccount;
public BankForm()
{
InitializeComponent();
_myAccount = new BankAccount();
}
private void initialDepositButton_Click(object sender, EventArgs e)
{
_myAccount.SavingsAccount = Convert.ToInt32(initialDepositTextBox.Text);
bankAccountListBox.Text = "Account opened with initial Deposit " + initialDepositTextBox.Text;
}
}
But I get this error:
Property or indexer must have at least one accessor
I'm not getting any errors. Move location of private BankAccount _myAccount;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BankForm
{
public partial class BankForm : Form
{
public BankForm()
{
InitializeComponent();
_myAccount = new BankAccount();
}
private BankAccount _myAccount;
private void initialDepositButton_Click(object sender, EventArgs e)
{
_myAccount.SavingsAccount = Convert.ToInt32(initialDepositTextBox.Text);
bankAccountListBox.Text = "Account opened with initial Deposit " + initialDepositTextBox.Text;
}
}
class BankAccount
{
// *PROPERTIES*
private int _initialDeposit = 0;
// **ACCESSORS**
public int SavingsAccount
{
set
{
_initialDeposit = value;
}
get
{
return _initialDeposit;
}
}
}
}
This is the first problem I have encountered. Truly, I don't know why this is. I read on it a bit and made most of what I saw important public (if not all). So I thought maybe someone here can explain this. Also, I am trying to make it where when someone types in the withdrawl text box and clicked on it, the aMtBox will show such amount. Is this workable? Or am I doing something very wrong here
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
BankAccount a = new BankAccount();
public Form1()
{
InitializeComponent();
decimal iBa = 300.00m;
this.aMtBox.Text = iBa.ToString();
}
public void withdrawl_Click(object sender, EventArgs e)
{
MessageBox.Show("The balace is... {0:c2}", a.balance.ToString());
}
public class BankAccount
{
decimal balance;
decimal iBa;
decimal num1;
public decimal Balance
{
get { return balance;}
}
public decimal IBa
{
get { return iBa;}
}
public decimal Num1
{
get { return num1;}
}
public BankAccount()
{
iBa = 300.00m;
num1 = 0.00m;
balance = iBa - num1;
}
}
}
}
change
a.balance.ToString()
to
a.Balance.ToString()
a.balance is inaccessible to outer class due to being private.
By running my code i am getting this error
Method must have a return type. Its in my public withdraw method at the bottom. I may not be understanding this correctly, but I thought my return would be okay since my accessor and mutator methods are doing such. Also, I feel like since I using aMtBox.Text = a.withdraw(withdraw); that its not going to cast it correctly(or going to be needed to be casted?). I don't have an error about it yet but I am guessing its because I have not got past the return problem.
So, in a simpler way, how can I call my withdraw method to be used correctly in my withdraw_Click.
My Main form code is as below
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
BankAccount a = new BankAccount();
public void withdrawl_Click(object sender, EventArgs e)
{
aMtBox.Text = a.withdraw(withdraw);
}
}
}
And My code for BankAccount is as below
public class BankAccount
{
decimal num1;
decimal iBa;
decimal total;
public decimal IBa
{
set { iBa = value; }
get { return iBa; }
}
public decimal Num1
{
set { num1 = value; }
get { return num1; }
}
public decimal Total
{
set { total = value; }
get { return total; }
}
public decimal withdraw(decimal withdraw)
{
num1 = 0.00m;
iBa = 300.00m;
total = iBa - num1;
return total;
}
}
this method must have return type
public withdraw(decimal withdraw)
{
num1 = 0.00m;
iBa = 300.00m;
total = iBa - num1;
}
like
public decimal withdraw(decimal withdraw)
{
num1 = 0.00m;
iBa = 300.00m;
total = iBa - num1;
return total;//or whatever
}
You are getting this error Method must have a return type because you do not have return type of
public withdraw(decimal withdraw)
{
num1 = 0.00m;
iBa = 300.00m;
total = iBa - num1;
}
Now, as you know the problem you can fix it by giving return type to it,
Now question is which return type will you give string or decimal?
My advice is to use decimal and then try to convert it in string while you assign it to
any string property like here you do aMtBox.Text = a.withdraw(withdraw);
Your code will look like
public decimal withdraw(decimal withdraw)
{
num1 = 0.00m;
iBa = 300.00m;
total = iBa - num1;
return total;
}
and while you getting it use aMtBox.Text = a.withdraw(withdraw).ToString();
Edit
Do not forget to declare variable withdraw in your code
Your code will look like
Decimal withdraw = 100;
aMtBox.Text = a.withdraw(withdraw).ToString();
#As asked in comment i am writing this code it is not part of original answer
/// <summary>
/// Called when user change textbox value
/// </summary>
/// <param name="sender">Represent object of sender (i.e text box itself)</param>
/// <param name="e">Reesent eventArgs</param>
private void withBox_TextChanged(object sender, EventArgs e)
{
TextBox tmpTextBox = (TextBox)sender; // Doing type casting sender to textbox so that we can access property of it.
decimal withdraw;
bool bIsParsed = Decimal.TryParse(tmpTextBox.Text, out withdraw); // TryParse method return false if there is any problem in process of doing parsing of string to decimal
if (!bIsParsed)
{
MessageBox.Show("Please enter valid number");
}
}
I am trying to learn classes, meaning writing codes outside of the windows form and calling that code in the form. I created a basic calculation in the form and it works. The problem is, I want to know how I can move the entire calculation into a class and call it into the form on load or when a control is click. It does not work!
Here is my local or form version which works:
Please note that the form will load with the first calculation because the Radiobutton is checked on load.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculations
{
public partial class Calculations : Form
{
public Calculations()
{
InitializeComponent();
}
public decimal SalesTax = 0.065M;
public decimal AppliedTax;
public decimal Price;
public decimal SubTotal;
public decimal GrandTotal;
public object Calc()
{
Int32 GetQuantity = Convert.ToInt32(txtQuantity.Text);
SubTotal = (Price * GetQuantity);
AppliedTax = (SubTotal * SalesTax);
GrandTotal = (SubTotal + AppliedTax);
if (radRed.Checked == true)
{
Price = 100;
}
else if (radBlue.Checked == true)
{
Price = 200;
}
else if (radGreen.Checked == true)
{
Price = 300;
}
lblPrice.Text = GrandTotal.ToString("c");
return GrandTotal;
}
private void Calculations_Load(object sender, EventArgs e)
{
txtQuantity.Text = "10";
radRed.Checked = true;
Calc();
}
private void radRed_CheckedChanged(object sender, EventArgs e)
{
Calc();
}
private void radBlue_CheckedChanged(object sender, EventArgs e)
{
Calc();
}
private void radGreen_CheckedChanged(object sender, EventArgs e)
{
Calc();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
Calc();
}
}
}
And here is my attempt to place all of this in a class:
It works but not on load, you have to check another radiobutton first
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Calculations
{
class ClsCalc
{
class ClsRef
{
public static Calculations FormCtrls
{
get
{
return Calculations.ActiveForm as Calculations;
}
}
}
public static decimal SalesTax = 0.065M;
public static decimal AppliedTax;
public static decimal Price;
public static decimal SubTotal;
public static decimal GrandTotal;
public static object Calc()
{
Int32 GetQuantity = Convert.ToInt32(ClsRef.FormCtrls.txtQuantity.Text);
SubTotal = (Price * GetQuantity);
AppliedTax = (SubTotal * SalesTax);
GrandTotal = (SubTotal + AppliedTax);
if (ClsRef.FormCtrls.radRed.Checked == true)
{
Price = 100;
}
else if (ClsRef.FormCtrls.radBlue.Checked == true)
{
Price = 200;
}
else if (ClsRef.FormCtrls.radGreen.Checked == true)
{
Price = 300;
}
ClsRef.FormCtrls.lblPrice.Text = GrandTotal.ToString("c");
return GrandTotal;
}
}
}
Finally I called it in the form Load event:
ClsCalc.Calc();
Please note this is not a real project just a way to learn, well for me that is.
Thank you!
Your calculation method is almost entirely involving interactions with UI elements. You gather a bunch of information from a textbox and various checkboxes, and then have just three lines of actual calculations, and then you display the results.
UI interaction shouldn't be moved outside of the form. You shouldn't expose the internal controls of a form publicly; they should only ever be accessed from within their parent form (or user control, if that's the case).
A common pattern that you'll see when performing some form submit operation is to gather information from input controls, do some calculations on the data, and then display the results on the form. Both the gathering information and displaying results should stay within the form, the only thing that you should (potentially) move outside of the form's definition is the calculations. In this case that's such a small amount of work there just isn't a compelling reason to do that.