|
-
May 28th, 2007, 08:17 PM
#1
Thread Starter
Addicted Member
[RESOLVED] [2.0] Problem with cint?
I have a problem with this code?
Code:
int a = ds.Tables[0].Rows.Count;
a = a + 1;
if (a < 10)
{
a = "0" + a;
a = cint(a);
}
I want in the first place to check if the number is less than 10. If it is, I am going to append "0" in front of the integer number. Example. if the value of a is 9 then the program will add 0 and the value will be 09.
I always get this error with this code:
Cannot implicitly convert type 'string' to 'int'. The name 'cint' does not exist in the current context.
-
May 28th, 2007, 11:00 PM
#2
Re: [2.0] Problem with cint?
There's no such thing as CInt in C#; it's a VB statement. In VB it will cast or convert a value to type Integer. In C# there is no overlap between casting and converting. They are distinct operations.
As to what you're trying to do there, it doesn't actually make any sense. Your 'a' variable is type int and there is no concept of leading zeroes for numerical values. Think of it this way. Hold up 9 fingers. Now hold up 09 fingers. Was there any difference? No, because the number 9 is the number 9 regardless. A number doesn't change.
You can represent the same number in many different ways. If you want to display a number to the user then you must first convert it to a string. Once you've done that you can add a leading zero if it's shorter than two digits if you want, e.g.
C# Code:
int a = 5;
int b = 50;
MessageBox.Show(a.ToString().PadLeft(2, '0'));
MessageBox.Show(b.ToString().PadLeft(2, '0'));
-
May 29th, 2007, 12:55 AM
#3
Thread Starter
Addicted Member
Re: [2.0] Problem with cint?
Hi jmcilhinney!
The reason why I would like to present a leading zero because I would like to sort it in a datagrid. Let says, I have the following:
9
8
7
6
5
4
3
21
20
2
19
18
17
16
15
14
13
12
11
10
1
Is there anyway to present it correctly in a datagrid?
-
May 29th, 2007, 04:58 AM
#4
Re: [2.0] Problem with cint?
If you have a list of numbers in a DataGrid then they will sort numerically. If they don't sort numerically then you don't have numbers in your DataGrid. You must have strings in your DataGrid, which will be sorted alphabetically. Numbers are numbers. Strings containing digits are NOT numbers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|