0

I need to convert an unicode to letter and then paste the letter into a textbox. So I have a code like this:

textBox1.Text = Convert.ToString(Convert.ToChar(letter_code));

I think this code line doesn't look good and there's another way to do the same thing. Is it?

John Green
  • 13
  • 2
  • 1
    What's `letter_code`? (And as an aside, I'd suggest that you start to follow the .NET naming conventions - which would use `letterCode` instead of `letter_code`.) – Jon Skeet Aug 27 '15 at 21:53
  • It's an integer value – John Green Aug 27 '15 at 21:54
  • Purely off the top of my head... Might be way off, isn't there an Encoding.Unicode.GetString() that can do this? – daniel Aug 27 '15 at 21:54
  • 1
    `textBox1.Text = new string(letter_code,1); `http://stackoverflow.com/questions/17730706/how-to-convert-a-number-to-an-ascii-character – austin wernli Aug 27 '15 at 21:55
  • So its compile-time type is `int`? (It would have been easier if you'd provided a short but complete program to start with...) – Jon Skeet Aug 27 '15 at 21:58

1 Answers1

1

Assuming letter_code is of type int, I'd probably cast to char and call ToString:

textBox1.Text = ((char) letter_code).ToString();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thank you for your answer. Could you please explain me, why use `.ToString ()` when `Convert.ToString` is safer? I mean, not in this particular case. Just wondering)) – John Green Aug 27 '15 at 22:17
  • @JohnGreen The difference between the two is that one handles null while the other doesn't. your using a char and int which can't be null – austin wernli Aug 27 '15 at 22:20
  • 1
    ToString() is not inherently unsafe. It is a method inherited from object. Here it is using the override of that method as defined for data type char. You could override that method to be anything you want it to be. – WDS Aug 27 '15 at 22:20
  • 1
    @JohnGreen: I *usually* use `ToString` to convert a value to a string. That's what it's there for, after all. If a the value I want to convert may be null, I usually think about that separately - but it's not going to happen for a `char`. – Jon Skeet Aug 28 '15 at 05:50