C# help. Call operations from classes - c#

class Addition : Form1
{
public string AdditionTotal(){
string AdditionTotal;
int num1 = int.Parse(txtFirstNumber.Text);
int num2 = int.Parse(txtSecondNumber.Text);
AdditionTotal = (num1 + num2).ToString();
return AdditionTotal;
}
public AdditionEqual() //Throws an error here
{
Convert.ToInt32(AdditionTotal);
int AdditionEqual = AdditionTotal;
Addition frm2 = new Addition();
frm2.Show();
this.Hide();
Form 1:
public Form1()
{
InitializeComponent();
}
public string AdditionEqual { get; set; }
private void button1_Click(object sender, EventArgs e)
{
Addition MyLaptop = new Addition();
if (Add.Checked)
{
MessageBox.Show( this.AdditionEqual);
}
Can someone please edit and tell me whats wrong with my code here? I need to use classes and radiobuttons to make a calculator. There's currently 1 error. It says the public AdditionEqual() needs to have a return type. Theres only 1 error but I dont know if the code will actually work after that's fixed. Can someone help?

The function needs a return type. Your first method returns a string:
public string AdditionTotal(){
That string is indicating what datatype the function will return.
public AdditionEqual()
This function doesn't have that indicator. If you don't intend for the method to return anything, use void.
public void AdditionEqual()

Related

Working with methods and return values in classes

Maybe this is a very silly question but it's been a long time I worked with VS.
I'm trying to figure out how I can call a method/function from inside my program e.g. by pressing a button. But the method is written in Class1.cs. I show below what I'm trying but I know this is completely wrong:
namespace TestProject1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
AddingNumbers(3, 5);
label1.Text = Result;
}
}
}
And in Class1.cs I have something like this:
namespace TestProject1
{
class Class1
{
public static AddingNumbers(int num1, int num2)
{
return num1 + num2;
}
}
}
If someone could help me understand this I would really appreciate. I don't need a full answer because then I won't learn anything.
I think it has to do with Public, Static, Void or something like this. I have tried to understand how this principle works by reading on MSDN but I don't grasp it for now.
Advice would be appreciated
I think you have missed return type of AddingNumbers method it would be as follows
public static int AddingNumbers(int num1, int num2)
{
return num1 + num2;
}
And in Form1 class you can called AddingNumbers(static method) by it's class name as follows
private void button1_Click(object sender, EventArgs e)
{
var Result = Class1.AddingNumbers(3, 5);
//As per your requirement you need to show it in label that's
//why used ToString() method to convert from int to string
label1.Text = Result.ToString();
}
Accessing static method within a class still require you to call the full namespace. Therefore you should be calling it using Class1.AddingNumbers(3, 5);
And your static method need a return type int

Method of second class doesn`t want to work in main class

guys! I have a problem. My method doesn`t want to work in main class Form1.
Closer to the point. I have main class Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void GetLengthFirst_Click(object sender, EventArgs e)
{
string result = Microsoft.VisualBasic.Interaction.InputBox("Input elem:");
FirstArray.Text = result;
// WHEN I CALL METHOD FROM MY CLASS Array IT DOESNT WORK
Array _arr = new Array();
_arr.Masstrickt(result);
/* BUT WHEN I CALL PROPERTIES OF THIS CLASS FOR EXMPL
ReturnSecond = "12345";
IT WORK*/
}
public string ReturnSecond
{
get { return SecondArray.Text; }
set { SecondArray.Text = value; }
}
}
Second class:
class Array
{
public void Masstrickt(string x)
{
Form1 frm = new Form1();
frm.ReturnSecond = x;
}
}
Sorry for my grammar. I`m not native speaker
You want to pass the result of Masstrickt to your form, so you need to change your function return type from void to string.
If you declare Array out of Form1 class, set it to public
// public keyword would make Array visible to Form1
public class Array
{
// Return type is string
public string Masstrickt(string x)
{
return x;
}
}
In Form1
public void GetLengthFirst_Click(object sender, EventArgs e)
{
string result = Microsoft.VisualBasic.Interaction.InputBox("Input elem:");
FirstArray.Text = result;
Array _arr = new Array();
ReturnSecond = _arr.Masstrickt(result);
// ReturnSecond is the content of result
}

C# SQLite Universal App Insert error Argument 1: cannot convert from 'MemBrainz.MainPage.Mood' to 'System.Collections.IEnumerable'

I've got this code, and it keeps coming up with the same error. Basically, I want to add "1" to the database under CurrentMood when the button is clicked. I'm getting:
Argument 1: cannot convert from 'MemBrainz.MainPage.Mood' to 'System.Collections.IEnumerable'
Here's my code
public class Mood
{
public int Id { get; set; }
public int CurrentMood { get; set; }
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
greetingOutput.Text = "1";
Mood mood1 = new Mood()
{
This is where the error is
CurrentMood = 1
};
SQLite.SQLiteAsyncConnection addconn = new SQLite.SQLiteAsyncConnection("mood.db");
await addconn.InsertAllAsync(mood1);
}
}
InsertAllAsync expects to be passed a list of things to insert. As you are only inserting one thing, you probably want InsertAsync.

Trying to add with method, believe something is not instantiating

When I type in the textbox, I am wanting it to add the numbers, instead if I type (for example) 12, and click deposit again, it only shows 12. I think this is because it seems to think thats its 0 plus 12 everytime. Something doesnt seem to be instantiating correctly. I think. Can anyone point out what I am doing incorrectly?
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_deposit_Click(object sender, EventArgs e)
{
double input;
input = double.Parse((putin.Text));
BankAccount a = new BankAccount(input);
aMtBox.Text = a.AccountBalance.ToString();
}
}
public class BankAccount
{
private double num1;
private double accountBalance;
public BankAccount(double input)
{
num1 = input;
Deposit();
}
public double Num1
{
set {num1 = value;}
get {return num1;}}
public double AccountBalance
{
get {return accountBalance;}
set {accountBalance = value;}}
public void Deposit()
{
accountBalance = accountBalance + num1;
}
}
}
You're creating a new instance of BankAccount each time the button is clicked - so accountBalance will be 0.0 (the default value for a field of type double). How did you expect it to "know" about the previous balance?
It's entirely possible that you should have an instance variable of type BankAccount in your form. You should also consider what the num1 instance variable in BankAccount is meant to represent. The name certainly doesn't tell us anything. It feels like it should actually just be a parameter to the Deposit method.
Additionally, for currency values you should never use double - use either decimal, or an integer type to represent the number of cents (or pence, or whatever). You don't want to get into the normal binary floating point issue.
At this point, your method would become something like:
// I hate the VS-generated event names, but...
private void btn_deposit_Click(object sender, EventArgs e)
{
// TODO: Use decimal.TryParse, and handle invalid input cleanly.
decimal newDeposit = decimal.Parse(putin.Text);
account.Deposit(newDeposit);
aMtBox.Text = account.AccountBalance.ToString();
}
You need to have a BankAccount field for the form which is initialized with the form. Should look something like this:
public partial class Form1 : Form
{
private BankAccount account;
public Form1()
{
InitializeComponent();
account = new BankAccount(0);
}
private void btn_Deposit_Click(object sender, EventArgs e)
{
account.Num1 = double.Parse((putin.Text));
account.Deposit();
aMtBox.Text = account.AccountBalance.ToString();
}
}
On a side note, Input should be validated, and the BankAccount class refactored to something like:
class BankAccount
{
private double num1;
private double accountBalance;
public BankAccount(double startingBalance)
{
accountBalance = startingBalance;
}
public double AccountBalance
{
get {return accountBalance;}
set {accountBalance = value;}
}
public void Deposit(double depositAmount)
{
accountBalance += depositAmount;
}
}

Understanding this Event Example

After reading online tutorials regarding events , I think I almost have an idea of whats going on. I developed the following extremely simple code to trigger an event in case a value is greater than 5.I know the code is pretty useless but I am using it to get my point across. (Instead of a main I just used a button event to trigger the code.)
//declare the delegate
public delegate void MyDelegate(string str);
public class SomeClass
{
public event MyDelegate MyEventFromDelegate;
private int i;
public int I
{
get
{ return i; }
set
{
if (value > 5)
{
MyEventFromDelegate("Value Greater than 5");
i = 0;
}
else
{
i = value;
}
}
}
}
public partial class Form1 : Form
{
public Form1()
{ InitializeComponent(); }
public void Method_To_Call(String rx)
{ MessageBox.Show("This method will be called if greater than 5");}
private void button1_Click(object sender, EventArgs e)
{
SomeClass a = new SomeClass();
a.MyEventFromDelegate +=new MyDelegate(Method_To_Call);
a.I = 12;
}
}
The only concern I have here is when we want to raise an event with the statement
MyEventFromDelegate("Value Greater than 5");
What point is passing a parameters to the event is at this point if later (at button click event) we are actually going to assign it a function to call every time an event is triggered.
In your very simple example - there is no point, because SomeClass instance "a" is very short-lived, and because you are not using rx parameter passed to Method_To_Call.
Your form method button1_Click is connected to the button's Click event through a delegate. Button does not know what code will execute when it is clicked. All it has to do is to signal that is has been clicked. That signal is implemented using a delegate.
Your could have defined your delegate as having an integer parameter where the checked value is passed. Then although the event method would be invoked only when value is greater than 5, inside the event method you could do things differently depending on the actual value.
//declare the delegate
public delegate void MyDelegate(int aValue);
public class SomeClass
{
public event MyDelegate MyEventFromDelegate;
private int i;
public int I
{
get
{ return i; }
set
{
if (value > 5)
{
MyEventFromDelegate(value);
i = 0;
}
else
{
i = value;
}
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Method_To_Call(int aValue)
{
MessageBox.Show("This method signals that value is greater than 5. Value=" + aValue.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
SomeClass a = new SomeClass();
a.MyEventFromDelegate +=new MyDelegate(Method_To_Call);
a.I = 12;
}
}

Categories

Resources