Inaccessible due to its protection level in windows forms - c#

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.

Related

Variable Not holding values

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);
}

Property or indexer must have at least one accessor

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;
}
}
}
}
​

No extension method with windows forms

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

Performing calculations using a class and referencing form controls

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.

Input string was not in a correct format. Converting String to double

I'm quite a beginner in C# , empty string to double conversion can be carried out under the button1_click event ..but doing it under Public Form1() it gives me this error
Input string was not in a correct
format.
Here`s the code..(the form1.cs and the Guy.cs class)
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
guy1 = new Guy() ;
guy1.guyname = textBox1.Text;
guy1.guycash = double.Parse(textBox2.Text);
}
}
Guy guy1 ;
private void button1_Click(object sender, EventArgs e)
{
label5.Text = guy1.TakeCash(double.Parse(textBox3.Text)).ToString();
}
}
}
The Guy.cs Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
class Guy
{
private string name;
private double cash;
public string guyname
{
get { return name; }
set { name = value; }
}
public double guycash
{
get { return cash ; }
set { cash = value; }
}
public double TakeCash(double amount)
{
if (cash > amount)
{
cash -= amount;
return cash;
}
else
{
MessageBox.Show("Not enough Cash.");
return 0;
}
}
}
}
the error is caused by the guy1.guycash = double.Parse(textBox2.Text); line , when i try double.TryParse(textbox2.Text, out x) in If() before it, it returns false .
how to solve this please ?
thanks in advance .
Continuing from astanders answer:
double d;
if(!Double.TryParse(textBox2.Text, out d)){
return; // or alert, or whatever.
}
guy1 = new Guy() ;
guy1.guyname = textBox1.Text;
guy1.guycash = d;
What you're doing is attempting the parse, and if it fails, taking some other action. Since the user can input anything they want, this guarantees that if you fail to parse the input (because it's not a decimal), you can handle it nicely and tell them to fix their input.
This should be fine
double d;
Double.TryParse(String.Empty, out d);
Double.TryParse Method (String, Double%)
It looks like the problem is that you are not handling inproper user input. You are trying to parse string from textbox to double without suggestion that it may not be parsed ok (for example user could input 'abcd' in the textbox). Your code should use TryParse method and show error message when input was not parsed.
I guess parsing fails because of non-numeric input or becase of culture problems (like you have "." as desimal symbol by user inputs number with ",").

Categories

Resources