# If statement to calculate the average

Page 1 of 1

## 11 Replies - 1433 Views - Last Post: 13 June 2012 - 11:07 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=282095&amp;s=0aefa561daadc43588b572dbe5425368&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 driftking96

Reputation: 1
• Posts: 24
• Joined: 02-June 12

# If statement to calculate the average

Posted 07 June 2012 - 03:43 PM

I'm making a deal or no deal game and i cant figure out how to this one section.

My objective: to check the remaining btns in play and obtain the cash prizes in the remaining btns then calculate the average. It may seem simple but it actually isn't.

Here is what i have so far:
```Private Sub btncase1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncase1.Click
If usercase = 0 Then
usercase = btncase1.Text
lblyourcase.Text = usercase
lblmessage.Text = "pick 6 more cases"
lblnumcases.Text = counter
ElseIf usercase > 0 Then
lblmessage.Text = case1
lblcase1.ForeColor = Color.Red
btncase1.Visible = False
counter = counter - 1
lblnumcases.Text = counter
End If
If counter <= 0 Then
Randomize()
randombankeroffer = 3 * Rnd() + 1                                     'This is where im stuck
'Obtain the buttons that are currently in play and add and divide to find average of their values then multiply by 1,2, or 3 (randombankeroffer)
lblmessage.Text = "The banker offers you a deal of" + bankeroffer
End If
End Sub

```

Is This A Good Question/Topic? 0

## Replies To: If statement to calculate the average

### #2 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6519
• Posts: 14,408
• Joined: 02-June 10

## Re: If statement to calculate the average

Posted 07 June 2012 - 06:33 PM

When a case button is clicked, as part of your moving of it you should disable it (.Enabled = false)

Then here you can just for loop through all of your buttons and only add the values of the buttons that are .Enabled = True

### #3 driftking96

Reputation: 1
• Posts: 24
• Joined: 02-June 12

## Re: If statement to calculate the average

Posted 08 June 2012 - 05:07 PM

So does that mean that i declare another integer variable and run a for loop statement. Because it is not possible to loop actual buttons and store as integer. checkforoffer = btncase to btncase26 step 1. So i was wondering whether i should use an IF statement because if i do use an IF statement i still have to check the conditions for each button right?

Ex:
```For checkforoffer = btncase to btncase26 step 1
If btncase1.enabled = true then
'Segment of code
End If
If btncase2.enabled = true then
'Segment of code
End If
....And so on
Next
```

This is the code i have so far:
``` Private Sub btncase1_Click

If counter <= 0 Then
Randomize()
randombankeroffer = 3 * Rnd() + 1
For Me.checkforoffer = btncase1 To btncase26 Step 1           'Doesnt Work, because btn cannot be converted to integer

Next

For Me.checkforoffer = btncase1.Text To btncase26.Text Step 1      ' 2nd Attempt didnt work, because text displayed in a btn cannot be enabled or disabled.

Next
'Obtain the buttons that are currently in play and add and divide to find average of their values then multiply by 1,2, or 3 (randombankeroffer)
End If

```

### #4 driftking96

Reputation: 1
• Posts: 24
• Joined: 02-June 12

## Re: If statement to calculate the average

Posted 08 June 2012 - 05:12 PM

Quote

When a case button is clicked, as part of your moving of it you should disable it (.Enabled = false)

Im sorry but i forgot to mention this. When the user picks the first case (usercase) it will only disable (.enabled = false) but the btn will still be visible (so in the end it will display the cash prize). Once the user has picked their case, every other case they pick will disappear (.visible = false)

### #5 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6519
• Posts: 14,408
• Joined: 02-June 10

## Re: If statement to calculate the average

Posted 08 June 2012 - 05:16 PM

Quote

Because it is not possible to loop actual buttons and store as integer.

So why can't you loop through the buttons?

C#
```int total = 0;
foreach(button b in myButtonCollection)
{
if (b.Enabled) total += (int)b.Tag;
}
```

### #6 driftking96

Reputation: 1
• Posts: 24
• Joined: 02-June 12

## Re: If statement to calculate the average

Posted 08 June 2012 - 05:41 PM

Quote

So why can't you loop through the buttons?

Well isnt the .tag property the one where if you hover your cursor above the button it will display some explanation? I wasn't 100% sure so i asked my teacher and he said that thats what it was so he recommended that i dont use tags.

### #7 BobRodes

• Lovable Curmudgeon

Reputation: 600
• Posts: 3,069
• Joined: 19-May 09

## Re: If statement to calculate the average

Posted 08 June 2012 - 06:12 PM

driftking96, on 08 June 2012 - 07:41 PM, said:

Quote

So why can't you loop through the buttons?

Well isnt the .tag property the one where if you hover your cursor above the button it will display some explanation? I wasn't 100% sure so i asked my teacher and he said that thats what it was so he recommended that i dont use tags.

