简体   繁体   English

为数组编写自己的“添加”方法

[英]Write Own “Add” Method For An Array

I would like to write a .Add method for an declared array. 我想为声明的数组编写一个.Add方法。

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. 您可以调用Add方法并将名称作为参数来添加元素。 The element with the extra should be added +1 then the previous highest index. 带有额外元素的元素应该加上+1然后是前一个最高索引。 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 Thanx提前

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: 这就是为什么大多数人会建议使用List(Of T) ,因为它有内置的基于引用的Add方法,但是你不希望这样,所以你只有这样的实际选项:

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)

I have Finnaly found it. 我有Finnaly找到它。 So here is the deal... 所以这里是交易...

So to make this happen we need a made a class with a object name as Employee. 因此,要实现这一点,我们需要创建一个具有对象名称为Employee的类。 Employee has the members like a Name as a string...and a section as a string... Employee的成员喜欢Name作为字符串......以及作为字符串的部分......

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. 为此,我们将创建一个我们将调用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). 当然,在那个类中,我们需要一个包含变量的字段,该变量包含employee类型的对象数组(1)现在,对于我想要添加项的方法,我使用employee类型的参数编写了子AddItem( 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. 现在建立并使用这个代码我做了一个控制台应用程序,在Module1中我在Sub Main中键入了我的代码。

In that sub main We have to declare a variable of the object type employeeCollection. 在那个sub main中我们必须声明一个对象类型employeeCollection的变量。 This is needed so we can acces the AddItem asswell as the Items() array. 这是必需的,因此我们可以作为Items()数组访问AddItem asswell。 In this case I call it anCollection (3). 在这种情况下,我称之为anCollection(3)。

ofcourse before we can add some objects of the type Employee we need to create them first. 在我们可以添加一些Employee类型的对象之前我们需要先创建它们。 That is why i made the employee1 and employee2. 这就是我创造employee1和employee2的原因。 Notice that the with .name and .section fills up the members of the class Employee(4). 请注意,with .name和.section填充了Employee(4)类的成员。

Then it's just about time to call the anCollection and at a item to it. 然后就是时候调用anCollection和一个项目了。 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. 所以我们需要通过对象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 用这个和平的fosa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM