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. Different object depending on variable value

Different object depending on variable value

Scheduled Pinned Locked Moved C#
tutorialdatabasequestion
8 Posts 5 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
    suzyb
    wrote on last edited by
    #1

    I am trying to implement a multi page form on a site that will be used for 3 different types of applicant. Each of these 3 types will fill in their personal details before filling in different sections of the form depending on type. I currently have a base class with the shared personal details and derived classes for the 3 specific types. In my form however I don't know how to create the correct object based on the type of applicant without having a lot of duplicate code. Here is a cut down example of my code. This is an example of the classes

    public class BaseApplication
    {
    public bool FirstName { get; set; }
    public bool LastName { get; set; }

    public virtual void Save()
    {
        // write first name and last name to database
    }
    

    }

    public class StudentApplication : BaseApplication
    {
    public bool SupportNeeds { get; set; }

    public override void Save()
    {
        base.Save();
    
        // write support needs to database
    }
    

    }

    And this the part of the page where I want to create the object.

    ?? app;
    switch (courseType)
    {
    case "31":
    app = new StudentApplication();
    break;
    case "32":
    app = new AdultApplication();
    break;
    }

    app.FirstName = FirstName.Text;
    app.LastName = LastName.Text;
    app.SupportNeeds = SupportNeeds.Text;

    app.Save();

    I don't know what to declare app as as it can be one of 3 types depending on the value of courseType.

    OriginalGriffO P L J 4 Replies Last reply
    0
    • S suzyb

      I am trying to implement a multi page form on a site that will be used for 3 different types of applicant. Each of these 3 types will fill in their personal details before filling in different sections of the form depending on type. I currently have a base class with the shared personal details and derived classes for the 3 specific types. In my form however I don't know how to create the correct object based on the type of applicant without having a lot of duplicate code. Here is a cut down example of my code. This is an example of the classes

      public class BaseApplication
      {
      public bool FirstName { get; set; }
      public bool LastName { get; set; }

      public virtual void Save()
      {
          // write first name and last name to database
      }
      

      }

      public class StudentApplication : BaseApplication
      {
      public bool SupportNeeds { get; set; }

      public override void Save()
      {
          base.Save();
      
          // write support needs to database
      }
      

      }

      And this the part of the page where I want to create the object.

      ?? app;
      switch (courseType)
      {
      case "31":
      app = new StudentApplication();
      break;
      case "32":
      app = new AdultApplication();
      break;
      }

      app.FirstName = FirstName.Text;
      app.LastName = LastName.Text;
      app.SupportNeeds = SupportNeeds.Text;

      app.Save();

      I don't know what to declare app as as it can be one of 3 types depending on the value of courseType.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Create it as the base type: that way it can hold all of the derived types:

      public abstract class myBase {...}
      public class derived1 : myBase {...}
      public class derived2 : myBase {...}
      ...
      myBase app;
      app = new derived1();
      app = new derived2();
      ...

      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      P 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Create it as the base type: that way it can hold all of the derived types:

        public abstract class myBase {...}
        public class derived1 : myBase {...}
        public class derived2 : myBase {...}
        ...
        myBase app;
        app = new derived1();
        app = new derived2();
        ...

        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Won't work I'm afraid. The OP has properties in the derived types that won't work here because they are more specialised.

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        1 Reply Last reply
        0
        • S suzyb

          I am trying to implement a multi page form on a site that will be used for 3 different types of applicant. Each of these 3 types will fill in their personal details before filling in different sections of the form depending on type. I currently have a base class with the shared personal details and derived classes for the 3 specific types. In my form however I don't know how to create the correct object based on the type of applicant without having a lot of duplicate code. Here is a cut down example of my code. This is an example of the classes

          public class BaseApplication
          {
          public bool FirstName { get; set; }
          public bool LastName { get; set; }

          public virtual void Save()
          {
              // write first name and last name to database
          }
          

          }

          public class StudentApplication : BaseApplication
          {
          public bool SupportNeeds { get; set; }

          public override void Save()
          {
              base.Save();
          
              // write support needs to database
          }
          

          }

          And this the part of the page where I want to create the object.

          ?? app;
          switch (courseType)
          {
          case "31":
          app = new StudentApplication();
          break;
          case "32":
          app = new AdultApplication();
          break;
          }

          app.FirstName = FirstName.Text;
          app.LastName = LastName.Text;
          app.SupportNeeds = SupportNeeds.Text;

          app.Save();

          I don't know what to declare app as as it can be one of 3 types depending on the value of courseType.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          The big issue you have here is that properties in the derived type aren't present in the base type, so you can't really declare app as BaseApplication. Well, you can, but you need to cast to the derived type to access the properties. What you could do is something like this though:

          BaseApplication app = null;
          switch (courseType)
          {
          case "31":
          app = CreateStudentApplication();
          break;
          }

          if (app != null)
          {
          app.Save();
          }
          ....

          private StudentApplication CreateStudentApplication()
          {
          StudentApplication app = new StudentApplication();
          SetBaseInformation(app);
          app.SupportNeeds = SupportNeeds.Text;
          return app;
          }

          private void SetBaseInformation(BaseApplication application)
          {
          application.FirstName = FirstName.Text;
          application.LastName = LastName.Text;
          }

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          1 Reply Last reply
          0
          • S suzyb

            I am trying to implement a multi page form on a site that will be used for 3 different types of applicant. Each of these 3 types will fill in their personal details before filling in different sections of the form depending on type. I currently have a base class with the shared personal details and derived classes for the 3 specific types. In my form however I don't know how to create the correct object based on the type of applicant without having a lot of duplicate code. Here is a cut down example of my code. This is an example of the classes

            public class BaseApplication
            {
            public bool FirstName { get; set; }
            public bool LastName { get; set; }

            public virtual void Save()
            {
                // write first name and last name to database
            }
            

            }

            public class StudentApplication : BaseApplication
            {
            public bool SupportNeeds { get; set; }

            public override void Save()
            {
                base.Save();
            
                // write support needs to database
            }
            

            }

            And this the part of the page where I want to create the object.

            ?? app;
            switch (courseType)
            {
            case "31":
            app = new StudentApplication();
            break;
            case "32":
            app = new AdultApplication();
            break;
            }

            app.FirstName = FirstName.Text;
            app.LastName = LastName.Text;
            app.SupportNeeds = SupportNeeds.Text;

            app.Save();

            I don't know what to declare app as as it can be one of 3 types depending on the value of courseType.

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

            Psst.. strategy pattern! ..and you might want to program against a simple and small (or even multiple) interfaces :)

            Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

            S 1 Reply Last reply
            0
            • L Lost User

              Psst.. strategy pattern! ..and you might want to program against a simple and small (or even multiple) interfaces :)

              Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

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

              I thought of an interface but couldn't see how it would work. Strategy pattern sounds like a proper programming thing and not something a hack merchant would be able to do :-O

              L 1 Reply Last reply
              0
              • S suzyb

                I thought of an interface but couldn't see how it would work. Strategy pattern sounds like a proper programming thing and not something a hack merchant would be able to do :-O

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

                suzyb wrote:

                I thought of an interface but couldn't see how it would work.

                You can have them at property-level; considered an IFirstName? Keep hacking, and you'll learn it as you go. Just a matter of time before you stumble over it :)

                Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                1 Reply Last reply
                0
                • S suzyb

                  I am trying to implement a multi page form on a site that will be used for 3 different types of applicant. Each of these 3 types will fill in their personal details before filling in different sections of the form depending on type. I currently have a base class with the shared personal details and derived classes for the 3 specific types. In my form however I don't know how to create the correct object based on the type of applicant without having a lot of duplicate code. Here is a cut down example of my code. This is an example of the classes

                  public class BaseApplication
                  {
                  public bool FirstName { get; set; }
                  public bool LastName { get; set; }

                  public virtual void Save()
                  {
                      // write first name and last name to database
                  }
                  

                  }

                  public class StudentApplication : BaseApplication
                  {
                  public bool SupportNeeds { get; set; }

                  public override void Save()
                  {
                      base.Save();
                  
                      // write support needs to database
                  }
                  

                  }

                  And this the part of the page where I want to create the object.

                  ?? app;
                  switch (courseType)
                  {
                  case "31":
                  app = new StudentApplication();
                  break;
                  case "32":
                  app = new AdultApplication();
                  break;
                  }

                  app.FirstName = FirstName.Text;
                  app.LastName = LastName.Text;
                  app.SupportNeeds = SupportNeeds.Text;

                  app.Save();

                  I don't know what to declare app as as it can be one of 3 types depending on the value of courseType.

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #8

                  suzyb wrote:

                  I don't know how to create the correct object based on the type of applicant without having a lot of duplicate code.

                  Generic creation problems are often solved with the Factory Pattern. Other than that I don't know what you mean by "duplicate" code. If the objects are different than they are different and you shouldn't attempt to generalize. If however the are the related (by design and NOT by your desire to avoid typing) then you should perhaps re-think your design/implementation.

                  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