新手问题:如何创建一个类来在Visual Basic Studio中保存数据? [英] Newbie question: how do I create a class to hold data in Visual Basic Studio?

查看:189
本文介绍了新手问题:如何创建一个类来在Visual Basic Studio中保存数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很抱歉。这似乎是一个令人难以置信的愚蠢的问题,但除非我问我永远不会想出来。我试图写一个程序,在Visual Basic中的csv文件中读取(试图放弃在C#),我问一个朋友,我的编程比我好多了。他说我应该创建一个类来保存我读入的数据。

I'm really sorry. This must seem like an incredibly stupid question, but unless I ask I'll never figure it out. I'm trying to write a program to read in a csv file in Visual Basic (tried and gave up on C#) and I asked a friend of mine who is much better at programming than I am. He said I should create a class to hold the data that I read in.

问题是,我从来没有创建过类,或任何东西。我知道与类相关的所有术语,我从高层次了解类如何工作没有问题。

The problem is, I've never created a class before, not in VB, Java, or anything. I know all the terms associated with classes, I understand at a high level how classes work no problem. But I suck at the actual details of making one.

这是我做的:

Public Class TsvData

    Property fullDataSet() As Array

        Get
            Return ?????
        End Get

        Set(ByVal value As Array)

        End Set

    End Property

End Class

我得到了问号,我被卡住了。
类将保存大量数据,所以我做了一个数组。这可能是一个坏的举动。我不知道。所有我知道,它不能是一个字符串,它肯定不能是一个整数或浮点。

I got as far as the question marks and I'm stuck. The class is going to hold a lot of data, so I made it an array. That could be a bad move. I don't know. All i know is that it can't be a String and it certainly can't be an Integer or a Float.

对于Getter和Setter,原因我放置问号是因为我想返回整个数组。该类最终会有其他属性,这些属性基本上是相同数据的排列,但是这是当我想保存它或某事时我将使用的完整集合。现在我想返回整个数组,但是输入Return fullDataSet()似乎不是一个好主意。我的意思是,属性的​​名称是fullDataSet()。它只会产生某种循环。但是没有其他数据要返回。

As for the Getter and Setter, the reason I put the question marks in is because I want to return the whole array there. The class will eventually have other properties which are basically permutations of the same data, but this is the full set that I will use when I want to save it out or something. Now I want to return the whole Array, but typing "Return fullDataSet()" doesn't seem like a good idea. I mean, the name of the property is "fullDataSet()." It will just make some kind of loop. But there is no other data to return.

我应该在属性内部调整另一个数组,该数组已经是一个数组,

Should I Dim yet another array inside the property, which already is an array, and return that instead?

推荐答案

通常使用私有成员存储实际数据:

Usually you use a private member to store the actual data:

Public Class TsvData
Private _fullDataSet As String()
Public Property FullDataSet() As String()
    Get
        Return _fullDataSet
    End Get
    Set(ByVal value As String())
        _fullDataSet = value
    End Set
End Property

注意,这是一个糟糕的设计实例,因为它将一个概念耦合到一个具体的表示,并允许类的客户端修改内部,没有任何错误检查。返回 ReadOnlyCollection 或一些专用容器会更好。

Note that this is an instance of bad design since it couples a concept to a concrete representation and allows the clients of the class to modify the internals without any error checking. Returning a ReadOnlyCollection or some dedicated container would be better.

这篇关于新手问题:如何创建一个类来在Visual Basic Studio中保存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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