"cannot apply indexing with [] to an expression of type object"

  • Thread starter Thread starter Karel Vandenhove
  • Start date Start date
K

Karel Vandenhove

Hi,

I get an error "cannot apply indexing with [] to an expression of type
object" when I try to compile the code below.

SSLScannerManager is a COM component. (Used to access fingerprint
scanners and developed by neurotechnologija)

How does one get an array from a com component?


private void loadScanners()
{
SSLScannerMan scanMan = new SSLScannerMan();
scanMan.Initialize();
try
{
Int32 lenght = scanMan.DeviceCount;
object ids = new String[lenght];
scanMan.EnumDeviceIDs(ref ids);
scanners.Items.Clear();
for (Int32 i = 0; i < ((Array)ids).Length; i++)
{
String strTemp = (string) ((object)ids); // ERROR HERE
scanners.Items.Add(strTemp);
}

if (Scanner != null)
{
selected.Text = Scanner.DeviceID;
}
else
{
selected.Text = "N/A";
}
}
finally
{
scanMan.Finalize();
releaseComObject(scanMan);
}
}
 
String strTemp = (string) ((string[])ids);

Should resolve it - you were trying to index into an object whereas the object is actually a string array - so you can cast it to that.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

I get an error "cannot apply indexing with [] to an expression of type
object" when I try to compile the code below.

SSLScannerManager is a COM component. (Used to access fingerprint
scanners and developed by neurotechnologija)

How does one get an array from a com component?


private void loadScanners()
{
SSLScannerMan scanMan = new SSLScannerMan();
scanMan.Initialize();
try
{
Int32 lenght = scanMan.DeviceCount;
object ids = new String[lenght];
scanMan.EnumDeviceIDs(ref ids);
scanners.Items.Clear();
for (Int32 i = 0; i < ((Array)ids).Length; i++)
{
String strTemp = (string) ((object)ids); // ERROR HERE
scanners.Items.Add(strTemp);
}

if (Scanner != null)
{
selected.Text = Scanner.DeviceID;
}
else
{
selected.Text = "N/A";
}
}
finally
{
scanMan.Finalize();
releaseComObject(scanMan);
}
}

[microsoft.public.dotnet.languages.csharp]
 
Thanks for the quick answer.

When I cast to string[] I get a "Cast is not valid exception" at
runtime.

I guess it is not returning a string. Te VB example used a string
 
Karel Vandenhove said:
Thanks for the quick answer.

When I cast to string[] I get a "Cast is not valid exception" at
runtime.

I guess it is not returning a string. Te VB example used a string

Print out ids.GetType() to find out what type it is, and then use that
as the cast.
 
Back
Top