Question Problem passing arguments to a function

Chris Domingo

New member
Joined
Aug 3, 2011
Messages
1
Location
Houston, Texas, United States
Programming Experience
Beginner
I am having a problem figuring this out. The text that is bold is where the problem is. Any help would be appreciated.

Imports Microsoft.Win32

Console.WriteLine(WMILookup("Win32_Bios","SerialNumber"))

Private Function WMILookup(ByVal WMIClass as String,ByVal WMIItem as String) As String
Dim WMI As Object = GetObject("WinMgmts:")
Dim WMIReturn As String = ""
Dim WMIClassObject As Object = wmi.InstancesOf(WMIClass)
For Each WMIItems As Object In WMIClassObject
WMIReturn &= ", " & WMIItems.WMIItem
Next WMIItems
If WMIReturn.Length > 0 Then WMIReturn = WMIReturn.Substring(2)
Return WMIReturn
End Function

This function without the second argument works fine

Imports Microsoft.Win32

Console.WriteLine(WMILookup("Win32_Bios"))

Private Function WMILookup(ByVal WMIClass as String) As String
Dim WMI As Object = GetObject("WinMgmts:")
Dim WMIReturn As String = ""
Dim WMIClassObject As Object = wmi.InstancesOf(WMIClass)
For Each WMIItems As Object In WMIClassObject
WMIReturn &= ", " & WMIItems.SerialNumber
Next WMIItems
If WMIReturn.Length > 0 Then WMIReturn = WMIReturn.Substring(2)
Return WMIReturn
End Function
 
You can't magically turn a String into a property like that. If you want to access a member by name using a String then you must use Reflection. You first need to get a Type object representing the type of the object whose property value you want to access. You then have to get a PropertyInfo from that Type for the property with the specified name. You then use the PropertyInfo to get the value of that property from the object.

get property value using reflection vb.net - Google Search
 
I will recommend that you use the .Net WMI tools from System.Management namespace. Check out http://www.vbdotnetforums.com/web-resources/13972-wmi-code-creator-1-0-a.html for an introduction, this tool can generate VB.Net code samples for the WMI queries you select.
As you also can see if you for example get SerialNumber from Win32_Bios that code would not need reflection, "SerialNumber" is specified as a string.
 
Back
Top