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