将自己的“添加”数组方法 [英] Write Own "Add" Method For An Array

查看:134
本文介绍了将自己的“添加”数组方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为一个声明的数组编写一个.Add方法。

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

您可以调用Add方法,并以名称作为参数来添加元素。
带有多余元素的元素应加+1,然后加上之前的最高索引。
因此,在上述情况下...如果您会说:

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)

然后输出必须为:

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

预先感谢

推荐答案

我已经找到Finnaly。因此,这就是交易。

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

因此,要实现这一点,我们需要一个对象名称为Employee的类。
Employee的成员像一个Name一样是一个字符串...以及一个section一样是一个字符串...

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..

代码在这里:

Public Class Employee

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

现在当然是数组的东西了。在此示例中,我们将创建一个数组,该数组可以容纳employee类型的不同对象。为此,我们将创建一个称为EmployeeCollection的类。当然,在该类中,我们将需要一个包含变量的字段,该变量包含一个类型为employee(1)
的对象的数组。现在,我要添加项目的方法是用参数编写了一个子AddItem。类型为employee(2)。

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

现在建立并使用此代码,我制作了一个consoleapplication,并在Module1中b $ b我在Sub Main中键入了代码。

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

在该Sub Main中,我们必须声明一个对象类型employeeCollection的变量。
这是必需的,因此我们可以将AddItem设置为Items()数组。在这种情况下,我将其称为anCollection(3)。

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).

当然,在添加一些类型为Employee的对象之前,我们首先需要创建它们。
这就是为什么我制作了employee1和employee2的原因。注意,用.name和.section填充了Employee(4)类的成员。

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).

然后就该调用anCollection了,并在它。
我添加了其中两个。

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

然后,最后一个任务是查看数组中的内容。如果看起来不错,您会看到对象存储在公共字段Items

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

中,因此我们需要通过对象anCollection对其进行调用。如果运行此命令,我们将看到所有添加到列表中的对象。

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

这篇关于将自己的“添加”数组方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