468,738 Members | 2,634 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

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

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)[i]; // ERROR HERE
scanners.Items.Add(strTemp);
}

if (Scanner != null)
{
selected.Text = Scanner.DeviceID;
}
else
{
selected.Text = "N/A";
}
}
finally
{
scanMan.Finalize();
releaseComObject(scanMan);
}
}
Nov 17 '05 #1
3 13035
String strTemp = (string) ((string[])ids)[i];

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)[i]; // 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]
Nov 17 '05 #2


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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Karel Vandenhove <ka*************@yahoo.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

70 posts views Thread by Roy Yao | last post: by
4 posts views Thread by =?Utf-8?B?SmFu?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.