How would you structure data with different datatypes in VB.NET

Right now I have many locations with this structure. At the moment I have: name as string and x,y,z positions as single. So it's a mix of data types and I might want to add both more data in the future and also other data types. I must be able to easily extract any part of this data.

Example of how I'll work with this data is: When I choose South Wales from a combobox then I want to get its properties, x,y,z populated in a textbox. So they need to be "linked". If I choose London then it'll have its x,y,z properties etc.

My initial idea is just to dim every single data such as in the first example below. This should be the easiest way with 100% control of what's what, and I could easily extract any single data but at the same time might get tedious I assume, or am I wrong? Is it a good way to approach this?

    Dim SW_FP As String = "South Wales"
    Dim SW_FP_X As Single = "489,1154"
    Dim SW_FP_Y As Single = "-8836,795"
    Dim SW_FP_Z As Single = "109,6124"

The next example below is something i just googled up. Is this a good method?

    Dim dt As DataTable = New DataTable
    dt.Columns.Add("South Wales", GetType(String))
    dt.Columns.Add("489,1154", GetType(Single))
    dt.Columns.Add("-8836,795", GetType(Single))
    dt.Columns.Add("109,6124", GetType(Single))

OR should I use something else? Arrays, Objects with properties... and this is where my ideas end. Are there other methods? XML?

I want to do it in a smart way from start instead of risking to rewrite/recreate everything in the future. So my main question is: Which method would you suggest to be the smartest to choose? and also if you could provide a super tiny code example.


You mentioned that when you choose an item you want to get it's properties. This shows that you are looking for objects. If not using a database one example could be to make Location objects and have a List of these to be added or removed from. Then you have a lot of different ways to get the data back from the List. For example:

Class:

Public Class Location
    Public Property Name As String
    Public Property X As Single
    Public Property Y As Single
    Public Property Z As Single
End Class

List:

Dim locations As New List(Of Location)
Dim location As New Location With {
    .Name = "South Wales",
    .X = 1.1,
    .Y = 1.2,
    .Z = 1.3
}
locations.Add(location)

LINQ to get result:

Dim result = locations.SingleOrDefault(Function(i) i.Name = "South Wales")

This is just an example for use within your program, hope it helps.

Visual Basic data types, 4 bytes on 32-bit platform 8 bytes on 64-bit platform Data types refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.


Disclaimer: Untested code. It's more to guide you than copy-paste into your project.

First, create a Class that will represent the structured data:

Public Class Location

    Public Property Name As String
    Public Property PositionX As Single
    Public Property PositionY As Single
    Public Property PositionZ As Single

    Public Sub New()
        Me.New (String.Empty, 0, 0, 0)
    End Sub

    Public Sub New(name As String, x As Single, y As Single, z As Single)
        Me.Name = name
        Me.PositionX = x
        Me.PositionY = y
        Me.PositionZ = z
    End Sub

Now, you can create a List(Of Location) and use that List to bind to a ComboBox, like this:

Dim list As New List(Of Location) = someOtherClass.ReadLocations ' Returns a List(Of Location) from your database, or file, or whatever.

cboLocations.DataSource = list
cboLocations.DisplayMember = "Name" ' The name of the Location class' Property to display.
cboLocations.ValueMember = "Name" ' Use the same Name Property since you have no ID.

You can also forego the list variable declaration like the following, but I wanted to show the declaration of list above:

cboLocations.DataSource = someOtherClass.ReadLocations

Function someOtherClass.ReadLocations() may populate the List(Of Locations) in a way similar to this. Note I'm not including data access code; this is just an example to show how to add Location objects to the List(Of Location):

Dim list As List(Of Location)

' Some loop construct
For each foo in Bar

    Dim item As New Location(foo.Name, foo.X, foo.Y, foo.Z)
    list.Add(item)

' End loop

Return list

The "magic" happens when you select an option from the ComboBox. I forget the ComboBox event offhand, so that's homework for you :-) You take the selected Object of the ComboBox and cast it back to the native type, in this case Location:

