Find value by MethodName(String) in a LinqToSql result? - vb.net

Let's say my Person class look like this:
Private Class Person
Public Property Name As String
Public Property Age As Integer
End Class
and I have a result called linqToSqlResult with name = "John" and Age = 30.
Now I want to get the Name of the person as follows:
Dim name As String = CStr(GetValue(linqResult, "Name"))
And I'm looking for some code like this:
Private Function GetValue(ByRef linqToSqlResult As Object, fieldName As String) As Object
'Here's the code I'm looking for??
End Function
Any idea?

Due to the lack of information, I Assume the value passed as LinqToSqlResult is the ouput of the query of the type Person, If not please explain more clearly the contents of LinqToSqlResult
What you could do is the following (Not the most performant piece of code though):
Imports System.Reflection
Module Module1
Sub Main()
Dim i As Object = New Person With {.Name = "TestUser", .Age = 30}
Console.WriteLine(GetValue(Of Person)(i, "Name").ToString())
Console.WriteLine(Convert.ToInt32(GetValue(Of Person)(i, "Age").ToString()))
Console.ReadLine()
End Sub
Private Function GetValue(Of T)(ByRef linqToSqlResult As Object, fieldName As String) As Object
Dim myProp As PropertyInfo = GetType(T).GetProperties().Where(Function(x) x.Name.Equals(fieldName)).FirstOrDefault()
If (myProp IsNot Nothing) Then
Return myProp.GetValue(linqToSqlResult, Nothing)
Else
Throw New ArgumentException(" Some usefull message")
End If
End Function
End Module
Public Class Person
Public Property Name As String
Public Property Age As String
End Class
This outputs:
//TestUser
//30

Related

How to access object in multidimensional array in Visual Basic

In Visual Basic, I am using a List to store an array of objects. However, I haven't figured out how to access a particular object once I have added it to my list.
I am using Microsoft Visual Studio
Private Results As New List(Of Object)
Dim Results As Object()
Dim CaseStatus as String = "Closed"
...
Results.Add(New Object() {CaseStatus, FlagsStr, OBTotal, OBPaid})
MessageBox.Show(Result(0).ToString)
Displays: System.Object[]
I have also tried MessageBox.Show(Results(0,0).ToString), but don't get the expected results.
When you can .ToString on an object you get the fully qualified name of the object unless the class overrides .ToString. Creating a class and a List (Of T) gives you easy access to any of the properties of the class.
Public Class ProjectCase
Public Property CaseStatus As String
Public Property FlagsString As String
Public Property OBTotal As Decimal
Public Property OBPaid As Decimal
Public Sub New()
'include a default constructor
End Sub
Public Sub New(cs As String, fs As String, total As Decimal, paid As Decimal)
CaseStatus = cs
FlagsString = fs
OBTotal = total
OBPaid = paid
End Sub
Public Overrides Function ToString() As String
Return $"{CaseStatus}, {FlagsString}, Total = {OBTotal}, Paid = {OBPaid}"
End Function
End Class
Private Results As New List(Of ProjectCase)
Private Sub OPCode()
Results.Add(New ProjectCase("Closed", "Some string", 74.36D, 22D))
MessageBox.Show(Results(0).ToString)
MessageBox.Show(Results(0).CaseStatus) 'or any property of your class
End Sub

VB.NET Object with custom name to store property?

