Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Enum not working correct in array index?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe Rattz - 23 Aug 2006 18:38 GMT
Ok, I can't believe what I am seeing.  I am sure I do this other places with
no problems, but I sure can't here.

I have some code that is indexing into the ItemArray in a DataSet's DataRow.
I basically want to change the value of the data stored at a particular
index in the ItemArray.  Ideally, I would like for my code to look like this
(pay attention to the index of ItemArray):

row.ItemArray[(int)GridColumnIndexes.Part - 1] =
 itemArray[(int)GridColumnIndexes.Part - 1].ToString().Substring(3);

This would allow me to chop off the first 3 characters.

However, the expression (int)GridColumnIndexes.Part - 1 is not evaluating to
the correct index in the ItemArray!!!

First, here is my GridColumnIndexes enum:

       protected enum GridColumnIndexes : int
       {
           Select = 0,
           Line,
           Part,
           GroupCode,
           BackorderQuantity,
           OrderDate,
           PO,
           OrderNumber,
           OrderType,
       }

The Part value is 2, so I should be indexing into the 2nd element (with an
index of 1) in ItemArray.

So, the statement:

row.ItemArray[(int)GridColumnIndexes.Part - 1] =
 itemArray[(int)GridColumnIndexes.Part - 1].ToString().Substring(3);

should be the same as:

row.ItemArray[1] =
 itemArray[(int)GridColumnIndexes.Part - 1].ToString().Substring(3);

but it is not.

Please look at the following image from my debugger:  I am going to try to
encode an img link, but just in case, here is a link to the image:

http://www.netsplore.com/PublicPortal/Default.aspx?tabid=276

<img src='http://www.netsplore.com/PublicPortal/Default.aspx?tabid=276'>

Notice that in the watch window,

   row.ItemArray[(int)GridColumnIndexes.Part - 1]

and

   row.ItemArray[index]

are not accessing the same element of ItemArray even though I set index to
(int)GridColumnIndexes.Part - 1.

Anyone have any idea what is going on here?

Thanks.
Bruce Wood - 23 Aug 2006 19:07 GMT
> Ok, I can't believe what I am seeing.  I am sure I do this other places with
> no problems, but I sure can't here.
[quoted text clipped - 63 lines]
>
> Thanks.

In this case I would suspect the debugger of lying to me.  Try this:

int gridIndex = (int)GridColumnIndexes.Part - 1;

and see what the debugger says gridIndex is set to.
Joe Rattz - 23 Aug 2006 19:34 GMT
I am a little confused.  I already did that except I named my variable index
instead of gridIndex.  As you can see in the screenshot I provide the link
for, I set index = to what you say, and you can see its value in the watch
window.

You think the debugger is lying?

> > Ok, I can't believe what I am seeing.  I am sure I do this other places with
> > no problems, but I sure can't here.
[quoted text clipped - 69 lines]
>
> and see what the debugger says gridIndex is set to.
Jon Skeet [C# MVP] - 23 Aug 2006 20:04 GMT
> I am a little confused.  I already did that except I named my variable index
> instead of gridIndex.  As you can see in the screenshot I provide the link
> for, I set index = to what you say, and you can see its value in the watch
> window.
>
> You think the debugger is lying?

I think it would be good to see a short but complete program
demonstrating the problem.

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

Signature

Jon Skeet - <skeet@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

Joe Rattz - 23 Aug 2006 20:23 GMT
Due to the issue that Stoitcho mentioned, I have changed my approach and no
longer have the code.  However, I stand by that original problem.  I offer
the screenshot of my debugger as evidence.  That image is not doctored in any
way.  I have no idea what was going on there.  If you look at the value I set
index equal to, and the two bottom expressions in the watch window, you can
see that the ItemArray was being indexed into at two different locations,
even though it seems like it should have been the same location.

BTW, those two Values displayed for the two bottom watch expressions are
legitimate indexes into the array.  array[0] was equal to "AMM", and array[1]
was equal to "AMM691410".  I didn't want anyone to think that one expression
is seeing only part of the array's value.  Its just that both of those two
bottom watch expressions should have seen the same array index, index 1, with
the value of "AMM691410".  For some reason, the third watch expression was
indexing into the wrong location of the array.

Thanks.

> > I am a little confused.  I already did that except I named my variable index
> > instead of gridIndex.  As you can see in the screenshot I provide the link
[quoted text clipped - 8 lines]
> See http://www.pobox.com/~skeet/csharp/complete.html for details of
> what I mean by that.
Stoitcho Goutsev (100) - 23 Aug 2006 20:05 GMT
Joe,

I'm not going to comment on the debugger screenshot. I just want to say that
you cannot use the ItemArray like this.

You can use this property to set the data for the whole row at once. When
you read this property the DataRow class creates new array and copy the
columns data in it. When the property is set with an array the setter copies
the elements form the array into the datarow. When you change the data at
particular index you don't change the datarow, rather you work with copy of
the data. To apply the changes you need to put the whole array back.

Signature

HTH
Stoitcho Goutsev (100)

>I am a little confused.  I already did that except I named my variable
>index
[quoted text clipped - 86 lines]
>>
>> and see what the debugger says gridIndex is set to.
Joe Rattz - 23 Aug 2006 20:15 GMT
Thanks.  After I posted this problem, I hardcoded my array indexes just to
temporarily get past the problem I posted.  In doing so, I saw that you are
correct, I cannot do it this way.  However, I believe it was the row object
that had a SetValue (or SetValues) method that sure seemed like it should
make this possible.  But even using it, I couldn't get this to work.  Since
then, I have changed approaches altogether.

Thanks.

> Joe,
>
[quoted text clipped - 98 lines]
> >>
> >> and see what the debugger says gridIndex is set to.
Stoitcho Goutsev (100) - 23 Aug 2006 20:36 GMT
Joe,

Just use the indexer of the DataRow class

row[(int)GridColumnIndexes.Part - 1] = row[(int)GridColumnIndexes.Part -
1].ToString().Substring(3);

Signature

HTH
Stoitcho Goutsev (100)

> Thanks.  After I posted this problem, I hardcoded my array indexes just to
> temporarily get past the problem I posted.  In doing so, I saw that you
[quoted text clipped - 123 lines]
>> >>
>> >> and see what the debugger says gridIndex is set to.
Bruce Wood - 23 Aug 2006 21:44 GMT
Sorry. I wasn't looking closely enough at your screen shot.

Why do I think that the debugger is lying? I don't trust the debugger
to evaluate complex expressions in watch windows. I don't even trust it
to show me correct values when I mouse over things. I can't remember
the precise details, but I've often moused over expressions as simple
as myObj.Property and been told that the value of Property is
"something", when in fact it's something else, because I have a local
variable named Property that current has the value "something".
Intellisense, or whatever it is in the debugger that shows mouse-over
values, doesn't "get" the context that this Property isn't the local
variable... it's a property of some object, and so shows me the wrong
value when I mouse over it. This has caused me more than one near heart
attack when debugging my code.

I would trust the value of your Index variable, and trust that the
correct array entry is being updated. If you're unsure, use a
MessageBox.Show or a Console.WriteLine to display what's going on...
old-school.

The debugger often lies to me. MessageBox.Show never does.

> I am a little confused.  I already did that except I named my variable index
> instead of gridIndex.  As you can see in the screenshot I provide the link
[quoted text clipped - 76 lines]
> >
> > and see what the debugger says gridIndex is set to.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.