using string instead of int

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?bWF2cmlja18xMDE=?=

    #1

    using string instead of int

    Often in code, when retrieving a value from table where I don't have to do
    any math calculations, I store the value in a string variable instead of
    converting to an int and then storing in an int variable.

    Do you guys see anything wrong with this practice?


  • Patrice

    #2
    Re: using string instead of int

    And the value in the table is ? IMO it's best to always work with the
    intended type (that is int if your database column is an int)....


    "mavrick_10 1" <mavrick101@dis cussions.micros oft.coma écrit dans le message
    de news: 209EF7DD-B52E-4F4C-8F7B-0BF766F3035F@mi crosoft.com...
    Often in code, when retrieving a value from table where I don't have to do
    any math calculations, I store the value in a string variable instead of
    converting to an int and then storing in an int variable.
    >
    Do you guys see anything wrong with this practice?
    >
    >

    Comment

    • Mark Rae [MVP]

      #3
      Re: using string instead of int

      "mavrick_10 1" <mavrick101@dis cussions.micros oft.comwrote in message
      news:209EF7DD-B52E-4F4C-8F7B-0BF766F3035F@mi crosoft.com...
      Often in code, when retrieving a value from table where I don't have to do
      any math calculations, I store the value in a string variable instead of
      converting to an int and then storing in an int variable.
      I guess there's nothing intrinsically wrong with it, but I can't imagine why
      you would want to do this...


      --
      Mark Rae
      ASP.NET MVP


      Comment

      • Seth Williams

        #4
        Re: using string instead of int

        Other than math calculations, sorting will definitely be a problem, since
        text is sorted much differently than numbers
        For instance 111 will come before 20, when sorting by text


        David Wier

        http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with no
        bloated markup


        "mavrick_10 1" <mavrick101@dis cussions.micros oft.comwrote in message
        news:209EF7DD-B52E-4F4C-8F7B-0BF766F3035F@mi crosoft.com...
        Often in code, when retrieving a value from table where I don't have to do
        any math calculations, I store the value in a string variable instead of
        converting to an int and then storing in an int variable.
        >
        Do you guys see anything wrong with this practice?
        >
        >

        Comment

        • =?Utf-8?B?bWF2cmlja18xMDE=?=

          #5
          Re: using string instead of int

          Just to avoid unneccary Convet.ToInt32, while retrieving from an Int type
          column in table.



          "Seth Williams" wrote:
          Other than math calculations, sorting will definitely be a problem, since
          text is sorted much differently than numbers
          For instance 111 will come before 20, when sorting by text
          >
          >
          David Wier

          http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with no
          bloated markup
          >
          >
          "mavrick_10 1" <mavrick101@dis cussions.micros oft.comwrote in message
          news:209EF7DD-B52E-4F4C-8F7B-0BF766F3035F@mi crosoft.com...
          Often in code, when retrieving a value from table where I don't have to do
          any math calculations, I store the value in a string variable instead of
          converting to an int and then storing in an int variable.

          Do you guys see anything wrong with this practice?
          >
          >
          >

          Comment

          • Stan

            #6
            Re: using string instead of int

            Often in code, when retrieving a value from table where I don't have to do
            any math calculations, I store the value in a string variable instead of
            converting to an int and then storing in an int variable.
            >
            Do you guys see anything wrong with this practice?
            It's an inefficient way to store an integer. A string requires more
            memory and requires more complex (compiled) code to handle it.

            So it makes no sense to do it just for the sake of it.

            Comment

            • Patrice

              #7
              Re: using string instead of int

              Humm how do you retrieve the data ? You shouldn't have to convert. You may
              want to post a new thread about your original issue...

              Actually I never even thought about not having the correct mapping (and I
              don't even remember to have heard about doing such a thing)...

              It has a potential to cause loads of problem (comparison, sort, type
              checking etc...) or at best you'll have to change your code as soon as you
              wan't to do actually something with this data + the conversion even if you
              have to do one is not that a huge issue...

              I would really use the correct mapping between the client side variables and
              the server side data. As said earlier I suspect you have an earlier issue
              you may want to post about...

              --
              Patrice

              "mavrick_10 1" <mavrick101@dis cussions.micros oft.coma écrit dans le message
              de news: B99272FF-C629-4E1D-ABA0-7BA5796CA497@mi crosoft.com...
              Just to avoid unneccary Convet.ToInt32, while retrieving from an Int type
              column in table.
              >
              >

              Comment

              • Hans Kesting

                #8
                Re: using string instead of int

                When your database column has an "int" type, you can easily do
                something like:

                DataRow dr = MyDataSet.Rows[0];
                int i = (int)dr["MyIntColum n"];

                but this will fail if there are "null"s in that column (the field will
                then contain a DbNull.Value). In that case use this:

                int ?i2 = dr["MyIntColum n"] as int?;

                Hans Kesting


                mavrick_101 explained :
                Just to avoid unneccary Convet.ToInt32, while retrieving from an Int type
                column in table.
                >

                Comment

                Working...