r/visualbasic • • Oct 23 '19

VB.NET Help Coin flipping program

Post image
5 Upvotes

8 comments sorted by

1

u/C4_Ghost Oct 23 '19

So Ive been trying to add a count of how many times heads and tales have been flipped. I'm sure its probably simple but Ive been at this for about 10 hours. Any help would really be appreciated.

4

u/sa_sagan VB.Net Master Oct 23 '19

You need to add two new integer variables outside of Button1_Click.

Then when tails, add 1 to the tails integer, and when heads, add 1 to the heads integer.

I have assumed this is homework so I won't write the code for you, but that should give you an idea of what to do.

1

u/C4_Ghost Oct 23 '19

Ok thanks. Ill take a look.

1

u/C4_Ghost Oct 23 '19

Wow..... How come they have to be global variables in order to count?

3

u/sa_sagan VB.Net Master Oct 23 '19

Because once the Button1_Click event has finished, every value inside of there will cease to exist.

Therefore you store your counting variables elsewhere, so you can reference them inside of Button1_Click and increase their values without losing them when it's finished.

1

u/clarinetJWD Oct 23 '19

Those aren't globals. You can even declare them Private, in which case they're class variables. However, variables within a sub will always be disposed and recreated when you exit the sub (out of scope).

You can actually declare the variables in the sub, if you say "Static countHeads" instead of Dim. Static means that they won't be reset each time the sub it called.

2

u/1d3nt Oct 24 '19

The posts in this thread are shocking. People who question if a post is a homework are 99% of the time new programmers who hide behind th fact they cannot give an answer. ABitOffTheTop comment is one of the worst pointless replies I have seen in my 20+ years of programming. I have never seen such a stupid reply saying "left a bug". Ignoring the fact the code is terrible. What that translates to is "Iv posted you an example but it does not quite work, so I will pretend I am giving you a task" Urgh just urgh. If you can't help a user don't. "Still cheating though", what with that crap?

Ignoring this typical crap help from members who have likely been writing Visual Basic a couple weeks lets focus on a few issues.

Public Class Form1

    Private Shared ReadOnly Rng As New Random

    Private Sub Button1_Click() Handles Button1.Click
        Dim isHeads = Rng.Next() Mod 2 = 0
        PictureBox1.Visible = isHeads
        PictureBox2.Visible = Not isHeads

        ' now what is the next steps?
    End Sub
End Class

0

u/ABitOffTheTop Oct 23 '19

I've left a fairly obvious bug in there, so best not submit it immediately if it is classwork. If you realise what the bug is, then you probably know enough to submit it. Still cheating though, but least you had to work for it..

 Dim NumberOfFlips As Integer = 1000
 Dim Heads As Integer
 Dim rnd As New Random
 Enum Coin
     Heads
     Tails
 End Enum

Sub Main()
 Dim CoinFlip = Function()
   If (rnd.Next(2) = 1) Then
    Return Coin.Heads
   Else
    Return Coin.Tails
   End If
 End Function

 Dim CoinFlipRecorder = Function()
   If (CoinFlip() = Coin.Heads) Then
    Heads = Heads + 1
   End If
   Return CoinFlip()
 End Function
 
 For i = 1 To NumberOfFlips
   Console.Write(CoinFlipRecorder() + " ")
 Next
 Console.WriteLine(Convert.ToString(NumberOfFlips) + " Coin Flips, " _
  + Convert.ToString(Heads) + " were heads and " _
  + Convert.ToString(NumberOfFlips - Heads) + " were tails")

End Sub