I'm not familiar with the type of structure or whatever I need to use to achieve this, but I know that there is one.
I'm trying to make it so that I can reference things something like this:
racerlist(x).compatibilityArr.john.CleatScore
instead of what I have to do now:
racerlist(x).compatibilityArr.CleatScoreArr(y).name/.score
So essentially, I want to add items to the compatibilityarr (will probably have to change to a list which is fine) and be able to reference the racer as their own name, instead of by using an index.
This is one way to build a solution that fits your needs as described above. It requires an embedded class that is built as a List(Of T) where we overload the property to accept a string rather than the integer.
Public Class Foo
Public Property compatibilityArr As New Members
End Class
Public Class Members : Inherits List(Of Member)
Public Overloads ReadOnly Property Item(name As String) As Member
Get
Return Me.Where(Function(i) i.Name = name).FirstOrDefault
End Get
End Property
End Class
Public Class Member
Public Property Name As String
Public Property CleatScore As Integer
End Class
Then to use it:
Public Class Form1
Dim f As New Foo
Private Sub loads() Handles Me.Load
Dim member As New Member With {.Name = "John", .CleatScore = 10}
f.compatibilityArr.Add(member)
MessageBox.Show(f.compatibilityArr.Item("John").CleatScore)
End Sub
End Class
There are other ways to do this, but the simplest is to write a function to search the array by name:
Sub Main1()
Dim racerlist(2) As Racer
racerlist(0) = New Racer With {.Name = "Adam", .CleatScore = "1"}
racerlist(1) = New Racer With {.Name = "Bill", .CleatScore = "2"}
racerlist(2) = New Racer With {.Name = "Charlie", .CleatScore = "3"}
For i As Integer = 0 To racerlist.GetUpperBound(0)
For j As Integer = 0 To racerlist.GetUpperBound(0)
If racerlist(j).Name <> racerlist(i).Name Then
ReDim Preserve racerlist(i).CompatibilityArr(racerlist(i).CompatibilityArr.GetUpperBound(0) + 1)
racerlist(i).CompatibilityArr(racerlist(i).CompatibilityArr.GetUpperBound(0)) = racerlist(j)
End If
Next j
Next i
Dim racerBill As Racer = Racer.FindRacer(racerlist, "Bill")
MsgBox(racerBill.FindCompatibility("Charlie").CleatScore)
End Sub
Class Racer
Property Name As String
Property CleatScore As String
Property CompatibilityArr As Racer()
Sub New()
ReDim CompatibilityArr(-1) 'initialise the array
End Sub
Function FindCompatibility(name As String) As Racer
Return FindRacer(CompatibilityArr, name)
End Function
Shared Function FindRacer(racerlist() As Racer, name As String) As Racer
For i As Integer = 0 To racerlist.GetUpperBound(0)
If racerlist(i).Name = name Then
Return racerlist(i)
End If
Next i
Return Nothing
End Function
End Class
As #Codexer mentioned, I used a dictionary to achieve this.
In my list of Racers (RacerList), I have RacerCompatibility, which I created similar to below:
Public RacerCompatibility As New Dictionary(Of String, Compatibility)
Compatibility is created like:
Public Class Compatibility
Public Cleat As Boolean
Public Skill As Integer
Public Height As Integer
End Class
So now I can access the compatibility of a racer inside the list like:
RacerList(x).RacerCompatibility.Item("John")

there is any way to instantiate a new class based on enum?

