11 Replies - 808 Views - Last Post: 13 June 2012 - 11:07 AM Rate Topic: -----

#1 driftking96  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • Joined: 02-June 10

Re: If statement to calculate the average

Posted 07 June 2012 - 06:33 PM

Going back to your other thread on this game:

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
Was This Post Helpful? 2
  • +
  • -

#3 driftking96  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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



Was This Post Helpful? 0
  • +
  • -

#4 driftking96  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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)
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • 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.

When last we talked about this the integer was being stored in the .Tag property.

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

Was This Post Helpful? 0
  • +
  • -

#6 driftking96  Icon User is offline

  • New D.I.C Head

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

Re: If statement to calculate the average

Posted 08 June 2012 - 05:41 PM

Quote

When last we talked about this the integer was being stored in the .Tag property.

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.
Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: If statement to calculate the average

Posted 08 June 2012 - 06:12 PM

View Postdriftking96, on 08 June 2012 - 07:41 PM, said:

Quote

When last we talked about this the integer was being stored in the .Tag property.

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

Was This Post Helpful? 1
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • 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.

Attached Image

Please tell us you aren't paying money for this course!
Was This Post Helpful? 2
  • +
  • -

#9 driftking96  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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
        If usercase = 0 Then                                  'Don't worry about this part, it works fine. :D/>
            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


Was This Post Helpful? 0
  • +
  • -

#10 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • 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.
Was This Post Helpful? 2
  • +
  • -

#11 driftking96  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • 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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1