Write Own “Add” Method For An Array

2019-09-05 01:51发布

问题:

I would like to write a .Add method for an declared array.

The logic would be that if you code something like this :

 Sub Main()

    Dim names(2) As String

    names(0) = "john"
    names(1) = "jane"
    names(2) = "mary"
End Sub

That you could call the Add method and give a name as parameter to add an element. The element with the extra should be added +1 then the previous highest index. So in the case above...if you would say :

add(Liz)

Then the output must be :

names(0) = "john"
names(1) = "jane"
names(2) = "mary"
names(3) = "Liz"

Thanx in advance

回答1:

You could write an extension method for string arrays, like this:

Public NotInheritable Class ArrayExtensions
    Private Sub New()

    End Sub

    <System.Runtime.CompilerServices.Extension> _
    Public Shared Sub Add(Of T)(theArray As T(), t As T)
        Dim newSize As Integer = theArray.Length + 1
        Array.Resize(theArray, newSize)
        theArray(newSize - 1) = t
    End Sub
End Class

Then you could call it, like this:

Dim SomeArray(5) as String
SomeArray.Add(SomeElementAsString)

Unfortunately, while this will work for your usage needs, the array is not a reference so the alteration done in the extension method is trashed, because it is just working on a copy. This is why most people will recommend using List(Of T), because it has the reference-based Add method built-in, but you do not want that so you only real option is this:

Public NotInheritable Class ArrayUtilities
    Private Sub New()

    End Sub

    Public Shared Sub Add(Of T)(ByRef theArray As T(), t As T)
        Dim newSize As Integer = theArray.Length + 1
        Array.Resize(theArray, newSize)
        theArray(newSize - 1) = t
    End Sub
End Class

Here is how you can use the above code:

Dim SomeArray(5) as String
ArrayUtilities.Add(SomeArray, SomeElementAsString)


回答2:

I have Finnaly found it. So here is the deal...

So to make this happen we need a made a class with a object name as Employee. Employee has the members like a Name as a string...and a section as a string...

See it as a employee in a company that has a name ..and works in a seperated section..

code here:

Public Class Employee

    Public Property Name As String
    Public Property Section As String
End Class

Ofcourse now the thing for the array. In this example we will make an array that can hold diffrent objects of the type employee. To do this we will make a class that we will call EmployeeCollection. Ofcourse in that class we will need a field that contains a variable that holds an array of objects of the type employee(1) Now for the method that i wanted to Add an item i wrote the sub AddItem with a parameter of the type employee(2).

Public Class EmployeeCollection
    Public Items As Employee() = {}                '(1)
    Public Sub AddItem(item As Employee)           '(2)
        ReDim Preserve Items(Items.Length)
        Items(Items.Length - 1) = item
    End Sub
End Class

Now to establish and use this code I made an consoleapplication and in the Module1 I typed my code in the Sub Main.

In that sub main We have to declare a variable of the object type employeeCollection. This is needed so we can acces the AddItem asswell as the Items() array. In this case I call it anCollection (3).

ofcourse before we can add some objects of the type Employee we need to create them first. That is why i made the employee1 and employee2. Notice that the with .name and .section fills up the members of the class Employee(4).

Then it's just about time to call the anCollection and at a item to it. I added two of them.

Then the last task is to look what is in the array. And if you look good you can see objects are stored in the public field Items

So we need to call it by the object anCollection. If we run this..we will see all the objects added to the list.

Module Module1
    Sub Main()

        Dim anCollection As New EmployeeCollection()        '(3)


        '(4)
        Dim employee1 As New Employee With {.Name = "John", .Section = "Reception"}
        Dim employee2 As New Employee With {.Name = "Maria", .Section = "Catering Services"}

        anCollection.AddItem(employee1)
        anCollection.AddItem(employee2)
        Console.WriteLine(anCollection.Items(0).Name & " " & anCollection.Items(0).Section)

        Console.WriteLine(anCollection.Items(1).Name & " " & anCollection.Items(1).Section)

        Console.ReadLine()




    End Sub
End Module

This was What i needed... It's very nice if you don't want to use a list-of

ofcourse its easier to use a list of...but what if you want to do it your own way ? use this peace fosa