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. Point conversion.

Point conversion.

Scheduled Pinned Locked Moved C#
question
17 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.
  • L Luc Pattyn

    First new class of the week! :thumbsup:

    Luc Pattyn [My Articles] Nil Volentibus Arduum

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #5

    :D

    1 Reply Last reply
    0
    • L Lost User

      paper67 wrote:

      What is the best way to convert a point string to a point object ?

      Using the appropriate TypeConverter[^] :)

      Bastard Programmer from Hell :suss:

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #6

      Interestingly, I tried that but it fails, as the ouput of Point.ToString() is not parsable by the converter.

      Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

      L 1 Reply Last reply
      0
      • L Lost User

        Interestingly, I tried that but it fails, as the ouput of Point.ToString() is not parsable by the converter.

        Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #7

        I'm not surprised. The .ToString method shows the contents of the object, it doesn't convert it to it's string-representation. I'd expect one would have to go from object->string and vice versa over the TypeConverter.

        Bastard Programmer from Hell :suss:

        1 Reply Last reply
        0
        • P paper67

          Hi, What is the best way to convert a point string to a point object ? Point Pt1 = new Point(10, 20); string strPt = Pt1.ToString(); Point Pt2 = ? (use strPt to convert back to Point object) Thx.

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #8

          If you have control over both parts of the operation, i.e. this is for persisting some data you own to disk/network/etc, you should use the TypeConverter:

          Point pt1 = new Point(10, 20);
          string strPt = TypeDescriptor.GetConverter(pt1).ConvertToString(pt1);
          Point pt2 = TypeDescriptor.GetConverter(typeof(Point)).ConvertFromString(strPt);

          If you don't have control over the string format, you will have to write a parser. It's pretty simple (clean off the brackets, split on ',', verify two parts, aand parse both halves as int).

          L 1 Reply Last reply
          0
          • B BobJanova

            If you have control over both parts of the operation, i.e. this is for persisting some data you own to disk/network/etc, you should use the TypeConverter:

            Point pt1 = new Point(10, 20);
            string strPt = TypeDescriptor.GetConverter(pt1).ConvertToString(pt1);
            Point pt2 = TypeDescriptor.GetConverter(typeof(Point)).ConvertFromString(strPt);

            If you don't have control over the string format, you will have to write a parser. It's pretty simple (clean off the brackets, split on ',', verify two parts, aand parse both halves as int).

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #9

            If you have control over both ends, why use strings at all?

            L B 2 Replies Last reply
            0
            • L Lost User

              If you have control over both ends, why use strings at all?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #10

              As an intermediate format without having to resort to serialization :)

              Bastard Programmer from Hell :suss:

              L 1 Reply Last reply
              0
              • L Lost User

                As an intermediate format without having to resort to serialization :)

                Bastard Programmer from Hell :suss:

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #11

                Just store two ints?

                L 1 Reply Last reply
                0
                • L Lost User

                  If you have control over both ends, why use strings at all?

                  B Offline
                  B Offline
                  BobJanova
                  wrote on last edited by
                  #12

                  Typically the answer to this is if you want to store something in a human readable form. Object->string conversions are often part of file I/O. (This is what I'm using similar code for.) Or, for a web app, creating a POST request body, though there it might be better to use &x=42&y=72.

                  L 1 Reply Last reply
                  0
                  • B BobJanova

                    Typically the answer to this is if you want to store something in a human readable form. Object->string conversions are often part of file I/O. (This is what I'm using similar code for.) Or, for a web app, creating a POST request body, though there it might be better to use &x=42&y=72.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #13

                    Ah yes of course, I was thinking too much in terms of persistence

                    1 Reply Last reply
                    0
                    • L Lost User

                      Just store two ints?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #14

                      True, that's an option too :) You can store every class by simply looping it's public properties, and to store the contents as a string. (Storing both ints in a string is the same thing) That would mean that you have to pay attention to the format, and match up the encode/decode functions. The TypeConverter is a somewhat "safer" way of converting your class, because it adheres to a fixed format for that type in a human-readable form. It's not only used for locations, but also for colors and the like. This would be my recommendation. A third option would be serialization. That'd be a bit overkill in this particular situation, but it remains an option.

                      Bastard Programmer from Hell :suss:

                      L B 2 Replies Last reply
                      0
                      • L Lost User

                        True, that's an option too :) You can store every class by simply looping it's public properties, and to store the contents as a string. (Storing both ints in a string is the same thing) That would mean that you have to pay attention to the format, and match up the encode/decode functions. The TypeConverter is a somewhat "safer" way of converting your class, because it adheres to a fixed format for that type in a human-readable form. It's not only used for locations, but also for colors and the like. This would be my recommendation. A third option would be serialization. That'd be a bit overkill in this particular situation, but it remains an option.

                        Bastard Programmer from Hell :suss:

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #15

                        Well I was just going to store the ints and ints, but yea it really depends on where the data is supposed to go..

                        1 Reply Last reply
                        0
                        • L Lost User

                          True, that's an option too :) You can store every class by simply looping it's public properties, and to store the contents as a string. (Storing both ints in a string is the same thing) That would mean that you have to pay attention to the format, and match up the encode/decode functions. The TypeConverter is a somewhat "safer" way of converting your class, because it adheres to a fixed format for that type in a human-readable form. It's not only used for locations, but also for colors and the like. This would be my recommendation. A third option would be serialization. That'd be a bit overkill in this particular situation, but it remains an option.

                          Bastard Programmer from Hell :suss:

                          B Offline
                          B Offline
                          BillWoodruff
                          wrote on last edited by
                          #16

                          Eddy Vluggen wrote:

                          You can store every class by simply looping it's public properties, and to store the contents as a string.

                          Just curious: you mean by using Reflection ? And, if I may ask, why would serialization be "over-kill" in this scenario ... assuming this question is not a one-off, but has broader implications. And, how about using Mehdi Gholam's zippy implementation of JSON on steroids here on CP ? Over-kill ? best, Bill

                          "Last year I went fishing with Salvador Dali. He was using a dotted line. He caught every other fish." Steven Wright

                          L 1 Reply Last reply
                          0
                          • B BillWoodruff

                            Eddy Vluggen wrote:

                            You can store every class by simply looping it's public properties, and to store the contents as a string.

                            Just curious: you mean by using Reflection ? And, if I may ask, why would serialization be "over-kill" in this scenario ... assuming this question is not a one-off, but has broader implications. And, how about using Mehdi Gholam's zippy implementation of JSON on steroids here on CP ? Over-kill ? best, Bill

                            "Last year I went fishing with Salvador Dali. He was using a dotted line. He caught every other fish." Steven Wright

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #17

                            BillWoodruff wrote:

                            Just curious: you mean by using Reflection ?

                            That's what it implies, and yes, I know that you don't need reflection for serialization.

                            BillWoodruff wrote:

                            And, if I may ask, why would serialization be "over-kill" in this scenario ... assuming this question is not a one-off, but has broader implications.

                            Because the TS gave an example where he'd convert a small structure to a string and back. If he had asked how to store a class or a collection, I'd answered serialization.

                            BillWoodruff wrote:

                            And, how about using Mehdi Gholam's zippy implementation of JSON on steroids here on CP ? Over-kill ?

                            I'm not familiar with the article, but it sounds like a viable alternative.

                            Bastard Programmer from Hell :suss:

                            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