C # creating functions and object references
This time, I need to turn the previously written code into a function and call the function in the code. I have a problem referencing a shortcut box and can't seem to find an answer. Here's the code:
private void btnEndSale_Click(object sender, EventArgs e)
{
dblGrandTotal = dblSubtotal + dblTaxTotal;
lbxTally.Items.Add("");
lbxTally.Items.Add("");
lbxTally.Items.Add("Subtotal: " + dblSubtotal.ToString("C"));
lbxTally.Items.Add("Tax Total: " + dblTaxTotal.ToString("C"));
lbxTally.Items.Add("Grand Total: " + dblGrandTotal.ToString("C"));
}
and this is what I am trying to turn into this:
static void PurchaseTotal(ref double dblSubtotal, ref double dblTaxTotal, ref double dblGrandTotal, object lbxTally)
{
dblGrandTotal = dblSubtotal + dblTaxTotal;
lbxTally.Items.Add("");
lbxTally.Items.Add("");
lbxTally.Items.Add("Subtotal: " + dblSubtotal.ToString("C"));
lbxTally.Items.Add("Tax Total: " + dblTaxTotal.ToString("C"));
lbxTally.Items.Add("Grand Total: " + dblGrandTotal.ToString("C"));
}
So that I can just use:
private void btnEndSale_Click(object sender, EventArgs e)
{
PurchaseTotal()
}
I am a bit lost on how to refer to the object's label field (or do I need to?) And if I need to reference my variables in the PurchaseTotal function again when I call it. Any help is appreciated! Thank!
source to share
For reasons of things, all the objects you need exist as class members (for example, they are declared in the form itself), so you can refer to them from any instance method.
With that said, you should simply do this:
private void btnEndSale_Click(object sender, EventArgs e)
{
PurchaseTotal();
}
private void PurchaseTotal()
{
dblGrandTotal = dblSubtotal + dblTaxTotal;
lbxTally.Items.Add("");
lbxTally.Items.Add("");
lbxTally.Items.Add("Subtotal: " + dblSubtotal.ToString("C"));
lbxTally.Items.Add("Tax Total: " + dblTaxTotal.ToString("C"));
lbxTally.Items.Add("Grand Total: " + dblGrandTotal.ToString("C"));
}
source to share
You cannot place PurchaseTotal. So just remove the static keyword.
Edit 1
There are two ways to do this. One is to keep your methods static and label-passed. Here's an example:
static void PurchaseTotal(LabelBox lbxTally, ref double dblSubtotal, ref double dblTaxTotal, ref double dblGrandTotal)
{
dblGrandTotal = dblSubtotal + dblTaxTotal;
lbxTally.Items.Add("");
lbxTally.Items.Add("");
lbxTally.Items.Add("Subtotal: " + dblSubtotal.ToString("C"));
lbxTally.Items.Add("Tax Total: " + dblTaxTotal.ToString("C"));
lbxTally.Items.Add("Grand Total: " + dblGrandTotal.ToString("C"));
}
Now you need to pass the label to the function ...
private void btnEndSale_Click(object sender, EventArgs e)
{
PurchaseTotal(lbxTally, ref dblSubtotal, ref dblTaxTotal, ref dblGrandTotal);
}
Another way to do this is to remove the static one as your lbxTally variable is local and the static methods are unaware of your local variable. Therefore, you will need to change it so that it looks like this:
void PurchaseTotal()
{
dblGrandTotal = dblSubtotal + dblTaxTotal;
lbxTally.Items.Add("");
lbxTally.Items.Add("");
lbxTally.Items.Add("Subtotal: " + dblSubtotal.ToString("C"));
lbxTally.Items.Add("Tax Total: " + dblTaxTotal.ToString("C"));
lbxTally.Items.Add("Grand Total: " + dblGrandTotal.ToString("C"));
}
Now you call this function with no parameters
private void btnEndSale_Click(object sender, EventArgs e)
{
PurchaseTotal();
}
source to share
All of this is written on the basis that you need to pass these objects as parameters for one reason or another. Trevor's answer is better if you are allowed to have an instance-method in the same class as your event.
lbxTally
does not have a type object
if it has a property Items
. Try to hang over it, then the clue should tell you what it is. Then you can change your parameter type to customize it. For example, if he said ListBox
, you can change your setting from object lbxTally
to ListBox lbxTally
.
Alternatively, you can find it by opening the constructor and looking at that particular object, or by right clicking the link to the control and clicking Go to Definition
and checking how it is declared.
Note, of course, that you need to check the type in the original method. It won't work if you do it with what's already in the method. since this is already a object
.
source to share
Edit
object lbxTally
to
LabelBox lbxTally
or whatever object you are using.
For me, when I do validation on a form, I pass in the controls that I want to validate in order to change their properties. So I want to check the text in a box, I pass the TextBox and its corresponding label. If the text is empty, change the label to red and mark boolean.
I answered this and another question: Passing data between class and form in C # using the delegate parameter
source to share
I don't understand why you refactored it to a method with this signature:
- The keyword is
ref
used when you are about to change the provided value and allow the caller to use the updated value -
lbxTally
is a type parameterobject
that is the least type specific and you are trying to access properties specific toLabelBox
. Change the parameter type toLabelBox
for simplicity, readability, and speed. - The named method
PurchaseTotal()
would lead me to assume that a big purchase is being made. You might want to reflect what you are actually doing in this method, for example:UpdatePurchaseTotalLabels()
(Yes, I understand this is not a code review site, but it is too long as a comment)
source to share
The keyword ref
means that the function can change the parameter that is passed to . The calling site will look something like this:
private void btnEndSale_Click(object sender, EventArgs e)
{
PurchaseTotal(ref dblSubtotal, ref dblTaxTotal, ref dblGrandTotal, lbxTally);
}
Sure; it is so stupid. Instead, you should use fields in your class internally PurchaseTotal
and remove the modifier static
from its definition:
void PurchaseTotal()
{
dblGrandTotal = dblSubtotal + dblTaxTotal;
lbxTally.Items.Add("");
lbxTally.Items.Add("");
lbxTally.Items.Add("Subtotal: " + dblSubtotal.ToString("C"));
lbxTally.Items.Add("Tax Total: " + dblTaxTotal.ToString("C"));
lbxTally.Items.Add("Grand Total: " + dblGrandTotal.ToString("C"));
}
And your calling site becomes, at will,
private void btnEndSale_Click(object sender, EventArgs e)
{
PurchaseTotal();
}
source to share