VB.NET How would I store multiple inputs in a single preset structure - vb.net

So I've been making a 'database' that would store different students ID's, names and grades.
And the problem I'm having is that the my structure has no 'depth' and I can only store one set of data inside it. How would I increase the capacity of my structure so I'm able to store more than 1 set of data? My code is this:
Structure record
Dim ID As Integer
Dim fname As String
Dim sName As String
Dim grade As String
End Structure

Convert your struct to a class:
Class clsRecord
public ID As Integer
public fname As String
public sName As String
public grade As String
end class
Create a List to hold a collection of Record Classes
Dim lstRecords as List(Of clsRecord)
Create new instance of class clsRecord
Dim someRecord as new clsRecord
Populate members
eg
someRecord.ID = 1
Add class to collection
lstRecord.Add(someRecord)
Repeat
Then loop through collection for each record
For Each xRecords in lstRecords
'do something
Next

Related

Creating a table in a module that contains a structure

How do I create an array of ten that contains a public structure? And a module that contains that array? I have this so far but the structure wont let my array have a number between the parentheses.
Module Module 1
Public Structure Élève
Dim Nom As String
Dim Prénom As String
Dim Note As Integer
Dim Rendement As Integer
Dim TabÉlève() As Élève
End Structure
End Module

VB.net class and function instance - most efficient method

I would like to know what you guys would do in this situation.
I am basically returning a data set for Person, but I would like to know the most efficient way of doing things.
Public Class TestClass
Public Shared Function returnPersonData() As Person
Dim p As New Person
p.Address = "Here and there"
p.Name = "Mike"
p.Career = "Pilot"
Return p
End Function
End Class
Person class:
Public Class Person
Public Property Name As String
Public Property Address As String
Public Property Career As String
End Class
I would then get the name by doing this in another class:
Dim name As String = TestClass.returnPersonData.Name
Dim address As String = TestClass.returnPersonData.Address
My question is this: why does it re-run the returnPersonData function every time I need to extract info the name, address and career? Why can't I just call the function once, save it in a data set, and then just reference that?
Because you are calling it twice...
Dim name As String = TestClass.returnPersonData.Name ' <--- One time here
Dim address As String = TestClass.returnPersonData.Address ' <--- An other time here
Save the person class instance
Dim currentPerson As Person = TestClass.returnPersonData
Then you can get the name or address with
Dim name As String = currentPerson.Name
Dim address As String = currentPerson.Address
You could remove those two variables and just use currentPerson all the time.

List (Of T) as DataGridView.DataSource makes sorting fail

I have read some threads about this "error" but I can't figure out how to solve my problem.
I have a class that looks something like this:
Public Class Person
Public Property Name As String
Public Property PhoneNumber As string
Public Property Age As Integer
Public sub New(ByVal Values As String())
Me.Name = Values(0)
Me.PhoneNumber = Values(1)
Me.Age = Convert.ToInt32(Values(2))
End Sub
End Class
I get my data from a semicolon separated file, and i create a list of Person objects by looping this file and split on semicolon. Like this
Dim PersonsList As New List(Of Person)
For Each line in textfile..........
PersonsList.Add(New Person(line.Split(";")))
Next
When the list is complete, I tell my DataGridView that DataSource is PersonsList.
This works like a charm, but I'm not able to sort the columns.
I found this post amongst many (where the class values are not properties, which mine are) and tried that converting function which did'nt really work in my case. The right amount of rows were created, but all of the columns were blank.
What am I missing?
If you use a datatable as the data source, column sorting is automatically enabled and you can sort the data by any column:
Dim dt As New DataTable
dt.Columns.AddRange(
{
New DataColumn("Name"),
New DataColumn("Phone"),
New DataColumn("Age")
})
For Each s As String In IO.File.ReadAllLines("textfile1.txt")
Dim temprow As DataRow = dt.NewRow
temprow.ItemArray = s.Split(";"c)
dt.Rows.Add(temprow)
Next
DataGridView1.DataSource = dt

Making new object from custom class- name using a variable value?

