Reputation: 4167
I had a code for getting the hdd id written in vb.net
Now I need to re-write the code into c#. I have converted the vb.net code to c# but it is not compiling.
Below is the vb.net code
Dim hdCollection As ArrayList = New ArrayList()
Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")
For Each wmi_HD As ManagementObject In searcher.Get()
Dim hd As HardDrive = New HardDrive()
hd.Model = wmi_HD("Model").ToString()
hd.Type = wmi_HD("InterfaceType").ToString()
hdCollection.Add(hd)
Next wmi_HD
here is the converted C# code:
ArrayList hdCollection = new ArrayList();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive();
hd.Model = wmi_HD("Model").ToString();
hd.Type = wmi_HD("InterfaceType").ToString();
hdCollection.Add(hd);
}
Following is the error I am getting when compiling the c# code:
'wmi_HD' is a 'variable' but is used like a 'method'
Please help!
Upvotes: 0
Views: 217
Reputation: 5108
Try
hd.Model = wmi_HD["Model"].ToString();
hd.Type = wmi_HD["InterfaceType"].ToString();
Upvotes: 1
Reputation: 117009
Try:
hd.Model = wmi_HD["Model"].ToString();
hd.Type = wmi_HD["InterfaceType"].ToString();
Square brackets, not round.
Upvotes: 1
Reputation: 498904
You have not converted the wmi_HD indexers properly.
Change these lines:
hd.Model = wmi_HD("Model").ToString();
hd.Type = wmi_HD("InterfaceType").ToString();
To:
hd.Model = wmi_HD["Model"].ToString();
hd.Type = wmi_HD["InterfaceType"].ToString();
Upvotes: 1
Reputation: 545488
The VB code performs a subscript (indexed) access. In C#, this converts to a call to the this[]
property. So the call needs square braces in C#:
hd.Model = wmi_HD["Model"].ToString();
hd.Type = wmi_HD["InterfaceType"].ToString();
Apart from that, there’s one thing wrong with both codes: Do not use ArrayList
, the type is obsolete. In fact, the same is true for (most of) the other types in the System.Collections
namespace. The types have been replaced by generic classes in the System.Collections.Generic
namespace.
In your case, you want a List<string>
instead of the ArrayList
(or, in VB, List(Of String)
).
Upvotes: 4