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. Bit of design advice needed

Bit of design advice needed

Scheduled Pinned Locked Moved C#
csharpdesignregexxmlquestion
4 Posts 3 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.
  • R Offline
    R Offline
    relaeir
    wrote on last edited by
    #1

    I'm reading a book on "Prefactoring" and the author suggest converting primitive types to classes such as: CommonString State; CommonString ZipCode; You can't assign these without the 'new' keyword in C# though, right? So my idea was to create the types and load their verifying Regex's from a Xml file.

    G R R 3 Replies Last reply
    0
    • R relaeir

      I'm reading a book on "Prefactoring" and the author suggest converting primitive types to classes such as: CommonString State; CommonString ZipCode; You can't assign these without the 'new' keyword in C# though, right? So my idea was to create the types and load their verifying Regex's from a Xml file.

      G Offline
      G Offline
      Graham Nimbley
      wrote on last edited by
      #2

      :confused: Seems a strange move to me. For me, state and zip code are easily represented by string. And creating a wrapper for a primitive type is useless as at some point you will need to interact with the primitive type (in some manner). Classes can't be created without use the new keyword. (Although you can do with reflection) -- modified at 16:22 Sunday 25th June, 2006

      1 Reply Last reply
      0
      • R relaeir

        I'm reading a book on "Prefactoring" and the author suggest converting primitive types to classes such as: CommonString State; CommonString ZipCode; You can't assign these without the 'new' keyword in C# though, right? So my idea was to create the types and load their verifying Regex's from a Xml file.

        R Offline
        R Offline
        Richard Houltz
        wrote on last edited by
        #3

        I read your question and here are some of my thoughts from the top of my head... I think what you suggest would be a really good approach if you anticipate having a lot of validation and stuff going on. >You can't assign these without the 'new' keyword in C# though, right? That's right however you can hide the 'new' call inside an static public method in the class that you want to create. This is part of the approach when implementing the singleton pattern in C#. If you like you can add static methods that create your information classes if you think it is ugly with the 'new' keyword. By using this approach you can be more precise about how the object is initialized than if you use oveloaded constructors (where you would have to look up the parameters for the constructor). For instance StateInfo stateInfo = StateInfo.LoadFromXml("state.xml"); might be better than StateInfo stateInfo = new StateInfo("state.xml"); By using a class for each type of information you can also make use of an refactoring called the "Null Object". This means that instead of initializing your StateInfo members to null, you set them to StateInfo.Empty. Now any checks for null is unneccessary and you can implement default values for when the info is missing. For instance the ToString metod return "" or something. This can lead to simpler and more readable code. If you want to Load any validation RegEx or something for the class you could do that in a static constructor. A static constructor is called once (and only once) when the class is accessed the first time (even if it is through static functions). Some sample (pseudo-)code: class StateInfo { string m_StateName; public static StateInfo Empty = new StateInfo(""); public static StateInfo LoadFromXml(string xmlFileName) { StateInfo stateInfo = new StateInfo(""); //Init the stateInfo from file... return stateInfo; } public StateInfo(string stateName) { //Do instance initialization here m_StateName = stateName; } public static StateInfo() { //Called once //Do class initalization here } public override string ToString() { return m_StateName; } } So go ahead and use a storage class for your different types of information. I would however recommend that you don't "over design" your system to begin with. Instead go with a light design and refactor it as you go along. Just some advice as requested, I hope it makes sense!

        1 Reply Last reply
        0
        • R relaeir

          I'm reading a book on "Prefactoring" and the author suggest converting primitive types to classes such as: CommonString State; CommonString ZipCode; You can't assign these without the 'new' keyword in C# though, right? So my idea was to create the types and load their verifying Regex's from a Xml file.

          R Offline
          R Offline
          relaeir
          wrote on last edited by
          #4

          Thank you very much for the replies. :-D

          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