Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. useless...

useless...

Scheduled Pinned Locked Moved The Weird and The Wonderful
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Noman Aftab
    wrote on last edited by
    #1

    txtName being a text field: txtName.Text.ToString() X|

    Noman Muhammad Aftab, Software Mechanic

    J J 2 Replies Last reply
    0
    • N Noman Aftab

      txtName being a text field: txtName.Text.ToString() X|

      Noman Muhammad Aftab, Software Mechanic

      J Offline
      J Offline
      Jeroen De Dauw
      wrote on last edited by
      #2

      Yeah, that's indeed quite insecure. Obviously you should Cstr() the thing and then Ctype it to string for good practice.

      Jeroen De Dauw
      Blog ; Wiki

      1 Reply Last reply
      0
      • N Noman Aftab

        txtName being a text field: txtName.Text.ToString() X|

        Noman Muhammad Aftab, Software Mechanic

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        I can't tell you how many times ive seen ToString() called on a string type. Its a typical newbie mistake, the lack of understanding of data types.

        S H 2 Replies Last reply
        0
        • J J4amieC

          I can't tell you how many times ive seen ToString() called on a string type. Its a typical newbie mistake, the lack of understanding of data types.

          S Offline
          S Offline
          supercat9
          wrote on last edited by
          #4

          I wonder if the person is trying to be paranoid-protective about the possibility that a property might return a type which is not a string, but features bidirectional widening conversions? As a hypothetical example, one might have a private string-interning pool. Properties of a class which would normally return string could instead return items from the pool. If the properties returned strings, then setting one such property to another would require generating a new string object and then doing some sort of dictionary lookup to locate the string in the pool. By contrast, if the objects returned intern-pool references, those steps could be eliminated. One hazard with such an approach is that if such an object were passed to a function that expected something of type object, things would appear to work normally (the string would be extracted when the object was cast to a string) but the object that was kept would be a pool reference. This could cause an entire pool of interned strings to be kept alive even if there were no live references to anything that cared about the pool being a pool (just a reference to something thought to be a string).

          J 1 Reply Last reply
          0
          • S supercat9

            I wonder if the person is trying to be paranoid-protective about the possibility that a property might return a type which is not a string, but features bidirectional widening conversions? As a hypothetical example, one might have a private string-interning pool. Properties of a class which would normally return string could instead return items from the pool. If the properties returned strings, then setting one such property to another would require generating a new string object and then doing some sort of dictionary lookup to locate the string in the pool. By contrast, if the objects returned intern-pool references, those steps could be eliminated. One hazard with such an approach is that if such an object were passed to a function that expected something of type object, things would appear to work normally (the string would be extracted when the object was cast to a string) but the object that was kept would be a pool reference. This could cause an entire pool of interned strings to be kept alive even if there were no live references to anything that cared about the pool being a pool (just a reference to something thought to be a string).

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            The sort of people who write myTextField.Text.ToString() would have no clue what you've just written.

            S 1 Reply Last reply
            0
            • J J4amieC

              The sort of people who write myTextField.Text.ToString() would have no clue what you've just written.

              S Offline
              S Offline
              supercat9
              wrote on last edited by
              #6

              J4amieC wrote:

              The sort of people who write myTextField.Text.ToString() would have no clue what you've just written.

              Naturally. On the other hand, they might have heard of some vaguely-similar situation in which a competent programmer who did understand what was going on had applied some seemingly-unnecessary cast, and figured that if a cast was needed there and they didn't understand any distinction between that case and the one at hand, they should apply a cast (or explicit ToString) in the present case as well.

              P 1 Reply Last reply
              0
              • S supercat9

                J4amieC wrote:

                The sort of people who write myTextField.Text.ToString() would have no clue what you've just written.

                Naturally. On the other hand, they might have heard of some vaguely-similar situation in which a competent programmer who did understand what was going on had applied some seemingly-unnecessary cast, and figured that if a cast was needed there and they didn't understand any distinction between that case and the one at hand, they should apply a cast (or explicit ToString) in the present case as well.

                P Online
                P Online
                PIEBALDconsult
                wrote on last edited by
                #7

                Now that reminds me of a time I had to maintain a (batch) program that accessed RDB. It had the equivalent of ToChar(ToChar(somestring)) all throughout the SQL code. The guy who had been maintaining it said it was that way when got it and that he was told it had to be that way. I called BS, removed the extraneous ToChar, and proved that it was unnecessary. I made some other changes and added some functionality and the resulting program ran in a quarter of the time.

                1 Reply Last reply
                0
                • J J4amieC

                  I can't tell you how many times ive seen ToString() called on a string type. Its a typical newbie mistake, the lack of understanding of data types.

                  H Offline
                  H Offline
                  Hired Mind
                  wrote on last edited by
                  #8

                  J4amieC wrote:

                  I can't tell you how many times ive seen ToString() called on a string type. Its a typical newbie mistake, the lack of understanding of data types.

                  To true. Great for catching newbies when conducting job interviews. Luckily though, the framework protects them from themselves:

                  // From mscorlib.dll
                  public override string ToString()
                  {
                  return this;
                  }

                  Not perfect but at least there's not a whole redundant complex conversion of some kind taking place...

                  Before .NET 4.0, object Universe = NULL;

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups