Creating a table in a module that contains a structure - vb.net

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

Related

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

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

vb.net - How to concatenate a variable(trimmed) to another variable to complete its variable name

Needed code is something like this:
Dim myArray(0) As String
Dim ay As String = "ay"
myArr & ay(0) = "asd"
I've tried but did not worked
Dim classlist1(0) As String
Dim classlist2(0) As String
Dim classlist3(0) As String
Dim classlist4(0) As String
Dim count As Integer = 0
For _year As Integer = 1 To 4
("classlist" & _year)(count) = "hi"
count += 1
Next
Any time you see something like this:
Dim classlist1(0) As String
Dim classlist2(0) As String
Dim classlist3(0) As String
' etc.
It's an indication that you're using the wrong data structure. Instead of trying to dynamically build variable names (which isn't really possible in a static language, at least not without some really ugly reflection code with a high potential for runtime errors), just use a collection.
For example, if you want a collection of strings:
Dim classList As New List(Of String)()
And if you want a collection of collections of strings:
Dim classLists As New List(Of List(Of String))()
Then you can reference the nested lists within the parent list. So to add your first "year" of classes:
classLists.Add(new List(Of String))
And add a class to that year:
classLists(0).Add("some value")
As you can see, it starts to get a little difficult to keep track of the data structures. This is where creating custom types and structures becomes very useful. For example, rather than representing a "year" as a list of strings, create an actual Year class. That class can internally hold a list of strings, and other logic/data.
Try Dictionary<TKey, TValue> Class From MSDN.
Dim classLists As New Dictionary(Of String, String)()
'Add items with keys
For _year As Integer = 1 To 4
classLists.Add(String.Format("classlist{0}",_year), "hi")
Next
And you can get value by key later
Dim key As String = "classlist2"
Dim value As String = classLists(key)

Can I add an object to a struct in VB.net?

I have a data struct for a piece of lumber. I've built a class to handle dimensions (architectural, metric, etc... ) that I'd like to make the data type of the length member of this struct. VB says I can't use 'new' in my definition unless I make the member 'Shared'. If I make the member 'Shared' I can't see the data when I try to access the member in my code.
Public Structure PieceInfo
Dim ProjectNumber As String
Dim ProjectName As String
Dim BuildingType As String
Dim BuildingNumber As String
Dim BLevel As String
Dim Batch As String
Dim Trussname As String
Dim Span As Single
Dim PieceName As String
Dim LumberType As String
Shared PieceLength As New clsDimension
Shared StockLength As New clsDimension
Dim LeftSplicePlate As String
Dim RightSplicePlate As String
End Structure
How can I use my "clsDimension" object as the data type for the "Length" members of my struct?
As all the comments indicate: You should change your Struct to a class because you want to reference it. And due to Structs being value types and Classes being reference types, This is what you want:
Public Class PieceInfo
Dim ProjectNumber As String
Dim ProjectName As String
Dim BuildingType As String
Dim BuildingNumber As String
Dim BLevel As String
Dim Batch As String
Dim Trussname As String
Dim Span As Single
Dim PieceName As String
Dim LumberType As String
Shared PieceLength As New clsDimension
Shared StockLength As New clsDimension
Dim LeftSplicePlate As String
Dim RightSplicePlate As String
End Class
.NET Structure don't have default constructor, you'll have to create your own (or a function to initialize the values). But that kind of defeat the purpose of the struct.
Public Structure PieceInfo
Dim ProjectNumber As String
Dim ProjectName As String
Dim BuildingType As String
Dim BuildingNumber As String
Dim BLevel As String
Dim Batch As String
Dim Trussname As String
Dim Span As Single
Dim PieceName As String
Dim LumberType As String
Dim PieceLength As clsDimension
Dim StockLength As clsDimension
Dim LeftSplicePlate As String
Dim RightSplicePlate As String
Public Sub New(ByVal t As String)
PieceLength = New clsDimension
StockLength = New clsDimension
End Sub
End Structure
But like others said, changing it to a class is the right thing to do. Class is reference type and structure is value type.

How to enumerat or index the properties of a structure

For some time I have been wondering if it is possible to enumerate, or set and index for the properties of an object or structure.
I currently have a set of custom graph generator classes for different reports, but they all accept the same structure as a parameter.
The structures' properties' values get set from a SQL reader that reads columns as they are set up in a table in the database. Now ideally want to loop though these column values sequentially and write them to the properties of the structure.
My Structure is as follows:
Public Structure MyStructure
Dim GraphName As String
Dim GraphValue As Integer
Dim Red As Integer
Dim Green As Integer
Dim Blue As Integer
End Structure
Now I want to be able to loop through these properties and assign values to each. E.g.:
Dim Struct as MyStructure
For i as integer = 0 to 4
Struct.i = "A value retrieved from database"
Next i
The main idea is that i want to avoid using a case statement:
Dim Struct as MyStruct
For i as integer = 0 to 4
Select Case i
Case 0
Struct.GraphName = "A value retrieved from database"
Case 1
Struct.GraphValue = "A value retrieved from database"
'Etc.
End Select
Next i
Any insight on this will be much appreciated.
To "dynamically" access the fields, you would use reflection:
Private Structure foo
Public i As Integer
Public s As String
End Structure
Private Sub bar()
Dim f As foo
Dim fields = GetType(foo).GetFields(BindingFlags.Public Or BindingFlags.Instance)
For Each fi As FieldInfo In fields
fi.SetValue(f, GetValueFromDBForName(fi.Name))
Next
End Sub
Be aware that Reflection is NOT fast. "FasterFlect" (via NuGet) or other "fast" replacements might be an alternative. OR you consider to use an ORM (object relational mapping) "out-of-the-box"

Nested List of Custom Object. VB.NET

I'm new to VB.net and requires your help.
I've two vb.net Structures, Quotation and FareAsPerVehicleType, Quotation is dependent on FareAsPerVehicleType. I am trying to add VehicleType in Quotation by using the following:
Dim Quot As New Quotation
Dim vT As FareAsPerVehicleType
vT.TypeOfVehicle = "S"
vT.Fare = _raw_Price * vF.Saloon_Factor
Quot.VehicleType.Add(vT)
Public Structure FareAsPerVehicleType
Dim TypeOfVehicle As String
Dim Fare As Decimal
End Structure
Public Structure Quotation
Dim VehicleType As List(Of FareAsPerVehicleType)
Dim Mileage As Decimal
Dim TimeToTravel As Decimal
Dim Pickup As String
Dim Dropoff As String
End Structure
In doing so I am getting the following error.
<"System.NullReferenceException was unhandled">
<" Message=Object reference not set to an instance of an object.">
<" Source=WindowsApplication1">
Please help
Regards
You need to instantiate the collection before you can use it. As is, when you are declaring a new Quotation object, the VehicleType list is set to nothing. Change the declaration line to
Dim VehicleType As New List(Of FareAsPerVehicleType)
Or even better, change to declaration to remove the Dim and replace with Public to show the accessibility of the field.
Public VehicleType As New List(Of FareAsPerVehicleType)
To really make the code shine, you could replace the field with an auto property:
Public Property VehicleType() As New List(Of FareAsPerVehicleType)
Any of these will work to get rid of your error.