|
-
Apr 7th, 2009, 06:14 PM
#1
Thread Starter
New Member
Textbox with a mind of its own
Hi, I'm using VB.NET 2008 Express Edition and I'm trying to create a simple program that will roll a number of dice I choose. It does this by generating a random number within a specified range(I'm rolling 3-sided dice in this case so I'm generating numbers between 1 and 3) and adding each number to a textbox to create a total of all the dice rolled so far. This is my code:
Code:
Private Sub butRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butRoll.Click
Dim RandNum, i As Integer
Dim RandomClass As New Random()
'if at least one dice is rolled
If txtD3.Text > 0 Then
For i = 0 To txtD3.Text 'roll the dice and add to total
'generate a number > or equal to 1 and < 4
RandNum = RandomClass.Next(1, 4)
txtLastRoll.Text = RandNum 'last number generated
txt1.Text = txtTotal.Text 'whats the value of txtTotal?
'add latest dice roll to total
txtTotal.Text = txtTotal.Text + RandNum
txt2.text = txtTotal.Text 'whats the value of txtTotal now?
Next i 'roll the next dice
End If
End Sub
Now the problem I am having is that although my random number generator is generating the right numbers (confirmed by checking the txtLastRoll textbox), when I add it to txtTotal, it seems to be getting another number from somewhere and adding that to my random number then adding it to the total and I have no idea how this is possible! txtTotal always starts as 0 but txt1 is not showing that. Normally I would just set txtTotal to 0 there but if I do that, I won't be able to add the numbers together. I've tried finding a pattern in the mystery numbers appearing in txtTotal but they just seem to be random. Any ideas would be great as this has me completely stumped.
Thanks in advance,
Mooncinder
-
Apr 7th, 2009, 06:34 PM
#2
Re: Textbox with a mind of its own
This just goes to show why you shouldn't use text boxes to hold numbers for calculations.
It's also a good example of why Option Strict should be turned on.
try this:
Code:
Private Sub butRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butRoll.Click
Dim RandNum, i As Integer
Dim RandomClass As New Random()
dim rollCount as Integer = CInt(txtD3.Text) 'But only if you can guarantee it's a number... other wise use TryParse...
dim rollTotal as Integer = 0 'Initializes the counter
'if at least one dice is rolled
If txtD3.Text > 0 Then
For i = 0 To rollCount 'roll the dice and add to total -- you do realize you get an extra roll here, right? If I enter 3.... it'll go from 0 to 3... which is 4 iterations..
'generate a number > or equal to 1 and < 4
RandNum = RandomClass.Next(1, 4)
'txtLastRoll.Text = RandNum 'last number generated <-- honestly... this will only ever keep the last number rolled, so what's the point?
txt1.Text = rollTotal.ToString 'whats the value of txtTotal?
'add latest dice roll to total
rollTotal += RandNum
txt2.text = rollTotal.ToString 'whats the value of txtTotal now?
Next i 'roll the next dice
End If
End Sub
-tg
-
Apr 7th, 2009, 06:35 PM
#3
Re: Textbox with a mind of its own
Turn Option Strict ON and fix the errors that arise and you will have better, faster, code that will probably solve the problem. The plus sign can act to add number or to concatenate strings. Since you are taking a string and using + to an integer, what is going to happen? Will the integer be converted to a string and concatenated onto the existing string, or will the existing string be converted to an integer and added to the other integer before being converted back to a string? You are assuming that it will work one way...and it's not. Option Strict would avoid that by requiring you to do the correct conversion explicitly, rather than letting the compiler decide which implicit conversion to do. Option Strict also results in faster code.
By the way, there is no such thing as a three-sided die. A two-sided die is a coin, and a four-sided die is the next one up. A three-sided would be irregular, which would mean that there would not be an even probability for each of the numbers.
My usual boring signature: Nothing
 
-
Apr 7th, 2009, 06:35 PM
#4
Re: Textbox with a mind of its own
Goes to show that I can be slow, too.
My usual boring signature: Nothing
 
-
Apr 8th, 2009, 09:47 AM
#5
Thread Starter
New Member
Re: Textbox with a mind of its own
Oh dear, you can tell it's been a long time since I tried programming in VB. I think last time I was using VB6 (yes, that long ago...) and I'd never needed to worry about option strict before. That seems to have fixed it though. Thanks for replying.
Oh, and when I said 3-sided dice, I actually meant rolling a D6 and halving the result. I guess I was rushing a bit last night...
-
Apr 8th, 2009, 10:46 AM
#6
Re: Textbox with a mind of its own
Actually... rolling a D3 isn't halving the result of a D6 ... it's rolling a D6 and using 1,2 = 1; 3,4 = 2; 5,6 = 3 .... but that's splitting hairs... 
-tg
-
Apr 8th, 2009, 11:10 AM
#7
Thread Starter
New Member
Re: Textbox with a mind of its own
Now you're just being pedantic.
-
Apr 8th, 2009, 11:12 AM
#8
Re: Textbox with a mind of its own
 Originally Posted by Mooncinder
Now you're just being pedantic. 
Its one of his more sterling qualities.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|