there is any way to instantiate a new class based on enum through reflection?
Basicly i wanna to remove the Select Case. I have alot of Enum and builders....
For example:
Public MustInherit Class GeneralClass
'...
Enum GeneralClassType
A = 1
B = 2
End Enum
Public Shared Function BuildClass(Val as Integer, ParamArray par() as Object) as GeneralClass
Dim NewObject as GeneralClass = Nothing
Select Case Ctype(Val, GeneralClassType)
Case GeneralClassType.A
NewObject = new A
Case GeneralClassType.B
NewObject = new B
Case else
throw new exception("invalid type.")
end select
NewObject.setPar(par)
return NewObject
end function
End Class
Public Class A
Inherits GeneralClass
'...
End Class
Public Class B
Inherits GeneralClass
'...
End Class
The Function BuildClass build a class base on a type and parameters get from Database.
But i need to have the Case to create a new instance of a type.
I know i can instantiate a class through reflection, but only if you know the final type.
There is no way to do this dinamicy, like save the class name on database?
public function InstantiateClass(of T)() as T
Dim NewObject as GeneralClass = GetType(T).GetConstructor(New Type() {}).Invoke(New Object() {})
return NewObject
end class
than i can get something like
Dim Var1 as GeneralClass = InstantiateClass(of A)()
and use this instance inside the Build function. But here i need to know the Type A
BaseTest example, working with enum name.
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim BaseFlag As Integer = 2
Dim BaseName As String = "Create a class from enum"
Dim T As BaseTest = BaseTest.Build(BaseFlag, BaseName)
MsgBox(T.GetClassTypeName)
End Sub
End Class
Public MustInherit Class BaseTest
Protected Name As String
Enum TestType
TestA = 1
TestB = 2
TestC = 3
End Enum
Public Function GetClassTypeName() As String
Return String.Concat(Name, ". The final class is: ", Me.GetType.FullName, "")
End Function
Public Shared Function Build(BaseFlag As Integer, Name As String) As BaseTest
Dim NS As String = GetType(BaseTest).FullName
Dim Index As Integer = NS.LastIndexOf(".") + 1
NS = NS.Substring(0, Index)
Dim ClassType As String = CType(BaseFlag, TestType).ToString()
Dim Test As BaseTest = Activator.CreateInstance(Type.GetType(NS & ClassType))
Test.Name = Name
Return Test
End Function
End Class
Public Class TestB
Inherits BaseTest
End Class
The first step is to figure out how to get reflection to create an object instance given the class name as a string. Here's an example of one way to do that:
Dim o As Object = Activator.CreateInstance(Type.GetType("MyNameSpace.A"))
Once you have that working, all you need to do is to get the string name of the enumeration value. To do that, all you need to do is to call the ToString method on the enumeration object, for instance, in your case:
Dim className As String = CType(Val, GeneralClassType).ToString()
Once you have that, you can simply concatenate the class name to the namespace to create the object:
Dim o As Object = Activator.CreateInstance(Type.GetType("MyNameSpace." & className))
The example you posted is type-safe, whereas using reflection, like this, is not. Also, reflection can be a bit slower. You need to decide, based on your particular situation, whether or not it is worth giving up that type-safety and efficiency.

Compiler Error while trying to call overloaded contstructors in VB.NET

Could someone explain to me why I get a compiler error when I try to call a base class' constructor from an inherited object? I've included a brief example of what I'm referring to.
Public Class Person
Public name As String
Public Sub New()
name = "John Doe"
End Sub
Public Sub New(Name As String)
name = Name
End Sub
End Class
Public Class NamedPerson
Inherits Person
Public Sub New(FirstName As String, LastName As String)
name = FirstName & " " & LastName
End Sub
'adding this makes it work
Public Sub New(Name As String)
MyBase.New(Name)
End Sub
End Class
'Valid
Dim guy1 As Person = New Person()
'Valid
Dim guy2 As Person = New Person("John Smith")
'Valid
Dim guy3 As NamedPerson = New NamedPerson("John", "Smith")
'Compiler Error
Dim guy4 As NamedPerson = New NamedPerson("John Smith")
Child classes do not inherit constructors from their base types. A child class is responsible for defining it's own constructors. Additionally it must ensure that each constructor it defines either implicitly or explicitly calls into a base class constructor or chains to another constructor in the same type.
For more info See: Instance Constructors
From your sample classes,
Public Class NamedPerson
Inherits Person
Public Sub New(Name As String)
MyBase.New(Name)
End Sub
Public sub New(FirstName As String, LastName As String)
name = FirstName & " " & LastName
End Sub
End Class

VB.NET CType: How do I use CType to change an object variable "obj" to my custom class that I reference using a string variable like obj.GetType.Name?

