Results 1 to 4 of 4

Thread: [RESOLVED] [2.0] Problem with cint?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. int a = 5;
    2. int b = 50;
    3.  
    4. MessageBox.Show(a.ToString().PadLeft(2, '0'));
    5. MessageBox.Show(b.ToString().PadLeft(2, '0'));
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width