How not to repeat the same code on an overload?

Asked

Viewed 68 times

1

What would be correct technique not to repeat the same code for these two overloads or maybe not to have the overloads?

It happens that sometimes I have to pass a parameter that is a string array and other times I pass integer array. If I don’t type the parameters, does not work, because the compiler complains that it cannot convert integer to object.

 Private Sub CarregarArraysPeloDgv(ByRef dgv As DataGridView, ByRef a() As String)

        ' REDIMENSIONAR
        If a Is Nothing Then
            ReDim a(dgv.Rows.Count - 1)
            Exit Sub
        End If

        If dgv.Rows.Count > a.Count Then
            ReDim a(dgv.Rows.Count - 1)
        End If

        ' ALIMENTAR
        Dim x As Integer = 0
        For Each row As DataGridViewRow In dgv.Rows
            a(x) = Val(row.Cells(0).Value)
            x = x + 1
        Next

    End Sub

    Private Sub CarregarArraysPeloDgv(ByRef dgv As DataGridView, ByRef a() As Integer)
        ' REDIMENSIONAR
        If a Is Nothing Then
            ReDim a(dgv.Rows.Count - 1)
            Exit Sub
        End If

        If dgv.Rows.Count > a.Count Then
            ReDim a(dgv.Rows.Count - 1)
        End If

        ' ALIMENTAR
        Dim x As Integer = 0
        For Each row As DataGridViewRow In dgv.Rows
            a(x) = Val(row.Cells(0).Value)
            x = x + 1
        Next
    End Sub
  • Every type derives from 'Object' in . Net, there is no way to caste the int for obj?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

In this case you must use Generics and no overload, so you need to parameterize the guy, so when you call, you tell me what kind you’re gonna use. I do not know if in VB.NET has inference, in C# I know it is common not to need to indicate explicitly. In some cases the genericity must be in the class and only parameterize in the method, there has to see the whole context to decide whether it is better to enable only for the method or for the whole class.

Every time you wear one cast A panda dies in China.

Private Sub CarregarArraysPeloDgv(Of T)(ByRef dgv As DataGridView, ByRef a() As T)
    ' REDIMENSIONAR
    If a Is Nothing Then
        ReDim a(dgv.Rows.Count - 1)
        Exit Sub
    End If

    If dgv.Rows.Count > a.Count Then
        ReDim a(dgv.Rows.Count - 1)
    End If

    ' ALIMENTAR
    Dim x As Integer = 0
    For Each row As DataGridViewRow In dgv.Rows
        a(x) = Val(row.Cells(0).Value)
        x = x + 1
    Next
End Sub

I put in the Github for future reference.

  • Wow. Very good! Really worth it!

  • 1

    @Tito see in the [tour] the best way to say thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.