Sounds like your teacher needs to learn to use google. Tag is invisible to the user; it just adds a string value to the control that the developer can use for whatever he wants. You and he are thinking about ToolTips.

I would suggest that you revisit Thlin's question.

This post has been edited by BobRodes: 08 June 2012 - 06:13 PM

### #8 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6519
• Posts: 14,408
• Joined: 02-June 10

## Re: If statement to calculate the average

Posted 08 June 2012 - 06:19 PM

No. And you really need to get used to looking things up on MSDN like we've suggested and not just assumed you know what something totally new to you is.
MSDN .Tag property

I told you once already in the other thread that a tag can be any object and that you could store the actual monetary value of the case in that tag. If that didn't make sense to you then you should have said something or looked it up. Doing a google search for "MSDN .tag" is free, don't be afraid or too lazy to do it.

Quote

I wasn't 100% sure so i asked my teacher and he said that thats what it was

Then your teaching is a fraking idiot and has no business getting paid to teachthing and you can tell them I said it and to write me:
Email me

The tooltip text you are talking about is store in... wait for it.. the .ToolTip property. You'll find it a couple down from the .Tag property.

Please tell us you aren't paying money for this course!

### #9 driftking96

Reputation: 1
• Posts: 24
• Joined: 02-June 12

## Re: If statement to calculate the average

Posted 09 June 2012 - 09:21 AM

ok thanks for the info guys next time ill make sure to check up on MSDN before asking people about it.

Quote

C#
```int total = 0;
foreach(button b in myButtonCollection)
{
if (b.Enabled) total += (int)b.Tag;
}

```

I tried what you guys told me and the code compiles and runs but i just dont get an answer for the bankeroffer variable.

Here's what i have so far. And all what i've done here is with help from @tlhIn`toq and @BobRodes and MSDN sites (How to Create a collection, Controls Property and For Each...Next Statement

```Public Class DealorNoDealGamePlay
Dim totalremcases As Integer
Dim instance As New Control
Dim btncollection As New Microsoft.VisualBasic.Collection()

Private Sub btncase1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncase1.Click
btncase1.Tag = case1
usercase = btncase1.Text
lblyourcase.Text = usercase
lblmessage.Text = "pick 6 more cases"
lblnumcases.Text = counter
btncase1.Enabled = False
ElseIf usercase > 0 Then
lblmessage.Text = btncase1.Tag
lblcase1.ForeColor = Color.Red
btncase1.Visible = False
counter = counter - 1
lblnumcases.Text = counter
End If
If counter <= 0 Then                             'THIS is where im having trouble
Randomize()
randombankeroffer = Int(3 - 1 + 1) * Rnd() + 1
For Each instance As Button In btncollection
If instance.Enabled = True Then
totalremcases = totalremcases + CInt(instance.Tag)
End If
Next
bankeroffer = totalremcases / 19
bankeroffer = bankeroffer * randombankeroffer
'My goal is to: Obtain the buttons that are currently in play and add and divide
' to find average of their values then multiply by 1,2, or 3 (randombankeroffer)
DealOrNoDealPrompt.Show()
DealOrNoDealPrompt.lblprompt.Text = "The banker offers " + CStr(bankeroffer)
End If
End Sub

```

### #10 BobRodes

• Lovable Curmudgeon

Reputation: 600
• Posts: 3,069
• Joined: 19-May 09

## Re: If statement to calculate the average

Posted 09 June 2012 - 03:27 PM

Let me clear a little detail up here. I didn't realize that the Tag property was an Object (thanks Thlin); it used to be a string in VB6. The VBA Tag property is String only. Thlin, you've put in a link to the VBA Tag property, not the .Net Control.Tag property. ("Quandoque dormitat bonus Homerum", as they say.)

The link to the Tag property for a .Net Control object is here. Thlin inadvertently sent you the wrong link , which points out that you have to be a bit careful not to confuse them when using MSDN with VB.

So, if you're looking up VB.Net stuff, make sure that you're not looking at Office VBA (Visual Basic for Applications) stuff, which is much more similar to the old VB6.

### #11 driftking96

Reputation: 1
• Posts: 24
• Joined: 02-June 12

## Re: If statement to calculate the average

Posted 10 June 2012 - 09:35 AM

So am i supposed to use collection and buttons or should i loop through tags? It's a little confusing cos the MSDN sites dont have similar examples to my objectives.

### #12 BobRodes

• Lovable Curmudgeon

Reputation: 600
• Posts: 3,069
• Joined: 19-May 09

## Re: If statement to calculate the average

Posted 13 June 2012 - 11:07 AM

Ok. Start out with a collection of buttons. Tag each button with a value. When the user clicks on a button, disable it. To get the average remaining value, loop through the collection of buttons, using this logic:

If the button is disabled, do nothing.
Otherwise, add the tag value to a variable where you're accumulating values.
And, increment a counter variable.

When you're done looping, divide the accumulator variable by the counter variable, and you have your average.