How to store a single character?

Asked

Viewed 178 times

1

My class has an attribute that is a single character. I can store as string, but I think the right thing would be char. Only this attribute can receive one int (between 0 and 9) another method. How do I save this number in this char error-free?

  • Have any answers solved what was in doubt? Do you need something else to be improved? Do you think it is possible to accept it now?

3 answers

4

If you can guarantee that will always come between 0 and 9 is basically math:

using static System.Console;
using static System.Convert;

public class Program {
    public static void Main() {
        var x = 2;
        WriteLine(ToChar(x + 48));
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

48 is the decimal code of ascii table for the character representing 0. If it comes outside the range of 0 to 9 it may not give the expected result, although in most cases it will have no problems.

1

I think the best way would be to use the guy string and to facilitate encapsulating the conversion process either in the manufacturer or to create methods to encapsulate the value assessment:

using System;

namespace consoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var oneChar1 = new OneChar('a');
            Console.WriteLine(oneChar1.Property);

            var oneChar2 = new OneChar(1);
            Console.WriteLine(oneChar2.Property);
        }
    }

    public class OneChar
    {

        public OneChar(char c)
        {
            Property = Convert.ToString(c);
        }

        public OneChar(int i)
        {
            Property = Convert.ToString(i);
        }

        public string Property { get; private set; }

        public void SetProperty(char c)
        {
            Property = Convert.ToString(c);
        }

        public void SetProperty(int i)
        {
            Property = Convert.ToString(i);
        }
    }
}
  • It is more practical to use a internal set; instead of declaring two functions to change the value of a property.

  • The use of two fuctions is to illustrate the encapsulation of the property assignment rule.

0

A string is a char[], a string. I made an extension method:

public static char ToChar(this string value) {
    if (string.IsNullOrEmpty(value)) throw new ArgumentException();

    // retorna o primeiro caractere da string
    // se não quiser usar o System.Linq, pode fazer "return value[0];"
    return value.First();
}

This method will make:

"a".ToChar();          // char 'a'
"1".ToChar();          // char '1'
"".ToChar();           // ArgumentException foi disparado
"321".ToChar();        // char '3'
"xyz".ToChar();        // char 'x'

It always returns the first character. To use with integers, see:

1.ToString().ToChar();  // char '1'

Behold working at Ideone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.