Question

Jake Glugla on Fri, 05 May 2017 19:41:44


Hi all, I'm doing a project for my Programming 1 class where we have to make a small game that involves the use of cases and random numbers, currently i have the code below, and I do not know how to put it out to show results from the generated numbers. Thanks, Jake

 Dim orcs As Integer
    Dim knights As Integer
    Dim dice1 As Integer
    Dim dice2 As Integer
    Dim turns As Integer
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        btnStart.Text = "Roll again"
      rnd1 as system.random
 rnd2 as system.random
 rnd1(2,13)
 rnd2(2.13)
 select case dice1
 case rnd1 = 2
 case rnd1 =3 
case rnd1 = 4
case rnd1 = 5
case rnd1 = 6
case rnd1 = 7
case rnd1=8
case rnd1=9
case rnd1=10
case rnd1=11
else rnd1=12
end select
select case dice2
     case rnd2 = 2
case rnd2 =3 
case rnd2 = 4
case rnd2 = 5
case rnd2= 6
case rnd2 = 7
case rnd2=8
case rnd2=9
case rnd2=10
case rnd2=11
else rnd2=12
end select
    End Sub

Thanks for the help :)



Sponsored



Replies

leshay on Fri, 05 May 2017 19:56:57


Hi

As this is a school project, I will show code based on your code but amended a little.

' Form1 with Button named btnStart
' and Label1 and Label2 to display results
Option Strict On
Option Explicit On
Public Class Form1
    Dim rand As New Random
    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        btnStart.Text = "Roll again"
        Dim dice1 As Integer = rand.Next(1, 7)
        Dim dice2 As Integer = rand.Next(1, 7)
        Select Case dice1
            Case 1
                Label1.Text = "1"
            Case 2
                Label1.Text = "2"
            Case 3
                Label1.Text = "3"
            Case 4
                Label1.Text = "4"
            Case 5
                Label1.Text = "5"
            Case 6
                Label1.Text = "6"
        End Select
        Select Case dice2
            Case 1
                Label2.Text = "1"
            Case 2
                Label2.Text = "2"
            Case 3
                Label2.Text = "3"
            Case 4
                Label2.Text = "4"
            Case 5
                Label2.Text = "5"
            Case 6
                Label2.Text = "6"
        End Select
    End Sub
End Class


Regards Les, Livingston, Scotland


Cor Ligthert on Fri, 05 May 2017 21:03:46


Les is this day the Man for me. 

Therefore I think it is good to show that he with his code behaves like a kind of VB6 purist. 

Yes he is from Scotland, and that makes it more difficult for me to write this because that is in Europe one of the countries from which I like very much their inhabitants because of their way of looking to the world. 

For months he is showing the audience here to use Option Infer of. 

For what. Yes he probably wears a skirt. But that does not mean we all should wear it. 

His code could be as well like this without that there was any chance of failures. 
It is only easier to write. 

' Form1 with Button named btnStart
' and Label1 and Label2 to display results
Option Strict On
Option Explicit On
Option Infer On
Public Class Form1
    Dim rand As New Random
    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        btnStart.Text = "Roll again"
        Dim dice1 = rand.Next(1, 7) 'In the runtime CLI is created an Integer for dice1
        Dim dice2 = rand.Next(1, 7) 'In the runtime CLI is created an Integer for dice2
        Select Case dice1
            Case 1
                Label1.Text = "1"
            Case 2
                Label1.Text = "2"
            Case 3
                Label1.Text = "3"
            Case 4
                Label1.Text = "4"
            Case 5
                Label1.Text = "5"
            Case 6
                Label1.Text = "6"
        End Select
        Select Case dice2
            Case 1
                Label2.Text = "1"
            Case 2
                Label2.Text = "2"
            Case 3
                Label2.Text = "3"
            Case 4
                Label2.Text = "4"
            Case 5
                Label2.Text = "5"
            Case 6
                Label2.Text = "6"
        End Select
    End Sub
End Class

Be aware a simple touch, does not make my good thoughts of Les different. 

