Re: Newbie Advice/Critiscism



MikeD wrote:
"Argusy" <argusy@xxxxxxxxxxxxxxx> wrote in message news:467CF968.6020103@xxxxxxxxxxxxxxxxxx

Darren (UK) wrote:

Hi Guys,

OK this is my very first attempt at development and whilst it does what i want it to i know that in actual the fact the coding is awful. The first thing you will notice is the amount of repetition of similiar code that does the same thing.

Im hoping that someone will take the time to look at the code and offer some advice as to how to curtail the amount of lines of code. As it is obvious to even the naivest coder there is some way of eliminating the repetition. Im not asking for someone to rewrite the thing, what i am after is someone to try explain the error of my ways and show me some ways of cleaning the code up, as im sure that there will be occasions where i will be able to apply lessons learnt.

Thanks

Darren


<snip> - we don't need a repeat

The coding is unfamiliar to me, so I guess this could be a "dotnet" program, not classic VB.

All the same, you should look at creating arrays of checkboxes and textboxes.



That is indeed VB.NET code. Assuming you mean control arrays, there's no such thing in .NET. Instead, you have procedures handle events for multiple controls. For example, these two event procedures:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If TabControl1.SelectedTab.Name = "TabPage1" Then
CheckBox1.Checked = True
CheckBox2.Checked = True
CheckBox3.Checked = True
CheckBox4.Checked = True
CheckBox5.Checked = True
CheckBox6.Checked = True
CheckBox7.Checked = True
CheckBox8.Checked = True
CheckBox9.Checked = True
CheckBox10.Checked = True
CheckBox11.Checked = True
CheckBox12.Checked = True
CheckBox13.Checked = True
Else
CheckBox14.Checked = True
CheckBox15.Checked = True
CheckBox16.Checked = True
CheckBox17.Checked = True
End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

If TabControl1.SelectedTab.Name = "TabPage1" Then
CheckBox1.Checked = False
CheckBox2.Checked = False
CheckBox3.Checked = False
CheckBox4.Checked = False
CheckBox5.Checked = False
CheckBox6.Checked = False
CheckBox7.Checked = False
CheckBox8.Checked = False
CheckBox9.Checked = False
CheckBox10.Checked = False
CheckBox11.Checked = False
CheckBox12.Checked = False
CheckBox13.Checked = False
Else
CheckBox14.Checked = False
CheckBox15.Checked = False
CheckBox16.Checked = False
CheckBox17.Checked = False
End If


End Sub


could be "combined" into a single event procedure like this:

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click, Button2.Click

If TabControl1.SelectedTab.Name = "TabPage1" Then
CheckBox1.Checked = False
CheckBox2.Checked = False
CheckBox3.Checked = False
CheckBox4.Checked = False
CheckBox5.Checked = False
CheckBox6.Checked = False
CheckBox7.Checked = False
CheckBox8.Checked = False
CheckBox9.Checked = False
CheckBox10.Checked = False
CheckBox11.Checked = False
CheckBox12.Checked = False
CheckBox13.Checked = False
Else
CheckBox14.Checked = False
CheckBox15.Checked = False
CheckBox16.Checked = False
CheckBox17.Checked = False
End If


End Sub



Thanks, Mike.
God, no wonder I'm not interested in .dotnetting
That's a complete re-structure of "How To Do It" - definitely not VB classic
(But I LIKE arrays - it cuts programming code tremendously)

Graham

.


Loading