Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi Friends,
 
I want to convert into to char and I tried this code
var floor=Convert.ToChar(ddlFloor.SelectedIndex);
 
but here I am not getting the correct value after converting to the char,for example if ddlFloor.SelectedIndex is 2 then I am getting the value for floor is 2''
 
that means floor=2'' why single quotes coming after number?
 
and when I try to insert it into the table (in table I have Floor column with char data type) it will insert null value. Why?
 
Thanks in advance
 
Abdul Salam
Posted 3-Jan-13 19:16pm
Comments
Sergey Alexandrovich Kryukov at 4-Jan-13 1:25am
   
Why ToChar, not ToString? integer may need more than one character to present as a string :-) —SA
Member 9233080 at 4-Jan-13 1:30am
   
Because my table has Floor column with char data type.Do you know why two single quotes coming after the number while converting to the char from int (eg floor=2'' )?
Sergey Alexandrovich Kryukov at 4-Jan-13 1:33am
   
OK, then why it's a char data type? Conversion from int to char is not a correct operation, in general. Didn't I explain why? —SA
Member 9233080 at 4-Jan-13 1:38am
   
Thank you for your replay.I have solved the issue now :)
Sergey Alexandrovich Kryukov at 4-Jan-13 1:41am
   
If you integer value is 2, it is not converted to '2'; it is converted to Unicode character which code point integer value equals 2. For example integer 40 is converted to '('. Are you getting it? Not all integer values correspond to a character, because in Unicode there are special ranges for non-characters (for example, ranges for surrogates), or undefined (reserved for possible future use, for alignment by certain boundary)... Is it more clear now? —SA
Member 9233080 at 4-Jan-13 1:51am
   
Yeah I understood.Thank you very much.
Sergey Alexandrovich Kryukov at 4-Jan-13 3:36am
   
Great. And you are very welcome. It's a pleasure to help someone who can get the explanation right away. Good luck, call again. —SA
Member 9233080 at 4-Jan-13 6:13am
   
Hello Sergey, could you explain the difference between abstract class and interface with real world example? Thanks in advance
Sergey Alexandrovich Kryukov at 4-Jan-13 7:12am
   
OK, I'll try. Please see Solution 4. —SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think you rather need ddFloor.SelectedIndex.ToString. And don't forget to take into account the case when nothing is be selected.
 
However, I'm not sure the whole idea is right. Why getting string representation of selected index at all? I'm asking, because, these days, a trend to work with string representation of data is a real sickness of many. String representation is only needed when you want to show something on screen, sometimes to store in a file or a stream. Nobody needs to perform calculations "converting" (not really correct term) to strings and back, but many do… Don't do it.
 
—SA
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Member 9233080 asked:
Hello Sergey, could you explain the difference between abstract class and interface with real world example? Thanks in advance.
Well, the question about "difference" is not quite correct. Could you possibly define what is "difference"? (What is the difference between apple and Apple? Smile | :) )
 
Nevertheless, considering the use of abstract classes vs interfaces is important, because in real life, the decision of using interfaces can be considered as the alternative to using abstract classes, or both approaches could be combined. Making the decision is not trivial and not easy. And it's not so easy to explain in brief; even though many tried to explain it simply, such explanations rarely reflected the essence of things and they rarely provided valid practical criteria for good code design.
 
Let me try. Please see these past discussions, including my answers:
When we use abstract and when we use interface...?[^],
Difference between abstract class and interface if they have same no of methods and var[^],
How to decide to choose Abstract class or an Interface[^],
Interfaces and Polymorphism[^].
 
As to the examples, the short examples of the usage are easy to write, but they can only show the syntax, which you can easily find out by yourself, but a short examples will hardly help to make a right choice. Maybe you will better ask you follow-up questions if you need.
 
—SA
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I'm assuming you're looking at the value of floor in the debugger.
The display of 2 ' ' is because the character with Unicode value of 2 does not have a displayable representation.
The debugger always shows both the numeric value of the character and the character itself between the apostrophe characters.
I think what you are trying to accomplish is to get the character representation of the numeric value, in which case you should use Convert.ToString().
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 4-Jan-13 1:37am
   
Correct, a 5. —SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this:
var floor = Convert.ToString(DropDownList1.SelectedIndex);
Char c=Convert.ToChar( floor);
 
If see its value in immediate window it will look like below:
50 '2'// here 2 is selected Index but when you assign it to some other variable then 2 will be assigned.
Let me know if you need any help.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 372
1 OriginalGriff 295
2 thatraja 215
3 Emre Ataseven 155
4 Shai Vashdi 125
0 Sergey Alexandrovich Kryukov 8,849
1 OriginalGriff 5,035
2 Peter Leow 3,949
3 Maciej Los 3,535
4 Abhinav S 3,148


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid