Results 1 to 3 of 3

Thread: [RESOLVED] [2.0] Switch with Case Range

  1. #1

    Thread Starter
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Resolved [RESOLVED] [2.0] Switch with Case Range

    Is there a way in C# to have a range for a case statement. You can do this in VB, but how can you in C#:

    Code:
        Select Case e.KeyChar
             Case Chr(8)
             Case "0"c To "9"c
                Dim val As Int64 = CType(MyBase.Text & e.KeyChar, Int64)
                If val < _minRange Then
                   MyBase.Text = _minRange.ToString
                   e.Handled = True
                End If
                If val > _maxRange Then
                   If Me.SelectionLength = 0 Then
                      MyBase.Text = _maxRange.ToString
                      e.Handled = True
                   End If
                End If
             Case Else
                Beep()
                e.Handled = True
          End Select
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

  2. #2
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: [2.0] Switch with Case Range

    No, but the if/else equivalent is pretty trivial anyway (via Instant C#):

    Code:
    //INSTANT C# NOTE: The following VB 'Select Case' included range-type or non-constant 'Case' expressions and was converted to C# 'if-else' logic:
    //	Select Case e.KeyChar
    //ORIGINAL LINE: Case Chr(8)
    	if (e.KeyChar == '\b')
    	{
    	}
    //ORIGINAL LINE: Case "0"c To "9"c
    	else if (e.KeyChar >= '0' && e.KeyChar <= '9')
    	{
    			Int64 val = System.Convert.ToInt64(base.Text + e.KeyChar);
    			if (val < _minRange)
    			{
    			   base.Text = _minRange.ToString();
    			   e.Handled = true;
    			}
    			if (val > _maxRange)
    			{
    			   if (this.SelectionLength == 0)
    			   {
    				  base.Text = _maxRange.ToString();
    				  e.Handled = true;
    			   }
    			}
    	}
    //ORIGINAL LINE: Case Else
    	else
    	{
    			Microsoft.VisualBasic.Interaction.Beep();
    			e.Handled = true;
    	}
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  3. #3

    Thread Starter
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: [2.0] Switch with Case Range

    Darn, I was hoping to use a switch here, but alright. Thanks!
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

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