Using Select case statement
Using Select case statement
Using Select case statement
Using Select case statement
Using Select case statement
Using Select case statement Using Select case statement Using Select case statement Using Select case statement Using Select case statement Using Select case statement Using Select case statement Using Select case statement
Using Select case statement Using Select case statement
Using Select case statement
Go Back  Xtreme Visual Basic Talk > > > Using Select case statement


Reply
 
Thread Tools Display Modes
  #1  
Old 06-03-2011, 02:16 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default Using Select case statement


Hi, I just replaced a different segment of code with the select case statement and it works fine. However I don't understand what the dim is for after you do "Select case [variable name here]"

I currently just dimmed a value as nothing. I didn't specify any data type for my variable I'm using for this case statement. But it works.


now with this code, it's a different operation I'm trying to make into a case:


Code:
        If e.KeyCode = 87 Then
            P2movement.Enabled = True ' P2 movement 
            P2_basic_score.Enabled = True ' starts basic score (adds 1 every 90 intervals)
            Player2_Speed_Y = -10
            Player2_Speed_X = 0

        End If


        If e.KeyCode = 83 Then
            P2movement.Enabled = True
            P2_basic_score.Enabled = True
            Player2_Speed_Y = 10
            Player2_Speed_X = 0

        End If


        If e.KeyCode = 68 Then
            P2movement.Enabled = True
            P2_basic_score.Enabled = True
            Player2_Speed_Y = 0
            Player2_Speed_X = 10

        End If

        If e.KeyCode = 65 Then
            P2movement.Enabled = True
            P2_basic_score.Enabled = True
            Player2_Speed_Y = 0
            Player2_Speed_X = -10

        End If
I tried implementing a select case statement, however the logic doesn't follow through. i tested it with a case statement and it just went in one direction. with IF / end if statements it works fine, follows correct directions /logic.

I'm new to case statements, any help would be appreciated. Thank you.
Reply With Quote
  #2  
Old 06-03-2011, 02:50 AM
DrPunk's Avatar
DrPunkUsing Select case statement DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

I don't understand your question about 'what the dim is for after you do "Select case [variable name here]"'. Dim just declares a variable to use. If you don't need a variable then you don't need to dim anything.

Code:
Select Case e.KeyCode
    Case 87
        ' Code for keycode 87 here
    Case 83
        ' Code for keycode 83 here
    Case 68
        ' Code for keycode 68 here
    Case 65
        ' Code for keycode 65 here
End Case
That should work for you. Is that how your case looks?

It's probably also worth noting that you can use Keys instead of the numbers. So you can have Keys.A instead of 65. Just makes it easy to read what's going on.
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 06-03-2011, 03:56 AM
PlausiblyDamp's Avatar
PlausiblyDampUsing Select case statement PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Quote:
Hi, I just replaced a different segment of code with the select case statement and it works fine. However I don't understand what the dim is for after you do "Select case [variable name here]"

I currently just dimmed a value as nothing. I didn't specify any data type for my variable I'm using for this case statement. But it works.
Without seeing the actual code you are talking about it is going to be difficult to give an explanation.

When you do a select case it will only match a single case, it will not check multiple case statements.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #4  
Old 06-03-2011, 05:44 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by PlausiblyDamp View Post
When you do a select case it will only match a single case, it will not check multiple case statements.
about the multiple statements part. I find this very weird indeed. I have a case with multiple cases, however as my variable changes with the runtime of the program, the case actually changes and selects a new decision (which is what I want).

Now according to what I have read online and what I know a logical case should do is, only go through once and select ONE decision only then carried out.

I think with my case is different as I have a variable within the condition that changes, causing the case to recheck if it's true and which decision it should take.
Reply With Quote
  #5  
Old 06-03-2011, 08:41 AM
AtmaWeapon's Avatar
AtmaWeaponUsing Select case statement AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Select Case doesn't have a memory. Every time it executes, it performs its comparison and selects the right case. If you don't want this to happen, you shouldn't execute the Select..Case statement.

Please err on the side of providing too much code; we can't help you with your program code if we can't see it. Sometimes it's easier to hand us code and let us see what it's doing than it is to try and explain it. In that case it's best to post the code and explain what you *think* should happen. It's important that you describe what you want, because most of us are skilled enough when we look at code we know exactly what it does. Imagine if I showed you a red crayon and complained that the crayon was red; that's often what it looks like to us when someone provides code with no explanation of why they think it's wrong.

I'll provide a brief example; perhaps you just need to see it in action. Drag two text boxes onto a form and paste this code inside the form's source:
Code:
Public Class Form1

    Private Const EasterEgg As String = "Jem"
    Private Const Password As String = "Rainbow Dash"

    Private Sub CheckPassword()
        Dim input As String = TextBox1.Text
        Select Case input
            Case EasterEgg
                TextBox2.Text = "Outrageous!"
            Case Password
                TextBox2.Text = "You guessed the password!"
            Case Else
                TextBox2.Clear()
        End Select
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox2.Enabled = False
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        CheckPassword()
    End Sub
End Class
When the form loads, it disables TextBox2 to make it clear which text box is for input. As you type, TextBox1 raises its TextChanged event, which calls CheckPassword(). That method compares the text box string to two hard-coded special strings. If you type "Jem", the first case is taken and TextBox2 contains "Outrageous!". If you type "Rainbow Dash", the second case is taken and TextBox2 contains "You guessed the password!". If anything else has been typed, TextBox2 is cleared.

Each time, the Select..Case statement picks one and only one case. However, since it has to choose a case each time you type a character in the text box, it looks like multiple cases are being executed. You could write this code in such a way that after a successful password it never changes TextBox2; see if you can figure it out. The trick is making the Select..Case not execute again.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Using Select case statement
Using Select case statement
Using Select case statement Using Select case statement
Using Select case statement
Using Select case statement
Using Select case statement Using Select case statement Using Select case statement Using Select case statement Using Select case statement Using Select case statement Using Select case statement
Using Select case statement
Using Select case statement
 
Using Select case statement
Using Select case statement
 
-->