Reputation: 31
This time around I have to turn a previously written code in a function and call the function in the code. I am having a problem when referencing my label 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 here's what I'm trying to turn it into:
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'm getting a little lost as to how to reference the object label box (or if I need to?) and if I need to reference my variables again in the PurchaseTotal function when I call it. Any help is appreciated! Thanks!
Upvotes: 3
Views: 99
Reputation: 1849
You can't make PurchaseTotal static. So just remove the static keyword.
Edit 1
There are two ways of doing this. One is to keep your methods as static and passing in the label. Here is 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 into the function...
private void btnEndSale_Click(object sender, EventArgs e)
{
PurchaseTotal(lbxTally, ref dblSubtotal, ref dblTaxTotal, ref dblGrandTotal);
}
Another way of doing it is by removing the static because your lbxTally variable is local and static methods doesn't know about your local variable. So you would need to change it to look 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();
}
Upvotes: 0
Reputation: 28825
The ref
keyword means that the function can modify the parameter that is passed in. The call site would look something like this:
private void btnEndSale_Click(object sender, EventArgs e)
{
PurchaseTotal(ref dblSubtotal, ref dblTaxTotal, ref dblGrandTotal, lbxTally);
}
Of course; that is very silly. Instead, what you should do is use the fields in your class inside PurchaseTotal
and remove the static
modifier 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 call site becomes, as desired,
private void btnEndSale_Click(object sender, EventArgs e)
{
PurchaseTotal();
}
Upvotes: 0
Reputation: 26436
I don't understand why you would refactor it to a method with that signature:
ref
keyword is used when you are going to change the supplied value and allow the caller to use the updated valuelbxTally
is a parameter of type object
, which is the least specific type, and you're trying to access properties specific to LabelBox
. Change the parameter type to LabelBox
for simplicity, readability and speedPurchaseTotal()
would make me assume a large purchase is being made. You may want to reflect what you're actually doing in that method, ie: UpdatePurchaseTotalLabels()
(Yes, I realize this is not the code review site, but it's too long as a comment)
Upvotes: 0
Reputation: 595
Change
object lbxTally
to
LabelBox lbxTally
or whatever the object you are using's name.
For me when I do validation on a form I pass in the controls I want to validate so I can change their properties. So I want to validate the text in a box, I pass in TextBox and its respective Label. If the text is empty, then change the label red and flag the boolean.
I've answered this in another question as well: Passing data between class and form in C# using delegate parameter
Upvotes: 0
Reputation: 16393
By the looks of things, all the objects you need exist as class members (e.g. they are declared within the form itself) so you can reference them from any instance method.
With that said, you should simply be able to 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"));
}
Upvotes: 4
Reputation: 13286
This is all written on the premise that you are required to pass those objects as parameters, for one reason or another. Trevor's answer is better practice if you're allowed to have an instance-method in the same class as your event.
lbxTally
isn't of type object
if it has an Items
property. Try hovering over it, then the Tooltip should tell you what it is. Then you can change your parameter type to adjust for that. For instance, if it said ListBox
, you could change your parameter from object lbxTally
to ListBox lbxTally
.
Alternatively, you could find it by opening the designer and reviewing that particular object, or by right-clicking a reference to the control and clicking Go to Definition
and checking how it's declared.
Note, of course, that you have to check the type in the original method. It won't work if you do it to the one that's already in the method. since that's already an object
.
Upvotes: 0