I am making a LIST to organize and manipulate arrays that represent lines off a spreadsheet. I've created a custom class for the arrays, and will call them out as objects.
My question is, can I use a value stored in a variable as the name of the object? If so, what would the syntax look like?
dim FileName as String
FileName = 123456.csv
Public Class List_Array
public variable1 as string
public variable2 as string
public variable3 as string
public variable4 as string
End Class
dim File_Name as List_Array = NEW List_Array
This is the coding as I understand it, but I keep thinking this will only create one Object over and over again with the same name as the string variable.
If not, how can I differentiate the different objects as I call them? There will be thousands of objects to reference, so using an unnamed object will not work so well.
If you want to store a list of named objects, what you need is a Dictionary. Dictionary objects store a list of key/value pairs. In this case, the "key" is the name that you want to assign to the object, and the value is the reference to the object itself. It's a generic class, which means when you use the Dictionary type, you must specify the types that you want it to use for it's keys and values. For instance Dictionary(Of String, MyClass) will create a list that uses String objects for its keys and MyClass objects for its values.
Here's an example of how you could use a dictionary to store a list of people with their ages:
Dim d As New Dictionary(Of String, Integer)()
d("Bob") = 30
d("Mary") = 42
Then, when you want to read a value, you can do it like this:
Dim age As Integer = d("Bob")
The Dictionary will only allow one item per key. It uses a hash table to index the values by their keys, so it's very fast get any item by its key.
Edit
Based on your comments below, here's a more pertinent example to show what I mean. Let's say you have a CSV file containing a list of people. So you create a class that stores all of the information about one person (one line of the CSV file), like this:
Public Class Person
Public Property Id As String
Public Property Name As String
Public Property Title As String
End Class
Then, you create a method which parses a single line from the CSV file and returns all of the fields from that line as an array of strings, like this:
Public Function ParseCsvLine(line As String) As String()
' ...
End Function
Then, you could load all of the people into a list, like this:
Dim persons As New List(Of Person)()
For Each line As String In File.ReadAllLines("People.csv")
Dim fields() As String = ParseCsvLine(line)
Dim person As New Person()
person.Id = fields(0)
person.Name = fields(1)
person.Title = fields(2)
persons.Add(person)
Next
Now you have all of the Person objects loaded into one list. The problem is, though, that the list is not indexed. If, for instance, you needed to find the person with an ID of 100, you'd need to loop through all of the Person objects in the list until you found one with that value in it's Id property. If you want to index them by ID so that you can find them more easily/quickly, you can use a Dictionary, like this:
Dim persons As New Dictionary(Of String, Person)()
For Each line As String In File.ReadAllLines("People.csv")
Dim fields() As String = ParseCsvLine(line)
Dim person As New Person()
person.Id = fields(0)
person.Name = fields(1)
person.Title = fields(2)
persons(person.Id) = person
Next
Then, when you need to get a person from the dictionary, you can easily access it by ID, like this:
Dim person100 As Person = persons("100")

How does one dynamically declare variables during program execution?

I am using VS 2012.
I can't figure out how to dynamically declare variables while my code is running. I am trying to write a program that pulls data for EMS dispatches off a website. The website updates every several second, and each call that is posted has a unique ID number. I want to use the unique ID number from each dispatch, and declare a new incident variable using that unique id number and add it to a collection of active dispatches. The only part I cant figure out, is how do I declare a variable for each dispatch as it is posted, and name it with its unique ID number?
something like:
Structure Incident
Dim id As Integer
Dim numer As String
Dim date1 As String
Dim time As String
Dim box As String
Dim type As String
Dim street As String
Dim crosstreet As String
Dim location As String
Dim announced As Boolean
Dim complete As Boolean
End Structure
UniqueIDstringfromwebsite = "1234"
Dim (code to get variable declared with unique ID as variable name) as incident
This is my first post, and I cant quite get the codesample to work quite right in the post.
This is my first answer - so you are in good company!
Do you not need to implement a class instead of a structure - that way you can implement a constructor. I don't think you want to create a variable with the ID as it's name, but you should add the ID to the dispatches collection (this is could be a list(of Incident):
e.g.
Public Class Incident
'define private variables
Private _id as integer
Private _number as string
Private _date1 as String
Private _time as String
'etc...
'define public properties
Public Property Number as string
Get
Return _number
End Get
Set (value as string)
_number = value
End Set
End Property
'Repeat for each Public Property
'Implement Constuctor that takes ID
Public Sub New(id as integer)
_id = id
'code here to get incident properties based on id
End Sub
End Class
Hope this helps!
You can use a Dictionary to identify your var:
Dim myVar As New Dictionary(Of Integer, Incident)
UniqueIDstringfromwebsite = 1234
myVar.Add(UniqueIDstringfromwebsite, New Incident)
I don't think you can change the name of a variable with a value dinamically, and sincerily, and don't get when it can be useful.
And, in this way, better turn your structure into a class.