Uriel Katz
Uriel Katz

Reputation: 329

How to create your own type and use it to store data

I've recently ran into the problem that dictionary only allows 1 value per key. Reading around I have seen multiple answers suggesting creating a type through a classes. Now granted I don't know much about classes, I have always though that classes were just a collection of functions and subs. How come they can create data types and how do you use them to do so?

Upvotes: 2

Views: 16800

Answers (3)

Uriel Katz
Uriel Katz

Reputation: 329

After combining knowledge from this and other sources, here is my final solution:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dictionary = New Dictionary(Of String, Pair)
        Dim p As New Pair("A", "B")

        MsgBox(p.First)
        MsgBox(p.Second)
    End Sub
End Class

Public Class Pair
    Private ReadOnly value1 As String
    Private ReadOnly value2 As String

    Sub New(first As String, second As String)
        value1 = first
        value2 = second
    End Sub

    Public ReadOnly Property First() As String
        Get
            Return value1
        End Get
    End Property

    Public ReadOnly Property Second() As String
        Get
            Return value2
        End Get
    End Property
End Class

Upvotes: 0

the_lotus
the_lotus

Reputation: 12748

Classes aren't just about functions and subs, they also contains variable and properties. Which can be used to store a bunch of values.

Lets say you want to store the first name and last name of a person in the dictionnary by person number.

Public Class Person
    Public Property Number As String
    Public Property FirstName As String
    Public Property LastName As String
End Class

Dim dict = New Dictionary(Of String, Person)
Dim p = New Person

p.Number = "A123"
p.FirstName = "John"
p.LastName = "Doe"

dict.Add(p.Number, p)

And then to fetch the person back

p = dict("A123")

Console.WriteLine(p.FirstName)
Console.WriteLine(p.LastName)

Upvotes: 1

user2480047
user2480047

Reputation:

The basic definition of a Dictionary is given by Dictionary(Of type1, type2), where types can be anything, that is, primitive types (String, Double, etc.) or ones you create (via Class, for example). Also you can account for them as "individual variables" or inside collections (Lists, Arrays, etc.). Some examples:

 Dim dict = New Dictionary(Of String, List(Of String))

 Dim tempList = New List(Of String)
 tempList.Add("val11")
 tempList.Add("val12")
 tempList.Add("val13")

 dict.Add("1", tempList)

 Dim dict2 = New Dictionary(Of String, type2)
 Dim tempProp = New type2
 With tempProp
     .prop1 = "11"
     .prop2 = "12"
     .prop2 = "13"
 End With
 dict2.Add("1", tempProp)

 Dim dict3 = New Dictionary(Of String, List(Of type2))
 Dim tempPropList = New List(Of type2)
 Dim tempProp2 = New type2
 With tempProp2
     .prop1 = "11"
     .prop2 = "12"
     .prop2 = "13"
 End With
 tempPropList.Add(tempProp2)

 dict3.Add("1", tempPropList)

Where type2 is defined by the following Class:

Public Class type2
    Public prop1 As String
    Public prop2 As String
    Public prop3 As String
End Class

NOTE: you can change the types in the examples above as much as you wish; also put anything (List, custom types, etc.) in both Values and Keys.

NOTE2: the primitive types in VB.NET (for example: Double) are basically a bunch of variables (declared globally inside the given framework) and functions: Double.IsInfinity (function), Double.MaxValue (variable), etc.; thus a type can be understood as an in-built Class, that is, a general name for a group of functions and variables, which can be used to define another variable in a different Class. I think that the proposed example is pretty descriptive.

Upvotes: 6

Related Questions