initialize, set, and Return Structure inline? - vb.net

In vb.net, is it possible to initialize, set, and Return a Structure inline?
If I have this Structure
Structure CopyAndPasteUniquesAndExistenceStructure
Dim duplicatePoints As List(Of Array)
Dim inexistentPoints As List(Of Array)
Dim insertedRows As List(Of Object)
End Structure
How can I do this pseudo?
Return New CopyAndPasteUniquesAndExistenceStructure...

Return New CopyAndPasteUniquesAndExistenceStructure With {.duplicatePoints = New List(Of Array), .inexistentPoints = New List(Of Array), .insertedRows = New List(Of Object)}
Or you can add a specialized constructor. This is useful if you want to validate the input, or if you want to make the lists read-only (which usually makes a lot of sense for lists).

Related

Is there a inline variant for New List(Of structure) without define extra structure or functions?

I habe a vb.net function that uses a
Dim mylist As New List(Of Object)
and populate this from two different datasets (linq/sql-db), where the items have the same structure
mylist.addRange(someLINQquery.Select(Function(v) New With {.A = v.bla_boolean, .B= v.blub_string, .C = v.stuff_string}))
Now I'm curious if I can define the New List(Of Object) in a way to define the structure of this Object, but without define extra an structure somewhere just for this single function.
I know, I could use a structure, but I want to get it inline.

vb.net create objects in for each loop from partial info in Ienumerable

Summary:I want to create objects in a for each loop.
I have a class - Dashboard, which has some properties - length, height, etc.
This info is contained within an XML document, but my class' properties is only a small subset of the information in the XML.
I have created a Collection of XElement, and I can iterate over these, but how do I create my object on each iteration of the loop?
Dim Col_DashBoards As IEnumerable(Of XElement) = From XDashboard In XDocument.Load(filename).Descendants("dashboard")
For Each XDashboard In Col_DashBoards
'PseudoCode
Dim Xdashboard.Name as New DashboardClassObject
Xdashboard.Name.Height = XDashboard.Element("Height").value
...
Next
If I have understood your question correctly, you wish to create a new object based on a subset of data from within an XML document?
The below is a function that will generate a new DashboardClassObject for every matching node and populate that object. A list of type DashboardclassObject is returned.
Public Function GenerateDashBoardFromXML(filename as string) As List(Of DashboardClassObject)
Dim dashboardList As List(Of DashboardClassObject) = New List(Of DashboardClassObject)()
Dim Col_DashBoards As IEnumerable(Of XElement) = From XDashboard In XDocument.Load(filename)?.Descendants("dashboard")
For Each XDashboard In Col_DashBoards
Dim dashboard As DashboardClassObject = New DashboardClassObject () With {
.Name = XDashboard.Element("Height")?.value
}
dashboardList.Add(dashboard)
Next
return dashboardList
End Function
It should be noted that Null checking is used here. The below is only populated if the matching element is found. Null coalescing operator is also an option here.
.Name = XDashboard.Element("Height")?.value

Defining the type of a List in vb.net at runtime

I'm creating a class at run time using typebuilder and after I create this class I want to define its type for a list like
dim fooList as new List(of DynamicClassName)
Since this doesn't exist at compile time of course it throws an error. When I generate this type I return the type so I can't do something like
dim newType = createNewType(foobar)
dim fooList as new List(of getType(newType))
How do I assign the type of a List at runtime?
You can create a List(Of T), but AFAIK you won't be able to cast it to a typed object. I've used the String type in the following example.
Dim list As Object = Activator.CreateInstance(GetType(List(Of )).MakeGenericType(New Type() {GetType(String)}))
Debug.WriteLine((TypeOf list Is List(Of String)).ToString())
Output
True
So in your case it would look like this:
Dim newType = createNewType(foobar)
'Creates a List(Of foobar):
Dim list As IList = Ctype(Activator.CreateInstance(GetType(List(Of )).MakeGenericType(New Type() {newType})), IList)
'Creates a BindingList(Of foobar):
Dim bindingList As IBindingList = Ctype(Activator.CreateInstance(GetType(BindingList(Of )).MakeGenericType(New Type() {newType})), IBindingList)
This does not answer your question, but may solve your problem.
Another option would be to use an ArrayList (for which you don't have to assign a type). You can see the details here: http://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx.
Here is a basic example:
Dim anyArrayList as new ArrayList()
anyArrayList.add("Hello")
anyArrayList.add("Testing")

Why does VB.NET behave like this with lists?

This is a question concerning a list of lists.
Dim smallList As New List(Of Integer)
Dim largeList As New List(Of List(Of Integer))
smallList.Add(3)
largeList.Add(smallList)
smallList.Clear()
smallList.Add(4)
largeList.Add(smallList)
In this code, I would expect largeList to add the list (3) to itself, and then to add the list (4) to itself. But instead of storing the data inside smallList, it seems to store a reference smallList instead, so ends up containing ((4), (4)), which is not what I want.
Why does it do this, and how can I get around it? Thanks.
When you have a list of reference types, you actually have a list of references. Adding something to the list doesn't mean that the data is copied, it's just the reference that is added to the list.
To add separate objects to the list, you have to create a new object for each item, and as lists are reference types themselves, that goes for lists too.
Dim smallList As List(Of Integer) ' just a reference at this time
Dim largeList As New List(Of List(Of Integer))
smallList = New List(Of Integer)() ' The first list
smallList.Add(3)
largeList.Add(smallList)
smallList = New List(Of Integer)() ' Here's another list
smallList.Add(4)
largeList.Add(smallList)

VB.Net - how can i get the type of the object contained in an List

If I have a list...
dim l as List(of MyClass) = new List(of MyClass)
and I want to get the type of the objects contained in the list, how do I do that?
The obvious answer, that doesn't seem to be possible from my actual implementation, would be to do something like this...
public function GetType(byval AList as IList(of GenericType)) as System.Type
dim lResult as system.type = nothing
if AList.Count > 0 then lResult = AList(0).GetType
return lResult
end function
But what if the list is empty and I still want to know the type it contains?
There's a good article on this at MSDN, here
Basically you can use GetGenericArguments() to get an array of the types provided as arguments to your generic type. In the case of a List, there's only one argument so you will get what you need using eg
dim l as List(of MyClass) = new List(of MyClass)
dim t as Type = (l.GetGenericArguments())(0)