Carlos Armendia - 5 months ago 9
Vb.net Question

# Calling the same function with different functions called inside

I hope my title is clear. Let me explain. I have 4 buttons that do pretty much the same.

``````Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
LimiteGlobalSeparador = InputBox("Introduzca la distancia mÃ¡xima entre huecos en el tubo", "Cuadro de Datos, Separaciones", "")
Dim a, b, c, d, f, g As New Label
Dim o, p, q, r, s, t As New Label
a.Text = "Medida del Tubo"
b.Text = "Espacio 1"
c.Text = "Espacio 2"
d.Text = "Espacio 3"
f.Text = "Espacio 4"
g.Text = "Espacio 5"
For i = 0 To 5
Next
If TextBox1.Text <> 0 Then
For j = 1 To Int(TextBox1.Text)
Dim x As New List(Of Decimal)
x = MedTuboFuncPCT()
For i = 0 To x.Count - 1
Dim lbl As New Label
lbl.Text = Math.Round(x(i), 2)
Next
Next
Else
End If

Dim h As Integer
h = Int(TextBox2.Text - TextBox1.Text)
If TextBox2.Text <> 0 Then
If h = Int(TextBox2.Text) Then
For j = 1 To Int(TextBox2.Text)
Dim x As New List(Of Decimal)
x = MedTuboFunCTCT()
For i = 0 To x.Count - 1
Dim lbl As New Label
lbl.Text = Math.Round(x(i), 2)
Next
Next
Else
For j = Int(1) + Int(TextBox1.Text) To (Int(TextBox1.Text) + Int(TextBox2.Text))
Dim x As New List(Of Decimal)
x = MedTuboFunCTCT()
For i = 0 To x.Count - 1
Dim lbl As New Label
lbl.Text = Math.Round(x(i), 2)
Next
Next
End If
End If

If TextBox3.Text <> 0 Then
Dim x As New List(Of Decimal)
x = MedTuboFuncPP()
For i = 0 To x.Count - 1
Dim lbl As New Label
lbl.Text = Math.Round(x(i), 2)
Next
End If
If Te180 > 0 Then
o.Text = "Medida del Tubo"
p.Text = "Espacio 1"
q.Text = "Espacio 2"
r.Text = "Espacio 3"
s.Text = "Espacio 4"
t.Text = "Espacio 5"
For i = 6 To 11
Next
End If
End Sub
``````

So I'm trying to create a Sub and then each button call that sub, so I don't have repeating code in each button like I have now. The only change is here:

One button calls this function.
`x = MedTuboFuncPCT().`

Another one calls
`x = MedTuboFuncPCTVid()`

Another one calls
`x = MedTuboFuncPCTVidBB()`

I don't know how to make a Sub in which I can make this difference. Hope I'm clear enough. Thanks in advance.

Create your function and send a value in it so you can change the `x` function

``````Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
VFunction = 1
RepeatedCode(VFunction)
End Sub
``````

On Button2 `VFunction = 2` and so on. Then create your function.

``````Public Sub RepeatedCode (ByVal VFunction as integer)
If VFunction = 1 Then
x = MedTuboFuncPCT()
ElseIf VFunction = 2 Then
x = MedTuboFuncPCTVid()
ElseIf VFunction = 3 Then
x = MedTuboFuncPCT()
End If

I belive the `If` condition must be placed inside your `If TextBox1.Text <> 0` Since it its the only place where I see you used one of the Functions you mentioned