本文介绍了索引者:我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

初学者问题,对不起!


我正在使用索引器访问实例中的StringBuilders数组

类:


获取:

value = board1 [i];

设置:

board1 [j] = value1;


我使用上面的方法遍历主程序中的数组来获取/设置

数组的成员。我发现

数组中的一些StringBuilders突然被设置为相同的值!


为了进一步解释,在调试时我看了

数组中的值,例如,当我处理元素12时,元素13,14,
15被设置为我将元素12设置为的相同值。


我可能遇到什么?垃圾收集?线程问题?


干杯,


Cliff


-


你有没有注意到如果某些东西被宣传为有趣或

''搞笑'',它通常不是吗?

解决方案



嗯,你还没有显示索引器的代码,这意味着我们真的要猜测是b $ b。


你能发一个简短但完整的程序来演示

问题吗?


见详情

我的意思是什么。


-

Jon Skeet - < sk *** @ pobox .com>
博客:有关详细信息

我的意思是什么。



嗯,很难知道选择哪些位。我实际上没有运行

以下,因为我不知道哪些位是相关的(是的,我做了

查看URL,是的,我知道我还没完全遵守。


但这很简单。这是董事会课程。比特省略但

不是来自这些方法。


命名空间Blahblah

{


公共类董事会

{

私人StringBuilder []网格;


公共董事会()

{

grid = new StringBuilder [81];

for(int i = 0; i< 81; i ++)

{

grid [i] = new StringBuilder(" 123456789",9);

}

}

public StringBuilder这[int index]

{

get

{

return grid [index];

}

set

{

grid [index] = value;

}

}

}


这里是主程序:


命名空间Blahblah

{

//静态班级课程

班级课程

{

/ //< summary>

///应用程序的主要入口点。

///< / summary>

/ /阅读文件

公共站点c程序pgm;

public static Board board1;

public static Form1 myForm;


[STAThread]

static void Main()

{

pgm = new Program();

board1 = pgm.getBoard();

//表单省略了..

}

//省略了东西。

public void processGrid()

{

StringBuilder value = new StringBuilder();

StringBuilder value1 = new StringBuilder();

...

value.Length = 0; //将长度设置为0;

value.Append(board1 [index] .ToString());

...

while(真的)

{

...

value1.Length = 0;

value1.Append(board1 [ (第* 9行)+ j] .ToString());

...

}

}

}

}


干杯,


悬崖


-


你有没有注意到如果有什么东西被宣传为''有趣''或

''搞笑'',它通常不是?




你没有发布任何实际设置索引值的代码。所有的

用途都是获得的。你也没有发布实际编译的代码和

运行。请重新阅读Jon的帖子,以及他引用你的链接,关于

一个简短但完整的程序。


那说,你的索引器看起来很简单......似乎很难相信

它实际上涉及到这个问题。我的猜测是你可以用一个简单的StringBuilder []数组替换这个类,并且你会遇到

同样的问题。既然你没有用问题发布代码,那么任何人都不可能指出错误。


Pete

Beginner question, sorry!

I am using indexers to access an array of StringBuilders in an instance
of a class:

Getting:
value = board1[i];
Setting:
board1[j] = value1 ;

I loop through the array in the main program using the above to get/set
the members of the array. I find that some of the StringBuilders in the
array are suddenly being set to the same value!

To explain a bit further, while debugging I watch the values in the
array, and when I am processing element 12 for example, elements 13, 14,
15 are set to the same value that I set element 12 to.

What might I be running into? Garbage collection? Thread issues?

Cheers,

Cliff

--

Have you ever noticed that if something is advertised as ''amusing'' or
''hilarious'', it usually isn''t?

解决方案

Well, you haven''t shown the code for the indexer, which means we''re
guessing really.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too



Well, you haven''t shown the code for the indexer, which means we''re
guessing really.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Well, it''s difficult to know which bits to choose. I''ve not actually run
the following, since I don''t know which bits are relevant (yeah, I did
look at the URL, and yeah, I know I haven''t totally complied)

It''s pretty simple though. Here''s the ''Board'' class. Bits omitted but
not from these methods.

namespace Blahblah
{

public class Board
{
private StringBuilder[] grid;

public Board()
{
grid = new StringBuilder[81];
for (int i = 0; i < 81; i++)
{
grid[i] = new StringBuilder("123456789", 9);
}
}
public StringBuilder this[int index]
{
get
{
return grid[index];
}
set
{
grid[index] = value;
}
}
}

Here''s the main program:

namespace Blahblah
{
// static class Program
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
// Read a file
public static Program pgm;
public static Board board1;
public static Form1 myForm;

[STAThread]
static void Main()
{
pgm = new Program();
board1 = pgm.getBoard();
// Form stuff omitted..
}
// Stuff omitted.
public void processGrid()
{
StringBuilder value = new StringBuilder() ;
StringBuilder value1 = new StringBuilder() ;
...
value.Length = 0; // Set Length to 0 ;
value.Append( board1[index].ToString() ) ;
...
while (true)
{
...
value1.Length = 0 ;
value1.Append( board1[ (line * 9) + j ].ToString() ) ;
...
}
}
}
}

Cheers,

Cliff

--

Have you ever noticed that if something is advertised as ''amusing'' or
''hilarious'', it usually isn''t?


You didn''t post any code that actually sets the indexed value. All of the
uses are just gets. Nor did you post code that would actually compile and
run. Please re-read Jon''s post, and the link he refers you to, regarding
"a short but complete program".

That said, your indexer looks pretty simple...it seems hard to believe
that it is actually involved in the problem. My guess is that you could
replace the class with a simple StringBuilder[] array and you''d have the
same problem. And since you didn''t post the code with the problem, it''s
impossible for anyone to point out the mistake.

Pete


这篇关于索引者:我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 00:42