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. General Programming
  3. C#
  4. what happens if value is longer than fieldlength

what happens if value is longer than fieldlength

Scheduled Pinned Locked Moved C#
question
8 Posts 4 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.
  • S Offline
    S Offline
    stephan_007
    wrote on last edited by
    #1

    hy everyone! what happens in cases like this:string myinput = "12345678910"; myinput = myinput.toString({0:10}); // formats the string to a length of 10
    Does this throw an exception or does it cut off all charecters > 10 ?? Because I want the string to be insert no matter if it is too long or not. If it is too long then this should be handled by a warning but the string should be insert without cutting off anything. So I wonder if this function tries to format it to length 10 but accepts strings which are longer as well, or if it doesn't. Because if it doesn't then i have to take care about this myself by checking, if the string is too long. Thanks. Stephan.

    G C S 3 Replies Last reply
    0
    • S stephan_007

      hy everyone! what happens in cases like this:string myinput = "12345678910"; myinput = myinput.toString({0:10}); // formats the string to a length of 10
      Does this throw an exception or does it cut off all charecters > 10 ?? Because I want the string to be insert no matter if it is too long or not. If it is too long then this should be handled by a warning but the string should be insert without cutting off anything. So I wonder if this function tries to format it to length 10 but accepts strings which are longer as well, or if it doesn't. Because if it doesn't then i have to take care about this myself by checking, if the string is too long. Thanks. Stephan.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Why write such a long post instead of just trying it to see ?

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      1 Reply Last reply
      0
      • S stephan_007

        hy everyone! what happens in cases like this:string myinput = "12345678910"; myinput = myinput.toString({0:10}); // formats the string to a length of 10
        Does this throw an exception or does it cut off all charecters > 10 ?? Because I want the string to be insert no matter if it is too long or not. If it is too long then this should be handled by a warning but the string should be insert without cutting off anything. So I wonder if this function tries to format it to length 10 but accepts strings which are longer as well, or if it doesn't. Because if it doesn't then i have to take care about this myself by checking, if the string is too long. Thanks. Stephan.

        G Offline
        G Offline
        Gareth H
        wrote on last edited by
        #3

        stephan_007, What language is this in?, because it doesn't compile in C#. Regards, Gareth.

        S 1 Reply Last reply
        0
        • S stephan_007

          hy everyone! what happens in cases like this:string myinput = "12345678910"; myinput = myinput.toString({0:10}); // formats the string to a length of 10
          Does this throw an exception or does it cut off all charecters > 10 ?? Because I want the string to be insert no matter if it is too long or not. If it is too long then this should be handled by a warning but the string should be insert without cutting off anything. So I wonder if this function tries to format it to length 10 but accepts strings which are longer as well, or if it doesn't. Because if it doesn't then i have to take care about this myself by checking, if the string is too long. Thanks. Stephan.

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

          I believe it will throw an exception..even sql server throws an exception that warns that string data is bieng truncated. your best solution would be to either limit what the user can type in, or check its length before you try to insert it, and either truncate it yourself, or tell the user that the input is invalid(best solution so the user doesnt get bad data in the db or where ever you're putting it)

          S 1 Reply Last reply
          0
          • G Gareth H

            stephan_007, What language is this in?, because it doesn't compile in C#. Regards, Gareth.

            S Offline
            S Offline
            stephan_007
            wrote on last edited by
            #5

            it's c# but i didn't copy it from my source, i just typed it into here. maybe i misstyped it. the reason why i posted it here is, maybe someone already did anything similar and could help me to keep it simple :) because it looks like i usually solve problems in a too difficult way which could be solved by default functions. that's why. i know i could try the two statements above in my own code, but as i already said, maybe someone knows a different way to format the string without raising an error ;)

            S 1 Reply Last reply
            0
            • S stephan_007

              it's c# but i didn't copy it from my source, i just typed it into here. maybe i misstyped it. the reason why i posted it here is, maybe someone already did anything similar and could help me to keep it simple :) because it looks like i usually solve problems in a too difficult way which could be solved by default functions. that's why. i know i could try the two statements above in my own code, but as i already said, maybe someone knows a different way to format the string without raising an error ;)

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

              ups, i misstyped the second line! it should be myinput = String.Format("{0,10}", myinput} and i already found out, it ignores the formating, if the string is longer than defined in the formatsring. so i have to check it myself, by splitting off the relevant position in the format string to compare it to.

              modified on Monday, May 5, 2008 9:10 AM

              1 Reply Last reply
              0
              • S SomeGuyThatIsMe

                I believe it will throw an exception..even sql server throws an exception that warns that string data is bieng truncated. your best solution would be to either limit what the user can type in, or check its length before you try to insert it, and either truncate it yourself, or tell the user that the input is invalid(best solution so the user doesnt get bad data in the db or where ever you're putting it)

                S Offline
                S Offline
                stephan_007
                wrote on last edited by
                #7

                the user wants this behaviour: if the string is smaller or equal the length defined in the format string, then just insert it, if it is longer than the length defined, then the programm should throw a worning, but insert the data anyway. i have to insert it into a stream. so if it is too long, then all other info is moved x characters to the right (x is the difference to the defined and the real length). well, because it just ignores the length if it is too long, i have to write a check myself to know, if i have to throw a warning or if the length is ok.

                S 1 Reply Last reply
                0
                • S stephan_007

                  the user wants this behaviour: if the string is smaller or equal the length defined in the format string, then just insert it, if it is longer than the length defined, then the programm should throw a worning, but insert the data anyway. i have to insert it into a stream. so if it is too long, then all other info is moved x characters to the right (x is the difference to the defined and the real length). well, because it just ignores the length if it is too long, i have to write a check myself to know, if i have to throw a warning or if the length is ok.

                  S Offline
                  S Offline
                  SomeGuyThatIsMe
                  wrote on last edited by
                  #8

                  you should probably try to explain to the user that just inserting the data even if its too long is a bad idea. you could just use the string.Length to find out how long it is, and use Remove to cut off any unwanted characters

                  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