Click here to Skip to main content
13,142,883 members (52,271 online)
Rate this:
Please Sign up or sign in to vote.
I am trying to make window form application i get a this error is cannot apply indexing with [] to an expression of type 'int' on debug time.

How can i fix this error on my application

Thanks you
Posted 5-Nov-12 1:52am
Updated 5-Nov-12 7:59am
Milind Thakkar 5-Nov-12 7:56am
What is the declaration for Lights ?
GeekBond 5-Nov-12 8:26am
We need more info, like 'What is 'Lights'?' We can't see your screen, please use the 'Improve question' widget to "improve" your question.
PramodSawant 6-Nov-12 4:54am
Just put your code where you got error then we can analyze what is the mistake

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

From your variable naming, I suggest that the indexer would belong to lights (plural, therefore an indexer makes sense), so we would get
this.chkbox1.Checked = lights[0].Value;
// ...
private void chkbox1_CheckedChanged(object sender, EventArgs e)
    this.lights[0].Value = this.chkbox1.Checked;

But this leaves us with the problem that Value is, as the error message seys, of type int[^]. So we cannot assign it to Checked[^], since that requires a value of type bool[^].

Please improve your question using the "Improve question" link just beneath it to provide the type definition for lights.

Ok, so lights is an Offset<int>. That's no part of the .NET framework that I am aware of. Maybe it stems from the namespace FSUIPC, that I don't know as well.

But from your code and comments, I think you want to assing individual bits of ligts.Value to several checkboxes' Checked properties.
Try this one
// long version
int mask = 1 << 3;
int match = lights.Value & mask;
bool bitIsSet = match != 0;
this.chkbox3.Checked = bitIsSet;
// short version
this.chkbox3.Checked = ((lights.Value & (1 << 3)) != 0);

You cannot access individual bits directly in C#, you have to circumvent that with binary AND and OR operations. The above example prepares a bit mask for the third bit by left-shifting '00000001' three times to '00001000'.
This mask AND yourValue gives an integer that equals zero if the interesting bit was not set. It's the same as 'mask' if the bit was set. You can now use a comparison with zero to get the correct value for 'Checked'.

For correctly setting lights.Value's individual bits from their respective Checked values, you have to AND (&) and OR[^] (|) things in the right order.
Comments 5-Nov-12 8:34am
Dear lukeer

still same error with your example
lukeer 5-Nov-12 9:05am
As I said, this is a rough suggestion inspired by your variable naming.
As I and Milind Thakkar and GeekBond already asked before: Show us the actual definitions of those variables. What is lights defined as?
(Remember to use the "Improve question" link I mentioned) 5-Nov-12 9:36am
Hi you can see now what i am trying to do and where is a error
lukeer 5-Nov-12 10:17am
I updated my answer. 5-Nov-12 11:40am
okay i checked so what about this
private void chkbox1_CheckedChanged(object sender, EventArgs e)
error is here : ---
this.lights.Value[0] = this.chkbox1.Checked;
lukeer 6-Nov-12 1:33am
citing myself: "You cannot directly access individual bits of an integer."

Have you heard of bitwise AND and OR operations?
They are represented as '&' and '|' in several programming languages, not just C#. 8-Nov-12 8:02am
okay lukeer thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100