:-)


Success
Cor





leshay on Fri, 05 May 2017 21:20:23


Hi Cor

I use Infer Off because I don't like the compiler to decide on my behalf. I make so many mistakes, I can do without any of them being hidden by the compiler deciding what type something is and I had in mind something else. I make no demands on anyone else to use what I include in the code I post. I do know that I can manage much better with those options set like that.

I suppose you prefer Infer On.

Cor Ligthert on Fri, 05 May 2017 22:44:01


Hi Cor

I use Infer Off because I don't like the compiler to decide on my behalf. I make so many mistakes, I can do without any of them being hidden by the compiler deciding what type something is and I had in mind something else. I make no demands on anyone else to use what I include in the code I post. I do know that I can manage much better with those options set like that.

I suppose you prefer Infer On.


Regards Les, Livingston, Scotland

Hi Les,

Forgive me, but I cannot understand why a reasonable guy like you thinks like this.

The compiler decides to make whatever kind of CLI code for you. The compiler decides how to optimize the code for you. There is more, but give me the freedom to leave it with that. 

And then you are afraid that the compiler infers wrong that in the case of dim myname = "I am Les" , myname is in the runtime code a string.

Be aware that Option Strict Off was made for compatibility with VB6. Option Infers On was made for more easy use of newer versions than VB8. 

I've no problem that you do it, but I see new persons who visit this forum use the same things like you do and go back to VB6 kind of programming. 


Success
Cor


leshay on Fri, 05 May 2017 22:59:55


Hi Cor

OK, you have convinced me. I have tried to force problems in code while using Infer On and so far I cannot.

'

I started using Infer Off at a time (long time ago) when I had lots of issues to resolve in code that I was having a hard time with and resolved to use those 3 options which helped me sort out the code. (not VB6 related - I only started with VB.Net). I will likely continue to define variable types myself as that is what I have become used to. Maybe I will get used to allowing the Infer to do it's thing in time.

'

From now on I will not use Infer Off. I hereby declare that I sit corrected.

'

BTW: I do not wear a skirt :)


Regards Les, Livingston, Scotland




Cor Ligthert on Fri, 05 May 2017 23:10:09


Two times success with you. 

First you helped me to overcome a problem which I had for 5 years and now I convinced you to do things easier

I take a Scotch (sorry it is JW) and drink on the health of you.

  


Proost
Cor



leshay on Fri, 05 May 2017 23:16:53


Two times success with you. 

First you helped me to overcome a problem which I had for 5 years and now I convinced you to do things easier

I take a Scotch (sorry it is JW) and drink on the health of you.

  


Proost
Cor



Hi Cor

For many many years I worked in various countries of the world and I always used JW (the Black variety). But, here in Scotland I couldn't buy it - it was made here but only for export.

Acamar on Fri, 05 May 2017 23:51:44


I've no problem that you do it, but I see new persons who visit this forum use the same things like you do and go back to VB6 kind of programming.

Uh?

 dim myname = "I am Les" is VB6 style.

 Dim myname As String = "I am Les" is .Net style.   There may be reasons for preferring one to the other, but if you suggest that people should not go back to the VB6 kind of programming, then you must choose the latter.

leshay on Sat, 06 May 2017 00:08:08


I've no problem that you do it, but I see new persons who visit this forum use the same things like you do and go back to VB6 kind of programming.

Uh?

 dim myname = "I am Les" is VB6 style.

 Dim myname As String = "I am Les" is .Net style.   There may be reasons for preferring one to the other, but if you suggest that people should not go back to the VB6 kind of programming, then you must choose the latter.

Hi

I can see both point of view, but it seems to me that using Infer On and continuing using the typing of variables would be the best of both worlds. Which is what I'll probably do now.

dbasnett on Sat, 06 May 2017 12:11:48


My start was assembler so using Infer On is alien to me.  Infer On does save a little bit of typing, and does make one more knowledgeable about type characters.  But that is all it does. 

It does introduce the need for some kludgy things, like using bytes and Nullable's that are null or you can revert to the As syntax.

