Click here to Skip to main content
16,005,473 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am new to VB.Net and with plethora of choses for data type variables it’s a bit overwhelming as to what type of variable to use in my instance. The data I am trying to store is:
VB
"1/16" & Chr(34) & " = 1" & Chr(39)          192
"3/32" & Chr(34) & " = 1" & Chr(39)          128
"1/8" & Chr(34) & " = 1" & Chr(39)           96
"3/16" & Chr(34) & " = 1" & Chr(39)          64
"1/4" & Chr(34) & " = 1" & Chr(39)           48
      --/--                                 --/--



In the past (VBA) I would store the data in a 2d array. But with VB.Net I was hoping for an easyer way to acomplish the same thing. I have tried Enum but was not sucsessful. As I started to look into other options like List, arraylist, collection . . . it became overwhelming. So I was hoping to get a better idea on my choses.

The data will consist of about hundred rows and I will have to manualy input it or pull it from an extenal sourse. The speed and ease for deriving/seaching data in the ‘table’ is also important.

Thank you for you help and advice in advance,

-Jay
Posted
Updated 23-May-12 4:14am
v3
Comments
Sergey Alexandrovich Kryukov 23-May-12 10:13am    
Not clear. Do you have a problem with multi-dimensional arrays in .NET? What your code is supposed to mean?
--SA
juno101 23-May-12 23:34pm    
Hi SAKryukov, no I don't really have a problem with multi arrays. It's just I was trying to explore other more efficient options. The Dictionary Idea by Jim Lahey was very useful, particularly the ability to pull data via key. The only why i know on how to pull data from arrays is by iterating it . . . unless you know something i don't, in that case i am all ears/eyes
Sergey Alexandrovich Kryukov 28-May-12 18:57pm    
Of course. You see, your problem is the very usual one: trying to work with strings representing data while you need to work with the data itself. Do you see what I mean?
--SA
juno101 28-May-12 22:14pm    
sorry, not really. can you please show me an example code.
Sergey Alexandrovich Kryukov 29-Jun-12 22:36pm    
You need to use serialization, with binary formatter or XML based. Persistence separate from data, loaded and forgot. Data should be agnostic to persistence. Needs to long time to create a minimal sample. Read on: "Data Contracts".
--SA

If speed and retrieval are your primary concerns, I'd look at using a generic Dictionary:

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^]

Using a Dictionary will require you to have unique values as your key, from what I can tell from your example, you should be OK.
 
Share this answer
 
v2
Comments
Wendelius 23-May-12 14:44pm    
Good answer, my 5
juno101 23-May-12 23:35pm    
Thank you Jim, that was just what the doctor ordered . . .
In object-oriented programming we use classes.

Let say your class could looks like:
VB
Public Class TDataStoreClass
    Implements IDataStoreClass

    Private f1 As String = String.Empty
    Private f2 As Integer = 0

    Public Sub New()
        'default values
    End Sub

    Public Sub New(ByVal _f1 As String, ByVal _f2 As Integer)
        f1 = _f1
        f2 = _f2
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub

    Public Property Field1() As String Implements IDataStoreClass.Field1
        Get
            Return f1
        End Get
        Set(ByVal value As String)
            f1 = value
        End Set
    End Property

    Public Property Field2() As Integer Implements IDataStoreClass.Field2
        Get
            Return f2
        End Get
        Set(ByVal value As Integer)
            f2 = value
        End Set
    End Property
End Class


As you see, the above class has 2 fields: Field1 (stores string value) and Field2 (stores integer value) and implements properties from IDataStoreClass interface:

VB
Public Interface IDataStoreClass

    Property Field1() As String
    Property Field2() As Integer

End Interface


To use this class we need to declare it in a module:
VB
Module ModDataStore

    Public dsc As IDataStoreClass = Nothing

End Module


Next, you should initialize your class (see New procedure) in the form class, on Button_Click event you can set values using class properties:
VB
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dsc.Field1 = Me.TextBox1.Text
        dsc.Field2 = Integer.Parse(Me.TextBox2.Text)
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        dsc = New TDataStoreClass()
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class


The above example is very simple.

Read more about: List(of T)
[^] to use it with array of your data.
 
Share this answer
 
Comments
Wendelius 23-May-12 14:43pm    
Nice!
Maciej Los 23-May-12 15:41pm    
Thank you, Mika ;)
juno101 23-May-12 23:39pm    
thank you very much for you answer Losmac

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900