Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Needing a better way to layout code. 1

Status
Not open for further replies.

MrDataGuy

Programmer
Oct 18, 2010
231
US
First of all this question not one of those: “if I do not get an answer to my question then my application will not work, and the word will end” situation. It is in the “There has to be a better way” category.

Ok here goes I have some screens/forms that have some ‘repeating’ code in them and I am looking for a better way of laying out the code. Example:

Code:
        Me.Route_Type1.Combobox_Populate()
        Me.Route_Type2.Combobox_Populate()
        Me.Route_Type3.Combobox_Populate()
        Me.Route_Type4.Combobox_Populate()
        Me.Route_Type5.Combobox_Populate()
        Me.Route_Type6.Combobox_Populate()
        Me.Route_Type7.Combobox_Populate()
        Me.Route_Type8.Combobox_Populate()
        Me.Route_Type9.Combobox_Populate()
        Me.Route_Type10.Combobox_Populate()

        Me.Route_Type11.Combobox_Populate()
        Me.Route_Type12.Combobox_Populate()
	     …..

Is there some way to write something like:

Code:
    For Each rt as Route_Type in Me
       Rt. Combobox_Populate
    ENDFOR

I suspect that there is, but I have yet to learn it.
BTW this is the 2008 (3.0) version of vb.Net


Lion Crest Software Services
Anthony L. Testi
President
 
What is a "Route_Type"? How does the "Combobox_Populate" method know which Route_Type it is populating (or is it okay that each object will have the same set of values)? One way to do this may be to add event handlers to each Route_Type...
Code:
Public Class Form1

    Public Event PopulateRouteTypeComboBoxes()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler Me.PopulateRouteTypeComboBoxes, AddressOf Route_Type1.Combobox_Populate
        AddHandler Me.PopulateRouteTypeComboBoxes, AddressOf Route_Type2.Combobox_Populate
        '...
        AddHandler Me.PopulateRouteTypeComboBoxes, AddressOf Route_Type12.Combobox_Populate
    End Sub

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

End Class
Alternatively, the following code may be closer to what you were trying to achieve...
Code:
    Private Sub AlternatePopulateMethod()
        For Each rt As Object In Me.Controls
            If (TypeOf rt Is Route_Type) Then CType(rt, Route_Type).Combobox_Populate()
        Next
    End Sub
 
Dave.
The "TypeOf rt Is Route_Type" is the magic pixe dust code that I was looking for, thank you.

As for things like "Route_Type"? How does the "Combobox_Populate" method know which Route_Type it is populating (or is it okay that each object will have the same set of values)? One way to do this may be to add event handlers to each Route_Type..." All I can say in my 'defense' is that I grabbed some example code and it may not have been the best example. You have brought up some good questions, and other possible codes/structure changes that I need to ponder. That is the benifit of code review.

Lion Crest Software Services
Anthony L. Testi
President
 
You said you prefer:
For Each rt as Route_Type in Me
Rt. Combobox_Populate
ENDFOR

That would be like:
For Each rt as Route_Type in Me.Route_Types
Rt. Combobox_Populate
NEXT

Where Me.Route_Types could be a List or Array of Route_Type.
When you create the instances, you add or set them into the
collectors. DO NOT name them as individual variables (Route_Type1, Route_Type2, etc.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top