Enum not working correct in array index?

G

Guest

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.
 
B

Bruce Wood

Joe said:
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.

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.
 
G

Guest

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?

Bruce Wood said:
Joe said:
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.

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.
 
J

Jon Skeet [C# MVP]

Joe Rattz said:
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.
 
S

Stoitcho Goutsev \(100\)

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.





--
HTH
Stoitcho Goutsev (100)
Joe Rattz said:
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?

Bruce Wood said:
Joe said:
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.

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.
 
G

Guest

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.

Stoitcho Goutsev (100) said:
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.





--
HTH
Stoitcho Goutsev (100)
Joe Rattz said:
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?

Bruce Wood said:
Joe Rattz wrote:
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.

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.
 
G

Guest

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.
 
S

Stoitcho Goutsev \(100\)

Joe,

Just use the indexer of the DataRow class

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


--
HTH
Stoitcho Goutsev (100)
Joe Rattz said:
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.

Stoitcho Goutsev (100) said:
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.





--
HTH
Stoitcho Goutsev (100)
Joe Rattz said:
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?

:


Joe Rattz wrote:
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.

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.
 
B

Bruce Wood

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.

Joe said:
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?

Bruce Wood said:
Joe said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top