错误是无法将[]的索引应用于类型为'int'的表达式 [英] error is cannot apply indexing with [] to an expression of type 'int'

查看:267
本文介绍了错误是无法将[]的索引应用于类型为'int'的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使窗口窗体应用程序出现错误,这是在调试时无法将带有[]的索引应用于类型为"int"的表达式.

如何解决我的应用程序中的错误

谢谢您

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

推荐答案

从您的变量命名中,我建议索引器应属于lights(复数,因此索引器很有意义),因此我们将得到
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;
}



但是,这给我们留下了一个问题,因为错误消息说Value的类型为已选中 [ ^ ],因为这需要一个 ^ ].

请使用其下方的改善问题"链接来改善您的问题,以提供lights的类型定义.


好的,所以lightsOffset<int>.这不是我所知道的.NET框架的一部分.也许它来自我也不知道的命名空间FSUIPC.

但是从您的代码和注释中,我认为您希望将ligts.Value的各个位分配给多个复选框''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);



您不能直接在C#中访问单个位,而必须使用二进制AND和OR运算来规避它.上面的示例通过将"00000001"左移三次到"00001000",为第三位准备了位掩码.
如果未设置有趣的位,则此掩码AND yourValue给出一个等于零的整数.如果该位置1,则与掩码"相同.现在,您可以使用零进行比较,以获取已检查"的正确值.

为了从各自的Checked值正确设置lights.Value的各个位,您必须进行AND(&)和 ^ ](|)的顺序正确.
[/Edit]



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]


这篇关于错误是无法将[]的索引应用于类型为'int'的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