Dim item As Location = DirectCast(cboLocations.SelectedItem, Location)

txtName.Text = item.Name
txtPosX.Text = item.PositionX.ToString
txtPosY.Text = item.PositionY.ToString
txtPosZ.Text = item.PositionZ.ToString

Data Types, What do you mean by data types and variables used in Visual Basic? A structure can implement one or more interfaces, and you can declare individual access levels for each field. You can combine data items of different types to create a structure. A structure associates one or more elements with each other and with the structure itself.


Here is one way, using a DataTable as you mentioned. This is a stand alone example project just to show code used. This example loads data from file is found and saves data on exit. Form1 Image

' Stand alone example
' needs DataGridView1, Label1 and
' ComboBox1 on the Designer
' copy/replace this code with default

Option Strict On
Option Explicit On

Public Class Form1
    Dim dt As New DataTable("Freddy")
    Dim bs As New BindingSource

    'edit path/filename to use as test data path
    Dim filepath As String = "C:\Users\lesha\Desktop\TestData.xml"
    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        dt.WriteXml(filepath)
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With dt
            dt.Columns.Add("Country", GetType(String))
            dt.Columns.Add("x", GetType(String))
            dt.Columns.Add("y", GetType(String))
            dt.Columns.Add("z", GetType(String))

            ' add extra column to hold concatenated
            ' location  (could be a hidden column)
            dt.Columns.Add("CombLoc", GetType(String), "'x = ' + x  + '  y = '  +  y + '  z = ' + z")

            If IO.File.Exists(filepath) Then
                ' saved file found so load it
                dt.ReadXml(filepath)
            Else
                ' no saved file so make one test row
                .Rows.Add("South Wales", 489.1154, -8836.795, 109.6124)
            End If
        End With
        bs.DataSource = dt
        DataGridView1.DataSource = bs

        ' set any properties for DataGridView1
        With DataGridView1
            ' to hide Combined Location column
            .Columns("CombLoc").Visible = False
            ' dontwant row headers
            .RowHeadersVisible = False
        End With

        set up ComboBox
        With ComboBox1
            .DataSource = bs
            ' displayed item
            .DisplayMember = "Country"
            ' returned item 
            .ValueMember = "CombLoc"
            If .Items.Count > 0 Then .SelectedIndex = 0
        End With

        ' default Label text
        Label1.Text = "No items found"
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        no items in list so exit sub
        If ComboBox1.SelectedIndex < 0 Then Exit Sub
        send returneditem to Label
        Label1.Text = ComboBox1.SelectedValue.ToString
    End Sub
End Class

Structures, can implement one or more interfaces, and you can declare individual access levels for each field. In this article I will explain about one of the main topics in VB.NET i.e. data types. You will also learn about the value and reference types.


Composite Data Types, Instead, each definition of a structure represents a unique data type, even if two structures define identical elements in the same order. However,  In the previous lesson, Solved tasks for Visual Basic .NET lessons 1-3, we learned basic data types of Visual Basic .NET (Integer, Double and String).In today's tutorial, we're going to look at them in more detail and explain how to use them correctly.


How to: Declare a Structure, What are Data Types? Data types determine the type of data that any variable can store. Variables belonging to different data types are  Data types apply to all values that can be stored in computer memory or participate in the evaluation of an expression. Every variable, literal, constant, enumeration, property, procedure parameter, procedure argument, and procedure return value has a data type.


VB.Net Data Types and Variable Declaration with DIM, In VB.NET I would like to create a complicated data structure with multiple types of data stored in an array like format (see below). I am trying to create a data  To start, this example has a Structure called Simple. This Structure has 3 fields: an Integer, a Boolean and a Double. These fields are stored directly as part of the Structure. Main: Here we create an instance of Simple. We do not need to use a New Sub (a constructor). So: A Structure, of any type, is used in the same way as an Integer. And an