-1

I am comparing to arrays to see which value (char) in the array is bigger. It worked fine until I thought I would add in the representation of the value it was comparing.

using System;

class ArrayEx3
{
    static void Main()
    {
        char[] array1 = { 'a', 'E', 'i', 'o', 'u' };
        char[] array2 = { 'A', 'e', 'I', 'O', 'U' };

        for (int i = 0; i < array1.Length; i++)
        {
            if (array1[i] > array2[i])
            {
                Console.WriteLine(" {0} is {2} bigger than {1} which is {3}", array1[i], array2[i],((int)array2[i].ToChar()),((int)array2[i].ToChar()));
            }
            else
            {
                Console.WriteLine(" {0} is  bigger than {1}", array2[i], array1[i]);
            }
        }
    }
}

I am receiving this error.

C:\Users\Sayth\Documents\Scripts>csc ArrayEx3.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.33440
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

ArrayEx3.cs(14,104): error CS1061: 'char' does not contain a definition for
        'ToChar' and no extension method 'ToChar' accepting a first argument of
        type 'char' could be found (are you missing a using directive or an
        assembly reference?)
ArrayEx3.cs(14,130): error CS1061: 'char' does not contain a definition for
        'ToChar' and no extension method 'ToChar' accepting a first argument of
        type 'char' could be found (are you missing a using directive or an
        assembly reference?)

C:\Users\Sayth\Documents\Scripts>

Checking the Convert ToChar docs though http://msdn.microsoft.com/en-us/library/4db6awt7(v=vs.110).aspx shows that ToChar is in the System space so why am I getting this?

sayth
  • 6,696
  • 12
  • 58
  • 100

1 Answers1

2

You need to call it like this:

Convert.ToChar(array2[i]);

It is not an extension method.

BTW, it doesn't make sense to convert char to char... If you want to get ASCII value of your char just cast it to int.

Console.WriteLine(" {0} is {2} bigger than {1} which is {3}", array1[i], array2[i], (int)array1[i], (int)array2[i]);
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • Thanks I was following this thread and got myself confused http://stackoverflow.com/questions/5002909/getting-the-ascii-value-of-a-character-in-a-c-sharp-string – sayth Aug 02 '14 at 10:10