473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Indexers: what am I doing wrong?

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?
Jul 27 '07 #1
10 1895
Enkidu <en********@com.cliffp.comwrote:
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?
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
Jul 27 '07 #2
Jon Skeet [C# MVP] wrote:
Enkidu <en********@com.cliffp.comwrote:
>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?

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?
Jul 27 '07 #3
On Fri, 27 Jul 2007 01:01:25 -0700, Enkidu <en********@com.cliffp.com>
wrote:
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)
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
Jul 27 '07 #4
Peter Duniho wrote:
On Fri, 27 Jul 2007 01:01:25 -0700, Enkidu <en********@com.cliffp.com>
wrote:
>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)

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.
Yes, I know I didn't post a 'working' program, but is this bit not
setting an indexed value? If so, that may be part of my problem.

board1[ (line * 9) + j ] = value1 ;

Yes, I am prepared to do the work and create a 'working' program, but my
difficulty is that, as a beginner, even the simplest things can block me
for ages, whereas a seasoned pro would immediately know what was wrong.
(Sort of like Expert: "You've jiggled the parsnips, but you should have
woggled 'em". Me: "Eh??"). I hoped that this would be the case. It's a
hard life on the steep bit of the learning curve!

Oh well, off to do a bit of coding....

Cheers,

Cliff

--

Have you ever noticed that if something is advertised as 'amusing' or
'hilarious', it usually isn't?
Jul 28 '07 #5
Enkidu wrote:
Peter Duniho wrote:
>On Fri, 27 Jul 2007 01:01:25 -0700, Enkidu <en********@com.cliffp.com>
wrote:
>>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)

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.
Yes, I know I didn't post a 'working' program, but is this bit not
setting an indexed value? If so, that may be part of my problem.

board1[ (line * 9) + j ] = value1 ;

