Zoals jullie vast en zeker weten komt het als ontwikkelaar vaak voor dat we code schrijven waarvan je denkt, zou dat nou niet makkelijker kunnen? Ik ben nu op mijn werk bezig met geavanceerde software, waarbij je de waardes van form controls dynamisch moet kunnen aanpassen. Dit klinkt niet heel erg ingewikkeld, maar zoals jullie hieronder aan de code kunnen zien zit het nog best lastig in elkaar. Dit is mijn code:
ObjectValue (baseclass)
TextBoxValue class:
Om de waardes dynamisch aan te passen gebruik ik nu bijvoorbeeld de volgende code:
TextBoxValue is een door mij gemaakte class die door middel van reflection de waarde van een textbox kan aanpassen. Het resultaat van de bovenstaande code is dat textbox1 de tekst "GoT rules" krijgt. Op zich werkt deze code goed, maar ik moet nu wel voor ieder type control (label, textbox etc) een eigen class maken, wat natuurlijk niet handig is aangezien er aardig wat verschillende controls zijn.
Ik zit eraan te denken om het op te lossen door middel van generics, dus dat je bijvoorbeeld zoiets zou kunnen doen:
Ik weet niet zeker of dit de beste oplossing is, wat voor design pattern zouden jullie hiervoor gebruiken?
ObjectValue (baseclass)
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| public abstract class ObjectValue { /// <summary> /// Readonly property for setting the Text property of a TextBox (needed for reflection) /// View http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox%28v=vs.71%29.aspx for details /// </summary> protected readonly string _TextPropertName = "Text"; /// <summary> /// Private property that sets or returns the private variable _Content /// </summary> private char[] _Content; protected string Content { get { if (_Content == null) { new NullReferenceException("Content property cannot be null"); } return new string(_Content); } set { _Content = value.ToCharArray(); } } } |
TextBoxValue class:
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
| public class TextBoxValue : ObjectValue { /// <summary> /// Private variable that store the TextBox /// </summary> private TextBox _TextBox; /// <summary> /// Private property that sets or returns the private _TextBox /// </summary> private TextBox TextBox { get { if (_TextBox == null) { throw new NullReferenceException("TextBox property cannot be null"); } return _TextBox; } set { _TextBox = value; } } /// <summary> /// Constructor for TextBoxValue class /// </summary> /// <param name="textBox">The Textbox that should correspond to the TextBoxValue class</param> public TextBoxValue(TextBox textBox) { TextBox = textBox; } /// <summary> /// Sets the value of the textbox with the specified string /// </summary> /// <param name="content">The content to show in the textbox</param> public void SetTextBoxValue(string content) { // Story value in private variable Content = content; // Set value to the textbox PropertyInfo propertInfo = typeof(TextBox).GetProperty(_TextPropertName); propertInfo.SetValue(TextBox, Content, null); } /// <summary> /// Returns the textbox that corresponds with the specified TextBoxValue class /// </summary> public TextBox GetTextBox() { return TextBox; } } |
Om de waardes dynamisch aan te passen gebruik ik nu bijvoorbeeld de volgende code:
C#:
1
2
3
4
5
6
7
| public Form1() { InitializeComponent(); TextBoxValue value = new TextBoxValue(textBox1); value.SetTextBoxValue("GoT rules"); } |
TextBoxValue is een door mij gemaakte class die door middel van reflection de waarde van een textbox kan aanpassen. Het resultaat van de bovenstaande code is dat textbox1 de tekst "GoT rules" krijgt. Op zich werkt deze code goed, maar ik moet nu wel voor ieder type control (label, textbox etc) een eigen class maken, wat natuurlijk niet handig is aangezien er aardig wat verschillende controls zijn.
Ik zit eraan te denken om het op te lossen door middel van generics, dus dat je bijvoorbeeld zoiets zou kunnen doen:
C#:
1
2
3
4
5
6
7
| public Form1() { InitializeComponent(); ObjectValue<TextBox> value = new ObjectValue<TextBox>(textBox1); value.SetTextBoxValue("GoT rules"); } |
Ik weet niet zeker of dit de beste oplossing is, wat voor design pattern zouden jullie hiervoor gebruiken?