Click here to Skip to main content
15,902,445 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Comments
Sergey Alexandrovich Kryukov 4-Jan-13 1:25am    
Why ToChar, not ToString? integer may need more than one character to present as a string :-)
—SA
salamkudru 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 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
salamkudru 4-Jan-13 1:38am    
Thank you for your replay.I have solved the issue now :)
Sergey Alexandrovich Kryukov 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

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
 
Share this answer
 
v3
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().
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Jan-13 1:37am    
Correct, a 5.
—SA
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? :-))

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
 
Share this answer
 
v3
Try this:
C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900