bytes

        Dim b1 = {0} 'not a byte array
        Dim b2 = {CByte(0)} 'is a byte array
        Dim b3() As Byte = {0} 'is a byte array

nullable integer null

        Dim i1 = If(True, New Nullable(Of Integer), 0)
        Dim i2 As Nullable(Of Integer)
        If Not i1.HasValue Then Stop
        If Not i2.HasValue Then Stop

And then there are hidden features(traps) when there is no initialization.  Say I have an application that reads integers from a database and totals them.  Works great at first.

'init so type is correct
Dim IntsReadFromDB = {1} '             
'do the read populating IntsReadFromDB
'simulate small read
IntsReadFromDB = {1, 2, 3, 4}
Dim total
For Each number In IntsReadFromDB
    total += number
Next


But over time the size of the reads grows to millions of rows.  Obviously the loop becomes slow because total is Object. Infer allowed it and I didn't initialize it.  If you use Infer then you better be using initialization.

For me Strict and Explicit will remain On and Infer will be Off, but to each their own.


"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

Cor Ligthert on Sat, 06 May 2017 12:25:12


My start was assembler so using Infer On is alien to me.  Infer On does save a little bit of typing, and does make one more knowledgeable about type characters.  But that is all it does. 

It does introduce the need for some kludgy things, like using bytes and Nullable's that are null or you can revert to the As syntax.

bytes

        Dim b1 = {0} 'not a byte array
        Dim b2 = {CByte(0)} 'is a byte array
        Dim b3() As Byte = {0} 'is a byte array

nullable integer null

        Dim i1 = If(True, New Nullable(Of Integer), 0)
        Dim i2 As Nullable(Of Integer)
        If Not i1.HasValue Then Stop
        If Not i2.HasValue Then Stop

and then there are hidden features when there is no type specifier.  Say I have an application that reads integers from a database and totals them.  Works great at first.

'init so type is correct
Dim IntsReadFromDB = {1} '             
'do the read populating IntsReadFromDB
'simulate small read
IntsReadFromDB = {1, 2, 3, 4}
Dim total
For Each number In IntsReadFromDB
    total += number
Next


But over time the size of the reads grows to millions of rows.  Obviously the loop becomes slow because total is Object because Infer allowed it and I didn't initialize it.

For me Strict and Explicit will remain On and Infer will be Off, but to each their own.


"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


My first program language was as well Assembler.

My first editor was a kind of Line Editor. 

Currently I use Word with spelling tools in all languages which I use.

In VB I have option Infer On. 

Option Infer is simple a word editor which is not anymore constantly telling that something is wrong written but directly make the change if there is only one possibiolity (although it does not show the type which is initialized).

The sample code which you gave does not run with Option Infer On it gives already an error at the moment you enter the code.

In this case there is simply nothing to infer, so that is told. 

Be aware Option Infer and Option Strict are as different as Apples and Computers. 

Option Infer is a kind of spelling corrector, while Option strict is made for compatibility with VB6 and tries to find the right type at runtime.

How is it going with the water by the way?


Success
Cor





dbasnett on Sat, 06 May 2017 12:40:21


Don't know why the difference but with Option Infer On, Strict Off, Explicit either, I did not get the red squiggly line.  VS2012 and I am quite aware of what the Options do.

The Osage River is back in it's bank.  Mudflats ;)


"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

Cor Ligthert on Sat, 06 May 2017 13:44:18


Don't know why the difference but with Option Infer On, Strict Off, Explicit either, I did not get the red squiggly line.  VS2012 and I am quite aware of what the Options do.

The Osage River is back in it's bank.  Mudflats ;)


"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

About the river, that is great, I hope you have not to much damage with furniture.  

About the difference, that is obvious. 

Option Strict Off allows late binding and therefore "total" is made an object which is bound at runtime. 

It still is the same, there cannot be anything inferred it is not even used. 

If you use Option Infer On, you should simply always set Option Strict On or better all Options to On.


Success
Cor