Jump to content

Visual basic simple Case statement question


karljv

Recommended Posts

Hello

 

I have a simple question. How to have the case statement execute more than one command? My code

 

Case x = 1

                TubaTemp = ((TubaTemp) + (RadikaTemp / (TubaTemp * 3))) + SoojusKadu

 

                Rad.Enabled = True

                RadikaTemp = CDec(Rad.Text)

                TTemp.Text = TubaTemp.ToString("N2")

                RTemp.Text = RadikaTemp

 

 

So it seems tu execute only the first mathematical thingy. All the rest ( Rad.enabled = true and downward) do not execute.

 

Thanks

Okei, so i understood it completely wrong, instead of running only one line of code, it runs my entire timer1_tick code line. Why does it not select a case on every tick?

 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

 

 

        Select Case x

            Case x = 0

                TubaTemp = ((TubaTemp) + (RadikaTemp / (TubaTemp * 3))) + SoojusKadu

                If SwitchCold > TubaTemp Then

                    RadikaTemp = 40

                End If

                If SwitchWarm < TubaTemp Then

                    RadikaTemp = 30

                End If

                Rad.Enabled = False

                TTemp.Text = TubaTemp.ToString("N2")

                RTemp.Text = RadikaTemp

            Case x = 1

                TubaTemp = ((TubaTemp) + (RadikaTemp / (TubaTemp * 3))) + SoojusKadu

                Rad.Enabled = True

                RadikaTemp = CDec(Rad.Text)

                TTemp.Text = TubaTemp.ToString("N2")

                RTemp.Text = RadikaTemp

 

        End Select

 

 

    End Sub

 

 

 

taking into account that (Dim x as integer) and on Form load x=0

Case statements 'fall through', meaning that if you have a condition that matches a particular case, then that case and all cases below it will execute, unless you specify them not to.  This is the same for all languages with switch/case, not just VB.  That includes PHP.

 

So, how do you tell a case to execute without falling through to the next case?  You add a break statement to the end of that particular case's code.

Why does it not select a case on every tick?

 

taking into account that (Dim x as integer) and on Form load x=0

 

You should have a static counter inside the Tick event.

 

Declare it as :

 

Static x As Integer

 

Then, you should increment it inside your Tick event, otherwise it will always only execute one segment, because x will remain the same

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.