Results 1 to 8 of 8

Thread: Textbox with a mind of its own

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Location
    Lincoln, UK
    Posts
    14

    Angry 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

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Textbox with a mind of its own

    Goes to show that I can be slow, too.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Location
    Lincoln, UK
    Posts
    14

    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...

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Location
    Lincoln, UK
    Posts
    14

    Re: Textbox with a mind of its own

    Now you're just being pedantic.

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Textbox with a mind of its own

    Quote Originally Posted by Mooncinder View Post
    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
  •  



Click Here to Expand Forum to Full Width