getting error while reading more than one char in C#

BeUnique 2,332 Reputation points
2021-03-15T13:47:03.873+00:00

I am getting error while reading more than one char in below code.

Intially we were using single char. But, later some time, we used more than one char.

When we used more than one char, i am getting error.

I used many places like below code.

**

  • Additional information: String must be exactly one character long.

**

char startY = char.Parse(Y_StartCol); //Excel Column "F"
char endY = char.Parse(Y_EndCol); // Excel Column "AZ" ==> Getting error if we used more than one char.

How to minimally change the code if we used more than one char or single char

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-03-15T14:10:40.777+00:00

    Try the following where the extension method ExcelColumnName is for getting into double characters for column names as there are in Excel.

    Mockup

    public static class ExtensionMethods
    {
        public static string ExcelColumnName(this int index)
        {
            var chars = new char[]
            {
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 
                'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
            };
    
            index -= 1;
            string columnName;
            var quotient = index / 26;
    
            if (quotient > 0)
            {
                columnName = ExcelColumnName(quotient) + chars[index % 26];
            }
            else
            {
                columnName = chars[index % 26].ToString();
            }
            return columnName;
        }
    
    }
    

    Form code

    for (int index = 1; index < 100; index++)
    {
        var value = index.ExcelColumnName();
        if (value.Length == 1)
        {
            Debug.WriteLine(value);
        }
        else
        {
            Debug.WriteLine($"\t{value}");
            foreach (var character in value)
            {
                Debug.WriteLine($"\t\t{char.Parse(character.ToString())}");                
            }
        }
    }
    

    Partial output

    X
    Y
    Z
     AA
       A
       A
     AB
       A
       B
     AC
       A
       C
     AD
       A
       D
     AE
       A
       E
     AF
       A
       F
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2021-03-15T14:33:56.927+00:00

    To convert Excel column name to number, check an example:

    string column = "AZ";
    int number = column
     .Reverse( )
     .Select( ( c, i ) => new { n = char.ToUpper( c ) - 'A' + 1, p = unchecked((int)Math.Pow( 26, i )) } )
     .Sum( d => d.n * d.p );
    

    The result is 1 for ‘A’, 2 for ‘B’, 52 for ‘AZ’, etc.

    Then you can obtain startY and endY from these numbers. What values do you expect from columns like "F" and "AZ"?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.