Making new object from custom class- name using a variable value?

∥☆過路亽.° 提交于 2019-12-25 03:15:21

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/22697576/making-new-object-from-custom-class-name-using-a-variable-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!