Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ADO.NET VS2010
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 2:52am
Edited 5-Nov-12 8:59am
v5
Comments
Milind Thakkar at 5-Nov-12 7:56am
   
What is the declaration for Lights ?
GeekBond at 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 at 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
good
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.
 
[Edit]
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.
[/Edit]
  Permalink  
v3
Comments
bikramjeet.sm at 5-Nov-12 8:34am
   
Dear lukeer
 
still same error with your example
lukeer at 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)
bikramjeet.sm at 5-Nov-12 9:36am
   
Hi you can see now what i am trying to do and where is a error
lukeer at 5-Nov-12 10:17am
   
I updated my answer.
bikramjeet.sm at 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 at 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#.
bikramjeet.sm at 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
0 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 George Jonsson 170
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2014
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