使用变量值从自定义类名创建新对象? [英] Making new object from custom class- name using a variable value?

查看:31
本文介绍了使用变量值从自定义类名创建新对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个列表来组织和操作代表电子表格中的行的数组.我为数组创建了一个自定义类,并将它们作为对象调用.

我的问题是,我可以使用存储在变量中的值作为对象的名称吗?如果是这样,语法会是什么样的?

dim FileName as String文件名 = 123456.csv公共类 List_Array公共变量 1 作为字符串公共变量 2 作为字符串公共变量 3 作为字符串公共变量 4 作为字符串结束类将 File_Name 调为 List_Array = NEW List_Array

这是我理解的编码,但我一直认为这只会一遍又一遍地创建一个与字符串变量同名的对象.

如果不是,我如何区分不同的对象?将有数以千计的对象要引用,因此使用未命名的对象效果不佳.

解决方案

如果要存储命名对象列表,则需要一个 字典.Dictionary 对象存储键/值对列表.在这种情况下,键"是您要分配给对象的名称,值是对对象本身的引用.它是一个泛型类,这意味着当您使用 Dictionary 类型时,您必须指定您希望它用于其键和值的类型.例如,Dictionary(Of String, MyClass) 将创建一个列表,该列表使用 String 对象作为其键,使用 MyClass 对象作为其值.>

下面是一个示例,说明如何使用字典来存储带有年龄的人员列表:

Dim d As New Dictionary(Of String, Integer)()d("鲍勃") = 30d(玛丽")= 42

然后,当你想读取一个值时,你可以这样做:

Dim age As Integer = d("Bob")

Dictionary 每个键只允许一个项目.它使用哈希表通过键对值进行索引,因此可以非常快速地通过键获取任何项目.

编辑

根据您在下面的评论,这里有一个更相关的示例来说明我的意思.假设您有一个包含人员列表的 CSV 文件.因此,您创建了一个类来存储有关一个人的所有信息(CSV 文件的一行),如下所示:

公开课的人公共属性 ID 作为字符串公共属性名称作为字符串公共属性标题作为字符串结束类

然后,您创建一个方法,该方法解析 CSV 文件中的一行并将该行中的所有字段作为字符串数组返回,如下所示:

公共函数 ParseCsvLine(line As String) As String()' ...结束函数

然后,您可以将所有人员加载到列表中,如下所示:

将人员变暗为新列表(Of Person)()对于 File.ReadAllLines("People.csv") 中的每行作为字符串Dim fields() As String = ParseCsvLine(line)Dim person As New Person()person.Id = 字段(0)person.Name = 字段(1)person.Title = 字段(2)人.添加(人)下一个

现在您已将所有 Person 对象加载到一个列表中.但是,问题是该列表没有编入索引.例如,如果您需要找到 ID 为 100 的人,则需要遍历列表中的所有 Person 对象,直到找到一个在它的 Id 属性中包含该值.如果您想通过 ID 为它们编制索引以便更轻松/快速地找到它们,您可以使用 Dictionary,如下所示:

Dim people As New Dictionary(Of String, Person)()对于 File.ReadAllLines("People.csv") 中的每行作为字符串Dim fields() As String = ParseCsvLine(line)Dim person As New Person()person.Id = 字段(0)person.Name = 字段(1)person.Title = 字段(2)人(人.Id)=人下一个

然后,当你需要从字典中获取一个人的时候,你可以很容易地通过ID来访问它,就像这样:

Dim person100 As Person = people("100")

I am making a LIST to organize and manipulate arrays that represent lines off a spreadsheet. I've created a custom class for the arrays, and will call them out as objects.

My question is, can I use a value stored in a variable as the name of the object? If so, what would the syntax look like?

dim FileName as String
FileName = 123456.csv

Public Class List_Array
    public variable1 as string
    public variable2 as string
    public variable3 as string
    public variable4 as string
    End Class

dim File_Name as List_Array = NEW List_Array

This is the coding as I understand it, but I keep thinking this will only create one Object over and over again with the same name as the string variable.

If not, how can I differentiate the different objects as I call them? There will be thousands of objects to reference, so using an unnamed object will not work so well.

解决方案

If you want to store a list of named objects, what you need is a Dictionary. Dictionary objects store a list of key/value pairs. In this case, the "key" is the name that you want to assign to the object, and the value is the reference to the object itself. It's a generic class, which means when you use the Dictionary type, you must specify the types that you want it to use for it's keys and values. For instance Dictionary(Of String, MyClass) will create a list that uses String objects for its keys and MyClass objects for its values.

Here's an example of how you could use a dictionary to store a list of people with their ages:

Dim d As New Dictionary(Of String, Integer)()
d("Bob") = 30
d("Mary") = 42

Then, when you want to read a value, you can do it like this:

Dim age As Integer = d("Bob")

The Dictionary will only allow one item per key. It uses a hash table to index the values by their keys, so it's very fast get any item by its key.

Edit

Based on your comments below, here's a more pertinent example to show what I mean. Let's say you have a CSV file containing a list of people. So you create a class that stores all of the information about one person (one line of the CSV file), like this:

Public Class Person
    Public Property Id As String
    Public Property Name As String
    Public Property Title As String
End Class

Then, you create a method which parses a single line from the CSV file and returns all of the fields from that line as an array of strings, like this:

Public Function ParseCsvLine(line As String) As String()
    ' ...
End Function

Then, you could load all of the people into a list, like this:

Dim persons As New List(Of Person)()
For Each line As String In File.ReadAllLines("People.csv")
    Dim fields() As String = ParseCsvLine(line)        
    Dim person As New Person()
    person.Id = fields(0)
    person.Name = fields(1)
    person.Title = fields(2)
    persons.Add(person)
Next

Now you have all of the Person objects loaded into one list. The problem is, though, that the list is not indexed. If, for instance, you needed to find the person with an ID of 100, you'd need to loop through all of the Person objects in the list until you found one with that value in it's Id property. If you want to index them by ID so that you can find them more easily/quickly, you can use a Dictionary, like this:

Dim persons As New Dictionary(Of String, Person)()
For Each line As String In File.ReadAllLines("People.csv")
    Dim fields() As String = ParseCsvLine(line)        
    Dim person As New Person()
    person.Id = fields(0)
    person.Name = fields(1)
    person.Title = fields(2)
    persons(person.Id) = person
Next

Then, when you need to get a person from the dictionary, you can easily access it by ID, like this:

Dim person100 As Person = persons("100")

这篇关于使用变量值从自定义类名创建新对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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