Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:16:55+00:00 2026-06-12T09:16:55+00:00

I have this code: switch (currentLetter) { case ‘A’: return ‘B’; case ‘B’: return

  • 0

I have this code:

switch (currentLetter)
{
    case 'A': return 'B';
    case 'B': return 'C';
    case 'C': return 'D';
    case 'D': return 'E';
    case 'E': return 'F';
    case 'F': return 'G';
    case 'G': return 'H';

    case 'a': return 'b';
    case 'b': return 'c';
    case 'c': return 'd';
    case 'd': return 'e';
    case 'e': return 'f';
    case 'f': return 'g';
    case 'g': return 'h';
}

I thought of many ways to change it but I’m not sure on which to choose. I could replace all the returns with (char)(currentLetter + 1), use if statements with ASCII values to determine range and then do (char)(currentLetter + 1), use Enumerable.Range.Contains and then see if the value is within the range, replace the switch with an if, etc.

This code will not be repeated anywhere else and I’m not sure if this isn’t the best way of doing it since it’s very clear to the reader of the code to what’s going on and they don’t have to think of character codes, arithmetic, etc. Also, there will never be any more characters to add to the case statements so it won’t get unwieldy.

Not sure if I should leave it as is or change it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:16:56+00:00Added an answer on June 12, 2026 at 9:16 am

    Use

    return (char)(currentLetter + 1);
    

    If you need to perform a range test

    if ("ABCDEFGabcdefg".Contains(currentLetter)) {
        return (char)(currentLetter + 1);
    }
    throw new ArgumentOutOfRangeException(
        "Letter in the range 'A'-'G' or 'a'-'g' expected.");
    

    It is visible immediately, that the next letter in the alphabet is returned without inspecting a long list of cases. Also it’s less prone to errors.


    UPDATE: char is considered to be a numeric type in C# and can implicitly be converted to other numeric types that are at least 16 bit wide. You can even apply the increment and decrement operators on them. Therefore a shorter solution with no casting exists:

    return ++currentLetter;
    

    Note: This changes the original value of currentLetter, but since char is not a reference type, this should not be a problem, if currentLetter was a method parameter. Also, the pre-increment operator must be used, as the old value would be returned with the post-increment operator!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have kept hearing this statement. Switch..Case is Evil for code maintenance, but it
I have this code where $difference=0 switch ($difference) { case 0<=$difference && $difference<300: return
I have this code: _trackit: function(){ for(var key in this.items.sublinks){ switch(key){ case 'shoes': for(var
I have this code: pid_t vPid=fork(); int vStat; switch(vPid){ case -1: perror(fork); exit(1); case
I think many of you have this kind of code somewhere: int foo; switch
I have this switch statement in my code: switch(buttonIndex){ case 0: [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
I'm not sure what is happening. I have this code: `case (XMLREADER::ELEMENT): $node_type =
I have this code: #include<stdio.h> int main() { int a=10; switch(a) { case '1':
I have this code snippet that I want to simplify: switch (typeString) { case
So I have this code: $(document).ready(function() { $(document).keypress(function(e) { switch(e.which) { // user presses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.