Yes, I am prepared to do the work and create a 'working' program, but my
difficulty is that, as a beginner, even the simplest things can block me
for ages, whereas a seasoned pro would immediately know what was wrong.
(Sort of like Expert: "You've jiggled the parsnips, but you should have
woggled 'em". Me: "Eh??"). I hoped that this would be the case. It's a
hard life on the steep bit of the learning curve!

Oh well, off to do a bit of coding....
Um, can someone advise - the line that follows:

board1[ (line * 9) + j ] = value1 ;

Does this copy the contents of the object 'value1' to the object
'board1[ (line * 9) + j ]' or does it set the object reference of
'board1[ (line * 9) + j ]' to the object reference of 'value1'?

Cheers,

Cliff

--

Have you ever noticed that if something is advertised as 'amusing' or
'hilarious', it usually isn't?
Jul 28 '07 #6
Enkidu <en********@com.cliffp.comwrote:
Oh well, off to do a bit of coding....
Um, can someone advise - the line that follows:

board1[ (line * 9) + j ] = value1 ;

Does this copy the contents of the object 'value1' to the object
'board1[ (line * 9) + j ]' or does it set the object reference of
'board1[ (line * 9) + j ]' to the object reference of 'value1'?
The latter. It's copying the value of "value1" - which is just a
reference to an object.

--
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
Jul 28 '07 #7
Jon Skeet [C# MVP] wrote:
Enkidu <en********@com.cliffp.comwrote:
>>Oh well, off to do a bit of coding....
>Um, can someone advise - the line that follows:

board1[ (line * 9) + j ] = value1 ;

Does this copy the contents of the object 'value1' to the object
'board1[ (line * 9) + j ]' or does it set the object reference of
'board1[ (line * 9) + j ]' to the object reference of 'value1'?

The latter. It's copying the value of "value1" - which is just a
reference to an object.
Ah, then that is probably my prob, thanks,

Cheers,

Cliff

--

Have you ever noticed that if something is advertised as 'amusing' or
'hilarious', it usually isn't?
Jul 28 '07 #8
Enkidu wrote:
Jon Skeet [C# MVP] wrote:
>Enkidu <en********@com.cliffp.comwrote:
>>>Oh well, off to do a bit of coding....
>>Um, can someone advise - the line that follows:

board1[ (line * 9) + j ] = value1 ;

Does this copy the contents of the object 'value1' to the object
'board1[ (line * 9) + j ]' or does it set the object reference of
'board1[ (line * 9) + j ]' to the object reference of 'value1'?

The latter. It's copying the value of "value1" - which is just a
reference to an object.
Ah, then that is probably my prob, thanks,
Thanks Jon, that *was* my problem!!

I changed it to:

board1[(line * 9) + j].Length = 0;
board1[(line * 9) + j].Append(value1.ToString());

That appears to work, though it may not be the best or preferred way!

Cheers,

Cliff

--

Have you ever noticed that if something is advertised as 'amusing' or
'hilarious', it usually isn't?
Jul 28 '07 #9
Enkidu wrote:
Yes, I know I didn't post a 'working' program, but is this bit not
setting an indexed value? If so, that may be part of my problem.

board1[ (line * 9) + j ] = value1 ;
Maybe I'm blind, but I don't see that line of code in the code you posted.
Yes, I am prepared to do the work and create a 'working' program, but my
difficulty is that, as a beginner, even the simplest things can block me
for ages, whereas a seasoned pro would immediately know what was wrong.
Even a seasoned pro cannot tell you what's wrong in code that you don't
post.

Beyond that, part of relying on a seasoned pro to point out your own
mistake rather than figuring it out yourself is to do as asked when a
seasoned pro asks you to formulate your question in a way that will
facilitate answering it. Not only will doing so help _you_, it avoids
having the seasoned pro dismiss your question due to lack of cooperation
on your part.

Finally, one very important part about coming up with an actual
concise-but-complete sample of code that reliably reproduces the problem
is that in refactoring your existing code to remove the irrelevant parts
is often sufficient for figuring out the problem yourself. The exercise
itself can either make the bug more apparent, or cause it to go away
when you take out some piece of the code, the latter being a very good
indication to you of what piece of the code is causing problems.

And I will note that that last paragraph does not apply only to
beginners. Just the other day, I had a problem in my code that was
causing me no end of frustration (it was related to the behavior of the
XmlReader class). I was just about to post a question here, and was
preparing my concise-but-complete code sample, when the bug in my code
revealed itself. The mere process of creating a question that was
actually appropriate to ask helped me solve my problem all by myself.

Now, all that said:

If the line of code that you posted alone in your reply is inside the
while() loop and you do not create a new StringBuilder to assign to
value1 each time through the loop, then that's your problem right there.
You are assigning the same StringBuilder to each element, and so any
time you change that one StringBuilder, changes to it are of course
reflected in each indexed element that was assigned to it.

Pete
Jul 28 '07 #10
Peter Duniho wrote:
Enkidu wrote:
>Yes, I know I didn't post a 'working' program, but is this bit not
setting an indexed value? If so, that may be part of my problem.

board1[ (line * 9) + j ] = value1 ;

Maybe I'm blind, but I don't see that line of code in the code you
posted.
OK, it was intended and if it wasn't there, mea culpa.
>>
Yes, I am prepared to do the work and create a 'working' program,
but my difficulty is that, as a beginner, even the simplest things
can block me for ages, whereas a seasoned pro would immediately
know what was wrong.

Even a seasoned pro cannot tell you what's wrong in code that you
don't post.
Absolutely. But the *symptom* may be indicative of a particular type of
error. I was looking for a quick answer, but if the quick answer is not
appropriate (as you have indicated), I'm quite prepared to do the work.
>
Beyond that, part of relying on a seasoned pro to point out your own
mistake rather than figuring it out yourself is to do as asked when
a seasoned pro asks you to formulate your question in a way that will
facilitate answering it. Not only will doing so help _you_, it
avoids having the seasoned pro dismiss your question due to lack of
cooperation on your part.
I did figure it out myself! In hindsight a problem with lots of
variables changing at once almost HAS to be a reference problem.
>
Finally, one very important part about coming up with an actual
concise-but-complete sample of code that reliably reproduces the
problem is that in refactoring your existing code to remove the
irrelevant parts is often sufficient for figuring out the problem
yourself. The exercise itself can either make the bug more apparent,
or cause it to go away when you take out some piece of the code, the
latter being a very good indication to you of what piece of the code
is causing problems.
Yes indeed, no argument, and that is what I went on to do.
>
And I will note that that last paragraph does not apply only to
beginners. Just the other day, I had a problem in my code that was
causing me no end of frustration (it was related to the behavior of
the XmlReader class). I was just about to post a question here, and
was preparing my concise-but-complete code sample, when the bug in my
code revealed itself. The mere process of creating a question that
was actually appropriate to ask helped me solve my problem all by
myself.
Yes, it often happens that way.
>
Now, all that said:

If the line of code that you posted alone in your reply is inside the
while() loop and you do not create a new StringBuilder to assign to
value1 each time through the loop, then that's your problem right
there. You are assigning the same StringBuilder to each element, and
so any time you change that one StringBuilder, changes to it are of
course reflected in each indexed element that was assigned to it.
That's exactly what the problem was! I'd already figured it out, but
thanks!

Cheers,

Cliff

--

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Alvin Bruney | last post by:
In later versions of the framework is it possible to have indexers on properties for member fields? Say for example I have private ArrayList blah; and a propget public object Blah
12
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
3
by: DKode | last post by:
Ok, Consider the following example: School Class - StudentCollection Property StudentCollection Class : CollectionBase - Add - Item
2
by: Jim | last post by:
How does one determine what indexers are available on a given object? The only way I have found is by looking at the Object Browser. But even then it only gives a simple signature like, this....
1
by: mdub317 | last post by:
I'm totally new to programming and I am wondering; when would be a good time to use an array or an indexer? I want to know what types of applications would make good use of arrays or indexers. ...
5
by: bonk | last post by:
Hello, IL does not have indexers. Infact the c# compiler compiles indexers to Set_Item and Get_Item (or whatever name I choose via the IndexerNameAttribute ). So how does c# (compiler) know...
4
by: tg.foobar | last post by:
i'd like to do the following, but i don't think it's possible. can you help me find a way to do this, or maybe a better way to write the code? I have a list of items that need to be modified...
5
by: Jeremy McPeak | last post by:
I am writing a collection. I downloaded the Rotor kit and started looking at how the classes in System.Xml are written and am using them as a guide. For my collection, I thought I'd use XmlNodeList...
3
by: Benssol | last post by:
Hi all great programmers and great coders Please can anyone explain clearly the following: usage of indexers? is it used widely (in most applications)? is there is another way that do its...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.