tags:

views:

42

answers:

3

Hey all,

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.

So here's what I did:

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.

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?

+1  A: 

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

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.

Philipp
Thanks Philipp! Just what I needed to know. I wish I could give everybody a green check though. cHao and stakx's answers were great.
StormShadow
+3  A: 

Ideally, you ought to have a class representing the specific data you want to read in. Setting an entire array at once is asking for trouble; some programs that read {C,T}SV files will freak out if all rows don't have the same number of columns, which is exceedingly easy to do if you can set the data to be an array of arbitrary length.

If you're trying to represent arbitrary data, frankly, you'd do just as well to use a List(Of String). If it's meant to be a table, you could instead read in the first line and make it a list as above (let's call it "headers"), and then make each row a Dictionary(Of String, String). (Let's call each row "row", and the collection (a list of these dictionary objects) "rows".) Just read in the line, split it like you did the first, and say something like row(headers(column number)) = value for each column, and then stuff it into 'rows'.

Or, you could use the data classes (System.Data.DataTable and System.Data.DataSet would do wonders here).

cHao
I hate referring to stuff by column number. It's error prone, and pretty much sets the table's format in stone.
cHao
Thanks cHao! I will look into those data classes. Haven't figured out Lists or Dictionaries yet.
StormShadow
+2  A: 
stakx
Thanks stakx, like I said I'm completely new at this but you've given me an impressive array of options to choose from. I will look into all of them.
StormShadow