How does one dynamically declare variables during program execution? - variables

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.

Related

How to find the index of an object in a list of objects? VB.Net

I am creating a method FindPerson which searches for a given name in a list of objects and returns the index in the list of the object with this name if found, otherwise it returns -1.
Public Class TPerson
Private Name As String
Private Address As String
Private Age As Integer
Public Sub New()
Name = "x"
Address = "x"
Age = 0
End Sub
……
End Class
Public Class TGroup
Private Group As List(Of TPerson)
Private GroupSize As Integer
Public Sub New(size As Integer)
GroupSize = size
Group = New List(Of TPerson)
End Sub
Public Sub FindPerson(findname As String)
Dim index As Integer
index = Group.FindIndex(findname) 'error
End Sub
End Class
The output should be an index in the list, however when I run the program I get the error: BC30311 Value of type 'String' cannot be converted to 'Predicate(Of TPerson)'
I am not quite sure how to fix this any help will be appreciated
How exactly do you expect the FindIndex method to know what to do with that String that you are passing in? You seem to assume that it will know that it represents a name and that it needs to match an item by Name property but how do you think it's going to do that? Why do you think it would match on Name rather than Address?
As the error message says, you need to provide a Predicate which is a delegate that takes an object of type T and returns a Boolean. In your case, T is TPerson and the Boolean needs to indicate whether findname matches its Name property. The simplest way to do that is with a Lambda expression:
Dim index = Group.FindIndex(Function(person) person.Name = findname)
You could do it with a named method and a delegate if you wanted to but it would be more long-winded and it would mean getting the findname value in by some convoluted means. If you read the documentation for the FindIndex method (which you should have done before posting here) you can find an example of that sort of thing.

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

Changing dictionary entrys with a structure as value in Visual Basic 2012

I have this structure:
Public Structure SourceDB
Public SourceDBid As Integer
Public SourceDBWimLocationName As String
Public SourceDBDBName As String
Public SourceDBIPAdress As String
Public SourceDBPort As String
Public SourceDBUsername As String
Public SourceDBPassword As String
Public ConnectivityState As String
Public LastTransmittedId As Integer
End Structure
I am declaring the dictionary in the following way:
Private MySourceDBDictionary As New Dictionary(Of Integer, SourceDB)
Now values get added to the dictionary...
At some point I want to change values of the dictionary in an for each loop:
For Each element As KeyValuePair(Of Integer, SourceDB) In MySourceDBDictionary
MySourceDBDictionary.Item(element.Key).LastTransmittedId = 0
Next
For some reason this does not work...
The error code is: Expression is a value and therefore cannot be the target of an assignment.
How its it possible, to change already existing values in a dictionary???
Kind Regards! and Thanks.
What you want to do cannot be done with a value type but you can do what you want by reading the Structure out and passing it back.
For Each element As KeyValuePair(Of Integer, SourceDB) In MySourceDBDictionary
Dim src = MySourceDBDictionary(element.Key)
src.LastTransmittedId = 0
MySourceDBDictionary(element.Key) = src
Next
This replaces the complete value type with a new 'value'
Alternatively you can use the code you have if you use a Reference Type instead of a Value Type i.e. A Class instead of a Structure. I would suggest that this should be a Class instead of a Structure as you are modifying the values after creation
The problem is in the structure take a look at this question Expression Is a value and therefore cannot be the target of an assignment
The above question should be a good explanation to when you should use Structure
Change your struct to a Class and your code should work,
also you can change your foreach loop a little to somthing like that:
For Each element As KeyValuePair(Of Integer, SourceDB) In MySourceDBDictionary
element.Value.LastTransmittedId = 0
Next

How do you assign values to structure elements in a List in VB.NET?

I have a user-defined structure in a list that I am trying to change the value for in an individual element within the list of structures. Accessing the element is not a problem. However, when I try to update the value, the compiler complains:
"Expression is a value and therefore cannot be the target of the
assignment"
For example:
Public Structure Person
Dim first as String
Dim last as String
Dim age as Integer
End Structure
_
Public Sub ListTest()
Dim newPerson as Person
Dim records as List (Of Person)
records = new List (Of Person)
person.first = "Yogi"
person.last = "bear"
person.age = 35
records.Add(person)
records(0).first = "Papa" ' <<== Causes the error
End Sub
As the other comments said, when you refer to records(0), you get a copy of the struct since it is a value type. What you can do (if you can't change it to a Class) is something like this:
Dim p As Person = records(0)
p.first = "Papa"
records(0) = p
Although, I think it's just easier to use a Class.
There are actually two important concepts to remember here.
One is that, as Hans and Chris have pointed out, Structure Person declares a value type of which copies are passed between method calls.
You can still access (i.e., get and set) the members of a value type, though. After all, this works:
Dim people(0) As Person
people(0).first = "Yogi"
people(0).last = "Bear"
people(0).age = 35
So the other important point to realize is that records(0) accesses the List(Of Person) class's special Item property, which is a sugary wrapper around two method calls (a getter and setter). It is not a direct array access; if it were (i.e., if records were an array), your original code would actually have worked.
I had the same problem, and I fixed it by adding a simple Sub to the structure that changes the value of the property.
Public Structure Person
Dim first as String
Dim last as String
Dim age as Integer
Public Sub ChangeFirst(value as String)
me.first = value
End Sub
End Structure

Populate an Array of Object Based on DataReader Data

I am not sure how to phrase my question properly but I want to achieve something like this.
I have a class named Products
public class Products
private ID as Integer
private Name as String
Public Property ProductID()
Get
Return ID
End Get
Set(ByVal value)
ID = value
End Set
End Property
In one of my code behind pages, I am retrieving data from an SQL Command and placing the same into a datareader object.
How would I be able to declare the class so that each row in my datareader would actually be an instance of the said class?
Like for example:
Dim myProduct() as New Product
Dim intCnt as Integer
While datareaderData.read()
intCnt += 1
myProduct(intCnt) = new Product
myProduct(intCnt).ID = datareaderData("ID")
myProduct(intCnt).Name = datareaderData("Name")
End While
When I do the same, I am getting an error "Object Reference Not Set to an Instance of an Object.
I am quite stumped on this one. Any tips greatly appreciated. Thanks.
You should use an Arraylist or -better- a generic List(of Product).
Besides i would strongly recommend to set Option Strict On in your project's Compiler Settings.
Dim products As New List(Of Product)
While datareaderData.read()
Dim nextProduct As New Product
nextProduct.ProductID = CType(datareaderData("ID"), System.Int32)
nextProduct.Name = datareaderData("Name").ToString
products.add(nextProduct)
End While