Call a function whos name is in a variable??

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
hi, i would like to call a funtion whos name is in a vairable.

VB.NET:
Dim Car_service As String

Private Sub ComboBox_Spray_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox_Spray.SelectedIndexChanged
            Car_service = ("Spray_" & Me.ComboBox_Spray.Text)
end sub
        
Call spray_Standard [COLOR=red]'this is what i need to change standard is what comes from the combobox[/COLOR]
[COLOR=palegreen][COLOR=red]'[/COLOR][COLOR=red]but i would like to call the funtion depending on what the combobox.text was eg,
' if combobox.text was Parkingsensors i would like to call the function call spray_parkingSensors[/COLOR][/COLOR]

Private Sub Spray_Standard()
        Price_Subtotal = 0
        Price_Subtotal = (Damagecount * 65)
        Car_Price_Subtotal.Value = Price_Subtotal
    End Sub
 
Not the best idea. You should use delegates instead. Define a type to represent an item in the ComboBox that can store the text to display and the delegate to execute, e.g.
VB.NET:
Friend Class ExecutableItem
    Public Property Text() As String
    Public Property Method() As Action
End Class
Next create a List of instances of that type and bind it to the ComboBox:
VB.NET:
Dim executableItems As New List(Of ExecutableItem)

executableItems.Add(New ExecutableItem With {.Text = "Standard", .Method = AddressOf Spray_Standard})
'Not sure whether the Action constructor is explicitly required or not.
executableItems.Add(New ExecutableItem With {.Text = "Parking Sensors", .Method = New Action(AddressOf Spray_ParkingSensors)})

With ComboBox_Spray
    .DisplayMember = "Text"
    .ValueMember = "Method"
    .DataSource = executableItems
End With
Finally, when an item is selected, execute the method:
VB.NET:
Dim method = DirectCast(ComboBox_Spray.SelectedValue, Action)

method.Invoke()
 
hi, and thanks for the code it does work for the two you have already done, im going to try and finishs it off hopefully it will all work fine. i must say some of the stuf you guys are doing is well over my head lol, but most of the stuff i know is self taught :S

Edit: just finished of the rest of the code and got it all working was confused what he with was all about at the bottom but after writing a few lines i realised it sets the properties to the combobox in question :d cherz does exactly what i need it to do, i havn't noticed any lag or anything but would their be a way to have the list already made rather than having it being made at an event,

i think i would have to change each name for each list
would i have to create a new object for each list?
and could i set the properties for the combobox in the form desinger instead of usinf the with statement

an example would be
Dim executableItems_Spray As New List(Of ExecutableItem)
executableItems_spray.Add(New ExecutableItem With {.Text = "Standard", .Method = AddressOf Spray_Standard})
executableItems_Spray.Add(New ExecutableItem With {.Text = "Parking Sensors", .Method = AddressOf Spray_Parking_Sensors})
executableItems_Spray.Add(New ExecutableItem With {.Text = "Wing Mirror", .Method = AddressOf Spray_Wing_Mirror})
executableItems_spray.Add(New ExecutableItem With {.Text = "Door Moulding", .Method = AddressOf Spray_Door_Moulding})
 
Last edited:
Sorry just come across another problem with this line of code
Dim method = DirectCast(ComboBox_Spray.SelectedValue, Action)
method.Invoke()

their are 5 diffrent Comboboxs ComboBox_Spray, Fiber, SSR, Aroma, Glass, would it be a case of if statements or is their a better way to determin which combobox the
 
i have started confusing my self so i made a text file with what im trying to achive in each step, so far i have got

5 radio buttons all hade to be seperatly coded because i have 1 raido button in a gorupbox within a groupbox so the groupbox didn't apply the one 1 checked rule. but when the radio button is checked is select the Service(ssr,Spray...,...) then calls a sub
VB.NET:
Private Sub SSR_List()
        Me.Damage_List.Items.Add("Front Bumper")
        Me.Damage_List.Items.Add("Rear Bumper")
        Me.Damage_List.Items.Add("Front O/S Wing")
        Me.Damage_List.Items.Add("Front N/S Wing")
        Me.Damage_List.Items.Add("Rear O/S Quarter Panel")
        Me.Damage_List.Items.Add("Rear N/S Quarter Panel")
        Me.Damage_List.Items.Add("Front O/S Door")
        Me.Damage_List.Items.Add("Front N/S Door")
        Me.Damage_List.Items.Add("Rear O/S Door")
        Me.Damage_List.Items.Add("Rear N/S Door")
        Me.Damage_List.Items.Add("Bonnet")
        Me.Damage_List.Items.Add("Tailgate")
        Me.Damage_List.Items.Add("Wing Mirror")
        Dim executableitems_SSR As New List(Of ExecutableItem)
        executableitems_SSR.Add(New ExecutableItem With {.Text = "Scratch", .Method = AddressOf SSR_Scratches})
        executableitems_SSR.Add(New ExecutableItem With {.Text = "Stone Chip", .Method = AddressOf SSR_Stone_chips})
        With ComboBox_SSR
            .DisplayMember = "Text"
            .ValueMember = "Method"
            .DataSource = executableitems_SSR
        End With
the sub populates a checklistbox with the damage desciption and adds the objects into the service comboxbox's eg stone chip or a scratch (mainly for invoice perposes and some diffrence in price)
then after the type of damage has been selected eg(ComboBox_fiber.SelectedValue, being a cigarette burn or a tear the code below is implemented

VB.NET:
Private Sub ComboBox_fiber_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox_fiber.SelectedIndexChanged
        Dim method = DirectCast(ComboBox_fiber.SelectedValue, Action)
        method.Invoke()
    End Sub

my problem is the sub that it calls, holds the formulas for woring out the price. im stuck because i tried to use the selected index event but it will only call the event once, so the formulas are being carried out before the user can start adding the damage. so i tried putting it in the damagelist itemchecked event, the formulas are carried out but u can't check an item that isn't in the list. so think i have to go back to the drawing board with the what get put into what sub, i would be very grateful if you would teamview and give a second opinion, i would sned u the app but i can't garantee virus free, and don't want to take that risk on a helpful forum lol.

i tried to use a case select but wouldn't work as planed lol
 
0k sorted it, i removed the dim method from the combobox and made it a declaired it in the for class so when selecting the combox to use it will set the method and the combobox.slectedchanged carryies out the just method.invoke() and the same for the damage_list
 
Last edited by a moderator:
SOLVED by JoshdbrCall a function whos name is in a variable??

Ok that code made hell appear in my programe it was just messing up big time. but joshdbr came and assisted with teamvier and fixed it
 
Back
Top