Get the type of a List - vb.net

I am using Visual Basic.net 2012.
How can I get the type of a List that I have loaded from serialization?
I can get the type of a single object as follows:
ObjectFromFile.GetType.Name
I have tried the following, with no result:
dim t as Type = (ObjectFromFile.GetGenericArguments())(0)
How can I find the type of a list?

You were close. This:
Dim tt As Type = ObjectFromFile.GetType.GetGenericArguments()(0)
should give you the type of list's items.

Related

type var is not defined vb.net

I found an example in C# and from my understanding there is no alternative to 'var' in VB.NET. I am trying to create a datatable that will populate depending on a LINQ command further down in my code that calls this function. I have searched for a solution, but unable to find anything that works. Any assistance on what I should use would be appreciated. Note that I do have both Option Strict and Option Infer on as well.
Private Shared Function ToDataTable(rows As List(Of DataRow)) As DataTable
Dim table As New DataTable()
table.Columns.Add("Title")
table.Columns.Add("Console")
table.Columns.Add("Year")
table.Columns.Add("ESRB")
table.Columns.Add("Score")
table.Columns.Add("Publisher")
table.Columns.Add("Developer")
table.Columns.Add("Genre")
table.Columns.Add("Date")
For Each row As var In rows
table.Rows.Add(row.ItemArray)
Next
Return table
End Function
C# uses 'var' for implicit typing - VB uses Option Infer On combined with omitting the type.
The VB equivalent is:
Option Infer On
...
For Each row In rows
table.Rows.Add(row.ItemArray)
Next row
.NET already has .CopyToDataTable extension for that:
Dim table As DataTable = rows.CopyToDataTable
The VB equivalent is simply Dim, without any strong typing.
Dim sName = "John Henry"
In this example, the compiler infers type String (when Option Infer is set to On).
In your example, you may omit the As var portion. The compiler will infer type DataRow.
Tag your questions well, in this case there is no C# issue. Your problem is your are not writing an actual type on the foreach statement. This will fix it:
For Each row As DataRow In rows
table.Rows.Add(row.ItemArray)
Next

Save a list of object types in a variable

I want to save a list of object types into a variable.
For example something like this
Dim allowedTypes As New List(Of Type)
allowedTypes.Add(TextBox)
The above produces an error, however I need to save a list of object types in this list so that I could compare the allowedTypes when creating elements dynamically via a loop.
Is this possible in Vb.Net (Any alternative suggestions are welcome).
Call GetType() to get Type object for the specified type :
Dim allowedTypes As New List(Of Type)
allowedTypes.Add(GetType(TextBox))
You'll need to use the GetType method, as in:
Dim allowedTypes as new List(Of Type)
allowedTypes.Add(GetType(TextBox))

Binding VB.net DataRepeater to dataview items at runtime with aggregation using linq

Hoping someone can help me out with what is probably a dumb question.
I'm trying to use a datarepeater to display data generated via LINQ from a datatable
I've managed to do this fine with a filtered existing datasource using:
Me.Tbl_52TableAdapter.Fill(Me.CBRDataSet.tbl_52)
Dim query =
From dlist In CBRDataSet.tbl_52.AsEnumerable
Where (dlist.Field(Of String)("TL") = "CTS 06")
Select dlist
query.CopyToDataTable().AsDataView()
DataRepeater1.DataSource = query
The problem being that I need to aggregate a field in the dataset into a count.
If I replace the query with:
Dim query =
From CountAgent In CBRDataSet.tbl_52.AsEnumerable
Group CountAgent By PBX = CountAgent.Field(Of String)("TL") Into Count()
Select Count
It then states that:
'CopyToDataTable' is not a member of 'System.Collections.Generic.IEnumerable(Of Integer)'
I've tried to get around it by changing the declaration to:
Dim query As IEnumerable(Of DataRow) =
Which compiles, but I have no idea if it works, and I cant check as I can't find a way to bind a label to the produced count col of the dataview.
If anyone can tell me what I'm doing wrong i'd be most appreciative.
Coming back to my own question, in case it helps anyone else: MSDN had the answer -
You need to overload copyToDataTable as described in
"How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow"
on:
http://msdn.microsoft.com/en-us/library/bb669096.aspx

facebook c# SDK vb.net issue

i have been trying to use this sdk with vb.net with dynamic, using the vb sample thats on the site and im getting an error
"Public member 'id' on type 'JsonObject' not found."
thats in the sample GetSampleWithAccessToken referring to is
Dim id = result.id
You're giving too little context, but it seems that result is typed as JsonObject, while it should be typed explicitly as Object for the dynamic features of VB to kick in.
Dim result as Object = ...
Dim id = result.id

VB.NET Parsing an Object type to a GUID type

how can I convert an object type to a GUID type in VB.NET?
I'm not sure what exactly you want but this might help:
Dim g = CType(obj, System.Guid)
If you want to convert a string to a Guid:
Dim g = New Guid(myString)
If you are looking to create the object as a new guid, use the following call:
dim objvar as guid = System.GUID.NewGuid()
edit Your question is a little unclear when you say "convert". If you already have the object created and assigned, use DirectCast to create an object that the Visual Studio environment will recognize.
Mehrdad's sample will work, however it is always best to declare the data type for all your variables:
Dim g As Guid = objectVariable
In this case there is no need to use CType or DirectCast.