The code below works for the class that I hard coded "XCCustomers" in my RetrieveIDandName method where I use CType. However, I would like to be able to pass in various classes and property names to get the integer and string LIST returned. For example, in my code below, I would like to also pass in "XCEmployees" to my RetrieveIDandName method. I feel so close... I was hoping someone knew how to use CType where I can pass in the class name as a string variable.
Note, all the other examples I have seen and tried fail because we are using Option Strict On which disallows late binding. That is why I need to use CType.
I also studied the "Activator.CreateInstance" code examples to try to get the class reference instance by string name but I was unable to get CType to work with that.
When I use obj.GetType.Name or obj.GetType.FullName in place of the "XCCustomers" in CType(obj, XCCustomers)(i)
I get the error "Type 'obj.GetType.Name' is not defined" or "Type 'obj.GetType.FullName' is not defined"
Thanks for your help.
Rick
'+++++++++++++++++++++++++++++++
Imports DataLaasXC.Business
Imports DataLaasXC.Utilities
Public Class ucCustomerList
'Here is the calling method:
Public Sub CallingSub()
Dim customerList As New XCCustomers()
Dim customerIdAndName As New List(Of XCCustomer) = RetrieveIDandName(customerList, "CustomerId", " CustomerName")
'This code below fails because I had to hard code “XCCustomer” in the “Dim item...” section of my RetrieveEmployeesIDandName method.
Dim employeeList As New XCEmployees()
Dim employeeIdAndName As New List(Of XCEmployee) = RetrieveIDandName(employeeList, "EmployeeId", " EmployeeName")
'doing stuff here...
End Sub
'Here is the method where I would like to use the class name string when I use CType:
Private Function RetrieveIDandName(ByVal obj As Object, ByVal idPropName As String, ByVal namePropName As String) As List(Of IntStringPair)
Dim selectedItems As List(Of IntStringPair) = New List(Of IntStringPair)
Dim fullyQualifiedClassName As String = obj.GetType.FullName
Dim count As Integer = CInt(obj.GetType().GetProperty("Count").GetValue(obj, Nothing))
If (count > 0) Then
For i As Integer = 0 To count - 1
'Rather than hard coding “XCCustomer” below, I want to use something like “obj.GetType.Name”???
Dim Item As IntStringPair = New IntStringPair(CInt(CType(obj, XCCustomers)(i).GetType().GetProperty("CustomerId").GetValue(CType(obj, XCCustomers)(i), Nothing)), _
CStr(CType(obj, XCCustomers)(i).GetType().GetProperty("CustomerName").GetValue(CType(obj, XCCustomers)(i), Nothing)))
selectedItems.Add(Item)
Next
End If
Return selectedItems
End Function
End Class
'+++++++++++++++++++++++++++++++
' Below are the supporting classes if you need to see what else is happening:
Namespace DataLaasXC.Utilities
Public Class IntStringPair
Public Sub New(ByVal _Key As Integer, ByVal _Value As String)
Value = _Value
Key = _Key
End Sub
Public Property Value As String
Public Property Key As Integer
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCCustomer
Public Property CustomerId As Integer
Public Property CustomerName As String
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCCustomers
Inherits List(Of XCCustomer)
Public Sub New()
PopulateCustomersFromDatabase()
End Sub
Public Sub New(ByVal GetEmpty As Boolean)
End Sub
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCEmployee
Public Property EmployeeId As Integer
Public Property EmployeeName As String
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCEmployees
Inherits List(Of XCEmployee)
Public Sub New()
PopulateEmployeesFromDatabase()
End Sub
Public Sub New(ByVal GetEmpty As Boolean)
End Sub
End Class
End Namespace
From MSDN
CType(expression, typename)
. . .
typename : Any expression that is legal
within an As clause in a Dim
statement, that is, the name of any
data type, object, structure, class,
or interface.
This is basically saying you can't use CType dynamically, just statically. i.e. At the point where the code is compiled the compiler needs to know what typename is going to be.
You can't change this at runtime.
Hope this helps.
Since List(Of T) implements the non-generic IList interface, you could change your function declaration to:
Private Function RetrieveIDandName(ByVal obj As System.Collections.IList, ByVal idPropName As String, ByVal namePropName As String) As List(Of IntStringPair)
And then your troublesome line would become (with also using the property name parameters):
Dim Item As IntStringPair = New IntStringPair(CInt(obj(i).GetType().GetProperty(idPropName).GetValue(obj(i), Nothing)), _
CStr(obj(i).GetType().GetProperty(namePropName).GetValue(obj(i), Nothing)))
Of course, you could still have the first parameter by Object, and then attempt to cast to IList, but that's up to you.
ctype is used to convert in object type.