I have 9 strings in Format1 class, that I want convert into different types as seen in Format2 class, However 3 strings will still remain as string types. I decided to start playing with them until I got a code I was happy with.
As you see in my Form1.cs code all I want to do really upon the button click event is to call the getConvert() method and let it handle everything. Apperantly I am missing out on something. I have to use my ugly 6 lines to call everything up.
You see my non working attemps in my comments in the code. What did I do wrong this time ??
You can also grab my source here:
https://mega.co.nz/#!64QzERRR!Qit9SDZQ7kW7rNCAUUHHDRZUUvZY9z0ukgfuqVt00mE
public class Format1
{
public string Name { get; set; }
public string Year { get; set; }
public string Director { get; set; }
public string AverageRating { get; set; }
public string LeadingActor1 { get; set; }
public string LeadingActor2 { get; set; }
public string LeadingActor3 { get; set; }
public string Language { get; set; }
public string ImdbLink { get; set; }
}
public class Format2 : Format1
{
public int Year { get; set; }
public int AverageRating {get; set;}
public string LeadingActors { get; set; }
public bool IsInEnglish { get; set; }
public bool HasImdbLink { get; set; }
public Format2 getConvert()
{
Format2 converted = new Format2();
//converted.Name = textBox1.Text;
//textBox18.Text = converted.Name;
converted.Name = this.Name;
converted.Director = this.Director;
converted.ImdbLink = this.ImdbLink;
return converted;
}
}
namespace as3_DVDproject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Format2 converted = new Format2();
private void label1_Click(object sender, EventArgs e)
{
}
private void label8_Click(object sender, EventArgs e)
{
}
private void label9_Click(object sender, EventArgs e)
{
}
private void okButton_Click(object sender, EventArgs e)
{
//converted.getConvert();
converted.Name = textBox1.Text;
textBox18.Text = converted.Name;
converted.Director = textBox3.Text;
textBox16.Text = converted.Director;
converted.ImdbLink = textBox9.Text;
textBox10.Text = converted.ImdbLink;
}
}
}
The more object oriented pattern would be to either add a constructor to Format2 that takes a Format1, or give Format2 a static method that takes a Format1 and returns a Format2. The actual mapping code doesn’t really look all that verbose to me though, you could drop it in either one of those places.
You want the objects to know how to construct themselves rather than building them in the form.