Changing dictionary entrys with a structure as value in Visual Basic 2012 - vb.net

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

Related

Reference to an attribute of a class by means of a string containing the name of the property

Let's take a very short example of a class like this:
Public Class The_Class1
Public Property ID As Integer
Public Property Property1_Integer As Integer
Public Property Property2_Single As Single
End Class
Somewhere else, I have a dictionary containing instances of The_Class1, like this:
Public Dictionary_Class1 As New Dictionary(Of Integer, The_Class1)
I want to perform an operation over Property1_Integer on all of the members inside Dictionary_Class1. Also, I want to perform the very same operation over Property2_Single, so I would like to create a function to perform such operation, and somehow instruct VB to use a given property on every call.
Can you think of an elegant way to do that?
Edit: Let's say, for example, that the operation that I want to perform is the sum of every Property1_Integer or Property2_Single of the members inside the dictionary. What I really really want to do is to determine if all of the values are the same, or if there is at least one that is different.
You can use Reflection, but it's not as clean as you may imagine. Here's some skeleton code you can adapt to your needs:
Public Class The_Class1
Public Property ID As Integer
Public Property Property1_Integer As Integer
Public Property Property2_Single As Single
End Class
Private Sub SetProperty1_Integer()
Dim myClassInstance As New The_Class1
Dim myType As Type = GetType(The_Class1)
Dim propertyInfo As System.Reflection.PropertyInfo = myType.GetProperty("Property1_Integer")
propertyInfo.SetValue(myClassInstance, 1)
MessageBox.Show(myClassInstance.Property1_Integer.ToString)
End Sub
Have fun!

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.

How do I copy Array values to a structure

I would like to to copy that values of an array into a Structure.
Example:
' The Array
Dim Columns(2) As String
' The Structure
Private Structure Fields
Public FName As String
Public LName As String
Public Email As String
End Structure
' I would like to map it like so:
Fields.FName = Columns(0)
Fields.LName = Columns(1)
Fields.Email = Columns(2)
Obviously I could write a function if it was so simple, but really there are over 25 columns and it's a pain to write a function that would map it.
Is there some way to do this?
There really is no simple way that will work in all cases. What you are complaining is too much effort is the only way to guarantee that it will work in all cases.
That said, if you can guarantee that the number of elements in the array matches the number of properties/fields in the structure/class and that they are in the same order and of the same types then you could use Reflection in a loop, e.g.
Private Function Map(source As Object()) As SomeType
Dim result As New SomeType
Dim resultType = result.GetType()
Dim fields = resultType.GetFields()
For i = 0 To source.GetUpperBound(0)
fields(i).SetValue(result, source(i))
Next
Return result
End Function
EDIT:
The code I have provided works as is if SomeType is a class but, as I missed the first time around, not for a structure. The reason is that structures are value types and therefore a copy of the original object is being sent to SetValue, so the field value never gets set on that original object. In theory, to prevent a copy being created, you should be able to simply box the value, i.e. wrap it in an Object reference:
Private Function Map(source As Object()) As SomeType
Dim result As Object = New SomeType
Dim resultType = result.GetType()
Dim fields = resultType.GetFields()
For i = 0 To source.GetUpperBound(0)
fields(i).SetValue(result, source(i))
Next
Return DirectCast(result, SomeType)
End Function
As it turns out though, the VB compiler treats that a little differently than the C# compiler treats the equivalent C# code and it still doesn't work. That's because, in VB, the boxed value gets unboxed before being passed to the method, so a copy is still created. In order to make it work in VB, you need to use a ValueType reference instead of Object:
Private Function Map(source As Object()) As SomeType
Dim result As ValueType = New SomeType
Dim resultType = result.GetType()
Dim fields = resultType.GetFields()
For i = 0 To source.GetUpperBound(0)
fields(i).SetValue(result, source(i))
Next
Return DirectCast(result, SomeType)
End Function

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.

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