[RESOLVED] Slots Game help (not a school project its for a personal one) Neg Bal issues-VBForums
Results 1 to 23 of 23

Thread: [RESOLVED] Slots Game help (not a school project its for a personal one) Neg Bal issues

  1. #1

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    794

    Resolved [RESOLVED] Slots Game help (not a school project its for a personal one) Neg Bal issues

    I am wanting to end the game if the balance is less than 0 (mymoney I think its textbox5)


    I have tried a Else if statement didn't work But I got a stackoverflow error something like that didn't catch it as I reverted to avoid any data loss (VS is strange sometimes when you get errors screws the whole thing up)


    Code:

    Code:
    Public Class Slots
    
    
        Dim myMoney As Integer = 10000000, myBid As Integer = 5000
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox5.Text = myMoney
            TextBox4.Text = myBid
        End Sub
    
        Private Sub Spin_Click(sender As Object, e As EventArgs) Handles Spin.Click
            If (Integer.Parse(TextBox4.Text) >= Integer.Parse(TextBox5.Text)) Then
                MsgBox("You can not bid more money than you have!")
            ElseIf (Integer.Parse(TextBox5.Text) <= 0) Then
                NewGame()
    
            Else
    
                myBid = Integer.Parse(TextBox4.Text)
                updateSlots()
                checkSlots()
                TextBox5.Text = myMoney
            End If
        End Sub
        Private Function updateSlots()
            Dim rand As Random = New Random()
            Dim slots As New List(Of TextBox)
            slots.Add(Slot1)
            slots.Add(Slot2)
            slots.Add(Slot3)
            For i As Integer = 0 To 2
                Dim r As Integer = rand.Next(9)
                slots(i).Text = r
                r = Nothing
            Next
        End Function
        Private Function checkSlots()
            Dim t1 As Integer = Slot1.Text, t2 As Integer = Slot2.Text, t3 As Integer = Slot3.Text
            If ((t1 = t2) And t2 = t3) Then
                Status.Text = "70X Money!!!"
                myMoney += (myBid * 70)
            ElseIf ((t1 = t2) Or (t1 = t3) Or (t2 = t3)) Then
                Status.Text = "700X Money! BIG WIN!"
    
                myMoney += (myBid * 700)
    
            Else
                Status.Text = "Bid Lost."
                myMoney -= myBid * 200
    
            End If
        End Function
    
        Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
            Me.Close()
        End Sub
    
        Private Sub Slots_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            MsgBox("Final score" + vbNewLine + "$" + TextBox5.Text)
    
    
        End Sub
    
        Private Sub NewGameToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewGameToolStripMenuItem.Click
            Slot1.Clear()
            Slot2.Clear()
            Slot3.Clear()
            TextBox5.Text = myMoney
            TextBox4.Text = myBid
            Status.ResetText()
        End Sub
       
      
       
    End Class

    ScreenShot:


    http://screenshot.sh/oAFfBDqpdFgpj

    As you can see it gives a negative balance, I want to prevent this. Not really sure on how to do it. Someone brainstorm with me I worked on this for about 2 hours and it annoyed me so I walked away for a bit, now I am back, and ready to go but need ideas.

    **Disclaimer**

    Been coding this entire project (that this is part of) for nearly 2 years, and this one is one of the first ones i added for the arcade i added. I recently increased variables for winnings, and what happens when you lose (which now I think is where the neg. balance comes in?!) so yea.
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  2. #2

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    794

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    Update:

    Error I get
    Code:
    An unhandled exception of type 'System.OverflowException' occurred in BeffsBrowser.exe
    
    Additional information: Arithmetic operation resulted in an overflow.
    I removed the multiplier for loss, and reduced the "Winning multiplier"

    Still get this error during some occasions.
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    95,198

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    What line is the exception thrown on and what values are in use when it does? The error message simply means that you're performing a mathematical operation that produces a result too big for the data type, e.g. adding Integer.MaxValue and any other Integer value. If the value you're getting is actually valid then you need to use a data type with greater range, e.g. Long instead of Integer.

    2007-2016

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    794

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    Alright thanks! I fixed the negative results, but the other error imentioned is now thrown when i hit "Bet"

    I will look into changing it to long instead of integer see if that helps it.

    The Max limit for the text box is set at 999,999 just so I rule out any issues with the textbox
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    29,954

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    You shouldn't be creating a new Random object in the function call, as you are. It may be fine, in this case, but it's a risky practice that should be avoided in general. The problem is that new Random objects are seeded with the current system time. If you create two within the same second, they'll have the same seed, and will produce the exact same sequence of numbers, so if you were to call that function too quickly, your random numbers would be anything but random.

    The only safe way to create a Random object is one, for the duration of the program, though one at form scope is usually good enough.

    As to the overflow, when the exception occurs, look at the line. If your max bet is effectively a million, then you've got a long ways to go to overflow an Integer (about 2 billion). I see you've got some hefty multipliers in there, though. The big win would produce about 700 million, and it wouldn't take much at that point to top 2 billion.

    On the other hand, slots are always kind of a gamble.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    794

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    Quote Originally Posted by Shaggy Hiker View Post
    You shouldn't be creating a new Random object in the function call, as you are. It may be fine, in this case, but it's a risky practice that should be avoided in general. The problem is that new Random objects are seeded with the current system time. If you create two within the same second, they'll have the same seed, and will produce the exact same sequence of numbers, so if you were to call that function too quickly, your random numbers would be anything but random.

    The only safe way to create a Random object is one, for the duration of the program, though one at form scope is usually good enough.

    As to the overflow, when the exception occurs, look at the line. If your max bet is effectively a million, then you've got a long ways to go to overflow an Integer (about 2 billion). I see you've got some hefty multipliers in there, though. The big win would produce about 700 million, and it wouldn't take much at that point to top 2 billion.

    On the other hand, slots are always kind of a gamble.
    Oh I know - I honestly don't know what you mean by the other method though example maybe? (it would be helpful if it would make it more effecient)

    And yep, that was the goal to make it "bigger" and better.


    Changing it from integer to long helped the error. As the number was simply too big for that to execute.
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    29,954

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    I don't know what I mean by the other method, either. Where did I mention that? Are you talking about where to create a Random object?
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    794

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    Let me rephrase, can you explain a "less risky" way to randomly generate numbers and have them really random? Like really make it a "slots" game - and have the odds of winning smaller?

    Can you explain what other options there are as far as generating numbers?

    Thanks!
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  9. #9

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    794

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    And yea thats what i meant you was talking about it, and I don't quite understand it. I know the random function is NOT the best practice and I really don't like using it, I just never came across a safer alternative.
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    95,198

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    All you need to do is create a single Random object and reuse it. Just like any other type, that means assigning an instance to a variable declared outside of any method.

    You could use the random number generator in the Cryptography namespace but there's no point. That would make the sequence of numbers generated less predictable but it won't change the distribution and thus won't change the chance of winning.

    2007-2016

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    794

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    Quote Originally Posted by jmcilhinney View Post
    All you need to do is create a single Random object and reuse it. Just like any other type, that means assigning an instance to a variable declared outside of any method.

    You could use the random number generator in the Cryptography namespace but there's no point. That would make the sequence of numbers generated less predictable but it won't change the distribution and thus won't change the chance of winning.
    Ha!

    was doing more digging seen where someone asked this almost 12 Years ago It helps a lot to understand what you really mean by reusing.

    The old thread (Dated Aug 7 2006)
    http://www.vbforums.com/showthread.p...ESOLVED-random

    It is a less straight forward as vb.net was still SOOOO young then but it does give me a grip of understanding
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  12. #12

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    794

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    Alright this is what I came up with - with a help of a stackoverflow question - and example.


    Code:
    Public Class Slots
    
    
        Dim myMoney As Integer = 10000000, myBid As Integer = 5000
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox5.Text = myMoney
            TextBox4.Text = myBid
        End Sub
    
        Private Sub Spin_Click(sender As Object, e As EventArgs) Handles Spin.Click
            If (Integer.Parse(TextBox4.Text) >= Integer.Parse(TextBox5.Text)) Then
                MsgBox("You can not bid more money than you have!")
            ElseIf (Integer.Parse(TextBox5.Text) <= 0) Then
                NewGame()
    
            Else
    
                myBid = Integer.Parse(TextBox4.Text)
                updateSlots()
                checkSlots()
                TextBox5.Text = myMoney
            End If
        End Sub
        Private Function updateSlots()
            Dim rand As Random = New Random(DateTime.Now.Millisecond * DateTime.Now.Second * DateTime.Now.Minute *DateTime.Now.Hour)
    
            Dim slots As New List(Of TextBox)
            slots.Add(Slot1)
            slots.Add(Slot2)
            slots.Add(Slot3)
            For i As Integer = 0 To 2
                Dim r As Integer = rand.Next(9)
                slots(i).Text = r
                r = Nothing
            Next
        End Function
        Private Function checkSlots()
            Dim t1 As Integer = Slot1.Text, t2 As Integer = Slot2.Text, t3 As Integer = Slot3.Text
            If ((t1 = t2) And t2 = t3) Then
                Status.Text = "70X Money!!!"
                myMoney += (myBid * 70)
            ElseIf ((t1 = t2) Or (t1 = t3) Or (t2 = t3)) Then
                Status.Text = "700X Money! BIG WIN!"
    
                myMoney += (myBid * 700)
    
            Else
                Status.Text = "Bid Lost."
                myMoney -= myBid * 200
    
            End If
        End Function
    
        Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
            Me.Close()
        End Sub
    
        Private Sub Slots_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            MsgBox("Final score" + vbNewLine + "$" + TextBox5.Text)
    
    
        End Sub
    
        Private Sub NewGameToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewGameToolStripMenuItem.Click
            Slot1.Clear()
            Slot2.Clear()
            Slot3.Clear()
            TextBox5.Text = myMoney
            TextBox4.Text = myBid
            Status.ResetText()
        End Sub
       
      
       
    End Class
    I highlighted the difference in red. I tested it, seems the winning was dimmed down as it was previously.
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  13. #13

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    794

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    I also updated this function:

    Code:
     Private Function checkSlots()
            Dim t1 As Long = Slot1.Text, t2 As Long = Slot2.Text, t3 As Long = Slot3.Text
            If ((t1 = t2) Or (t1 = t3) Or (t2 = t1) Or (t2 = t3) Or (t3 = t1) Or (t3 = t2)) Then
                Status.Text = "70x X Money!!!"
                myMoney += (myBid * 70)
            ElseIf ((t1 = t2) And (t1 = t3) And (t2 = t3)) Then
                Status.Text = " 700 X Money! BIG WIN!"
    
                myMoney += (myBid * 700)
            Else
                Status.Text = "Bid Lost."
                myMoney -= myBid
    
    
            End If
    
        End Function
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    29,954

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    I don't think that change made a bit of difference. Declare rand outside of any method and you don't have to bother with providing a seed. ANY seed is as good as any other as long as you are not creating new random objects too quickly. By creating just one, and only using that one, then you certainly won't be creating new random objects too quickly, because there will be only one.

    So, move rand out to a form level variable and dispense with that seed stuff.

    As to the second snippet, there are a couple things to do:

    1) Turn Option Strict ON for the project (Project | Properties on the Compile tab), and fix all the resulting errors. Those are real errors, whether they are causing any crashes, or not. All those implicit conversions are slower than explicit conversions. The difference is small, but it will add up over time. Also, implicit conversions hide bugs.

    2) Use OrElse and AndAlso rather than Or and And. This will improve performance slightly, since the former pair cause the if statement to return as soon as the truth of the whole statement is evaluated. For example:

    If A Or B Then

    This will evaluate both A and B (whatever they are). Of course, if A is True, then it doesn't actually matter what B is, because the If will be true anyways, so evaluating B is a waste of time.

    If A OrElse B Then

    This will evaluate B ONLY if A is False. If A is True, then B isn't even considered. This is faster in every situation.
    My usual boring signature: Nothing

  15. #15
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    95,198

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    Quote Originally Posted by Shaggy Hiker View Post
    So, move rand out to a form level variable and dispense with that seed stuff.
    Exactly. I said this in post #10:
    All you need to do is create a single Random object and reuse it. Just like any other type, that means assigning an instance to a variable declared outside of any method.
    and this in that other thread:
    You should just create a single Random object and reuse it but there is no need to specify a seed.
    but apparently both were ignored.

    2007-2016

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    794

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    1) Turn Option Strict ON for the project (Project | Properties on the Compile tab), and fix all the resulting errors. Those are real errors, whether they are causing any crashes, or not. All those implicit conversions are slower than explicit conversions. The difference is small, but it will add up over time. Also, implicit conversions hide bugs.
    Never understood the option strict thing what does it do and how will it impact my project of over at LEAST 200k lines and AT LEAST 40 Forms that I really don't want to be messed up if i were to change those kinda settings.

    Next, thanks for the tip. I will def change that.

    I am no longer getting errors, I am only adjusting the rand as suggested. (or at least partially)

    The numbers seem to be more random, and really there is no "value" in the feature, its just something to show I know to code lol.... quite simple just makes my family and friends realize how much time i have invested into learning programming
    Last edited by jdc20181; Mar 19th, 2017 at 08:37 PM.
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  17. #17
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    95,198

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    Quote Originally Posted by jdc20181 View Post
    Never understood the option strict thing what does it do and how will it impact my project of over at LEAST 200k lines and AT LEAST 40 Forms that I really don't want to be messed up if i were to change those kinda settings.
    Option Strict On means that strict typing is implemented, which means that late-binding is not permitted and implicit narrowing conversions are not permitted. Basically, you need to make sure that variables and references are the type you're trying to use them as. For example, you can't do this:
    Code:
    Dim n As Integer = 100
    
    MessageBox.Show(n)
    but rather must do this:
    Code:
    Dim n As Integer = 100
    
    MessageBox.Show(n.ToString())
    because MesageBox.Show expects a String. Likewise, you cannot do this:
    Code:
    Dim row As DataRow = table.Rows(0)
    Dim n As Integer = row("Count")
    but rather must do this:
    Code:
    Dim row As DataRow = table.Rows(0)
    Dim n As Integer = CInt(row("Count"))
    because you can't assign an Object reference to an Integer variable. You also can't do this:
    Code:
    Dim row As DataRow = table.Rows(0)
    Dim length As Integer = row("Name").Length
    but must rather do this:
    Code:
    Dim row As DataRow = table.Rows(0)
    Dim length As Integer = CStr(row("Name")).Length
    because you can't get the Length of a String via an Object reference.

    If you turn it On in your project now then any place that you have used late-binding or an implicit narrowing conversion will be flagged as a compilation error. If you've written good code then that will be nowhere, but that's probably not going to be the case. If you don't want to do it in this project - and even if you do - you should turn Option Strict On in the IDE options so that it will be On by default in all future projects.

    Even if you write the exact same code with Option Strict On and Off, there will be a small (although probably unnoticeable to the user) improvement in execution speed of you code as there will be less type-checking. The main benefit is that it draws your attention to situations that a cast or conversion is required and thus encourages you to think about how that should be done, rather than just assuming that the system will do it the best way when that may not be ideal or even possible. Having Option Strict On won't prevent you making errors but it will help you avoid some obvious ones if you pay attention to what it tells you. It may seem like extra work to perform those casts and conversions but, when you've been doing it for a while, it becomes second nature and no burden at all.

    2007-2016

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  18. #18
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    29,954

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    To extend what JMC said to your particular case: If you have a large enough program, with enough implicit conversions (you have a good number), then when you turn Option Strict ON, you will get a LOT of compiler errors. This may surprise you, because the code was running fine. Well....it may be running fine, but it isn't running efficiently.

    I did this exact thing at one point, on a program that was a single loop that took about three days to complete. There were a LOAD of implicit conversions, too, mostly the fairly innocent conversion of a string to an integer which you have in the code you showed:
    Code:
    Dim t1 As Long = Slot1.Text
    You may KNOW that this will always work, so the change to make it an explicit conversion is this:
    Code:
    Dim t1 as Long = CLng(Slot1.Text)
    Seems like a waste, but it isn't. When I went through my program and made all the changes to get rid of all the implicit conversions, the run time dropped from three days to two. That's a pretty significant improvement, and shows the potentially large cost for implicit conversions. Of course, each conversion of the type shown will take so little time that you'd have to do tens or hundreds of thousands of them to be able to see the difference, but over the course of a large enough program, it will add up.

    And then there are the much more subtle bugs. If the conversion from implicit to explicit isn't totally obvious...then you've found a bug waiting to bite you. If you turn Option Strict ON, and find an error where the solution isn't quick and simple, then that will be trouble down the road.
    My usual boring signature: Nothing

  19. #19

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    794

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    Interesting (I went to bed after all these replies lol I didn't get a chance to read them)

    I understand entirely.... But the work that would be required would almost be over remarkable.... and then i also feel like i would flood the forum, because I don't know how to reroute something that I learned one way but now wants me to do it 3 steps more another way...

    I just don't know if I want to take on the hefty task...

    But for future projects I will def. use option strict. And try to improve my code effeciency. I have been trying to do that, as I tooka bout a 6 month break from vb.net to learn some other languages, all of which are web programming much different but much a like VB.net which I love that.

    Anyhow - I understand why, but I don't understand how... meaning I haven't really messed with option strict enough to find a split second different way of doing something that is more accurate code conversations.

    But I will be doing some googling and learning on the subject... Please understand everything I know is self-taught (or told by one of you guys which i appreciate) and it takes longer when you are self-taught, because well.. I kinda have to google anything I want to do or learn... it isn't in a giant text book and taught day to day by a intstructor.

    Anyways, thanks for the advice. Defiently will help me down the road.

    like I said, I may not do it for this project (as it could cause unwanted results or too many errors, and some unsolvable or somethign that doesn;t work with strict on, for whatever reason) but will research using it, and use it in any future projects.
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  20. #20

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    794

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    oh and I think this is resolved for now...Keep giving me advice im listening even though it may not seem that way xD
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  21. #21
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    29,954

    Re: [RESOLVED] Slots Game help (not a school project its for a personal one) Neg Bal

    Actually, Option Strict isn't all or nothing. You can turn it on, fix a few things, then turn it off again. It's nothing more than a checkbox, and every change you make to get rid of a problem with it on is always going to be valid code with it off. So, you don't have to fix it all in one step, but can do so bit by bit (well, byte by byte, anyways).
    My usual boring signature: Nothing

  22. #22
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    95,198

    Re: Slots Game help (not a school project its for a personal one) Neg Bal issues

    Quote Originally Posted by jdc20181 View Post
    i also feel like i would flood the forum, because I don't know how to reroute something that I learned one way but now wants me to do it 3 steps more another way...
    The VAST majority of the issues flagged by turning Option Strict On will be of the same basic type and will take the addition of a single operator or method call to fix so there will be no additional lines of code and, if you're flooding the forum with questions about such issues then you're not listening to the answers because they will be the same in every case. It's almost always going to be the case that simply need to add a cast or conversion as we've already shown.

    In a small number of cases, it may be that you are assuming that data will be able to be converted to a specific type when it may not be. Those are the most important issues to find because they are the ones that may lead to exceptions and possibly crashes after deployment. In those cases, you may need to add a few lines of extra code but it's very important that do and there's no better time to learn how. In such cases, it's generally going to be that, instead of converting data and using it, you need to validate it first and then only convert and use it if it indeed valid. An example that is commonly encountered is the user entering a numeric value in a TextBox. In such cases, developers may just assume that valid data has been entered and use it as a number. With Option Strict Off, that might look like this:
    vb.net Code:
    1. resultTextBox.Text = firstNumberTextBox.Text * secondNumberTextBox.Text
    With Option Strict On, you may think that this is enough of a change:
    vb.net Code:
    1. resultTextBox.Text = (CInt(firstNumberTextBox.Text) * CInt(secondNumberTextBox.Text)).ToString()
    but it's not because you have no guarantee that the user has entered anything in either TextBox or, if they have, that it's a valid number. In such cases, you should validate and only convert and use the data if it is valid and do whatever is appropriate otherwise, which will probably involve notifying the user:
    vb.net Code:
    1. Dim firstNumber As Integer
    2. Dim secondNumber As Integer
    3.  
    4. If Integer.TryParse(firstNumberTextBox.Text, firstNumber) AndAlso
    5.    Integer.TryParse(secondNumberTextBox.Text, secondNumber) Then
    6.     resultTextBox.Text = (firstNumber * secondNumber).ToString()
    7. Else
    8.     MessageBox.Show("Please enter valid integers.")
    9. End If
    There's really no more to it. If you know the data is the appropriate type then cast it. If you know the data can be converted to the appropriate type then convert it. If you don't know if the data can be converted to the appropriate type then validate it and then convert it.

    2007-2016

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  23. #23

    Thread Starter
    Fanatic Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    794

    Re: [RESOLVED] Slots Game help (not a school project its for a personal one) Neg Bal

    hmm... I always add if then statements in the situation you presented above^

    Thanks...As I said I will most certainly take a look at option strict and what differences there is...
    Disclaimer: When code is given for example - it is merely a example. See my First CodeBank Submission Upgrading to the newest IE when using the Webbrowser control See my Second CodeBank SubmissionVB.NET - Recording History or changes using WebbrowserControl RichTextBox Textbox
    See my Third codebank submission VB.NET - Detecting Secure/Insecure Websites (ssl) w/ Webbrowser control See CodeBank Submission 4 A Simple Large View Calendar Control See my browser project BeffsBrowser Try my SpeedTest Project

    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.