3

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!

4
  • 1
    Is your PurchaseTotal method in another class? Commented Aug 4, 2014 at 17:16
  • Where are dblGrandTotal and dblSubtotal defined? In the same form? Commented Aug 4, 2014 at 17:23
  • The question was answered already but thank you for your help fellows! Commented Aug 4, 2014 at 17:42
  • Just wanted to also note, I don't think you need to pass in dblSubtotal, dblTaxTotal and dblGrandTotal as refs in your case. Commented Aug 4, 2014 at 17:46

6 Answers 6

4

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"));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I was making it harder than it seemed. We just learned about referencing and methods so I assumed that I would have to use both. Thank you very much!
0

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.

1 Comment

Yeah, I see that now >.< Thanks for the help!
0

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

Comments

0

I don't understand why you would refactor it to a method with that signature:

  • The ref keyword is used when you are going to change the supplied value and allow the caller to use the updated value
  • lbxTally 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 speed
  • A method named PurchaseTotal() 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)

1 Comment

I got tripped up as to what ref actually does. I am a new student to programming and everyone here has been extremely helpful in helping me understand and educate myself further! Thanks for the help!
0

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

1 Comment

Yeah, I got a little tripped up on what ref actually does, Thank you for the explanation!
0

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

9 Comments

Um, why can't you have the method static?
Because PurchaseTotal references the instance field lbxTally.
@JacobKrall, that is passed in as the last argument (I thought that before, but I missed the argument)
@gunr2171: Yep, you are right; libxTally is the last parameter.
the question is not clear whether there should be parameters, this could be a good answer but you need to expand on it to make sense
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.