p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer (http://p2p.wrox.com/forumdisplay.php?f=574)
-   -   Chapter 34 Ex.2 (http://p2p.wrox.com/showthread.php?t=89753)

Mech_1000 April 2nd, 2013 08:47 PM

Chapter 34 Ex.2

I didn't have problems working on this program and understand most of what is happening - however I had a question about where some data goes.

Section of Code:
//Parse the values
string item = fields[0];
decimal unitPrice = decimal.Parse(fields[1], NumberStyles.Any);
int quantity = int.Parse(fields[2], NumberStyles.Any);
decimal totalPrice = decimal.Parse(fields[3], NumberStyles.Any);
//Try to add a 5th field, might break data
decimal rndPrice = decimal.Parse(fields[4], NumberStyles.Any);

//Display on gridView (Takes the variables defined above and creates an object with those values.
object[] values =
rndPrice.ToString("C") //5th Field


I changed the text documents to have a 5th field (decimal number). The object values[] now holds 5 pieces of data. I add the data to the row and expected an error (out of bounds or undefined cell).

My questions are (sorry trying to keep it simple)
What happened the the fifth field in (object)values when it was added to the valuesDataGrid.Rows? Did it enter some sort of memory loss, can the value be recovered without using (object)values[4]? Or does C# do something else like automatically clean up the mistake and disregard it, which could be a problem if the user needed all the values. Lastly if this does result in some sort of memory loss would the 'using' statement w/ IDisposable be of any use in this case?

Rod Stephens April 3rd, 2013 10:03 AM

I'm not sure I understand so let me know if this doesn't make sense.

It looks like you're creating an array with 5 values and passing it to a DataGrid that expects only 4 values. My guess is the DataGrid is ignoring the extra value because it doesn't know what to do with it.

Some controls like DataGrid are pretty "smart." They can do things like display named data values in particular fields. They know enough to ignore values that don't have names that they are interested in. For example, you could pass an Employee record to a DataGrid and it would display only the fields that it knew about. That would let you display only some of the data if you want to.

You might try putting the new data in one of the earlier array entries and see if it shows the new value and drops the last one.

Mech_1000 April 3rd, 2013 03:37 PM

Thanks that's sort of what I was getting at by the DataGrid dropping the 5th field. I hate being redundant, but just to be 100% clear:

The only other question was is that data deleted or does the DataGrid actually create a place for the 5th (ignored) field but just not display it?

Rod Stephens April 3rd, 2013 07:45 PM

As far as I know, the DataGrid shouldn't change any values that it doesn't display.

All times are GMT -4. The time now is 08:19 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.