Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 2nd, 2013, 08:47 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2013
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default Chapter 34 Ex.2

Hello,

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 =
{
item,
unitPrice.ToString("C"),
quantity.ToString(),
totalPrice.ToString("C"),
rndPrice.ToString("C") //5th Field
};

valuesDataGrid.Rows.Add(values);

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?
  #2 (permalink)  
Old April 3rd, 2013, 10:03 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #3 (permalink)  
Old April 3rd, 2013, 03:37 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2013
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default

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?
  #4 (permalink)  
Old April 3rd, 2013, 07:45 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The chat 34 fly_fish BOOK: Professional C# 4.0 and .NET 4 0 November 16th, 2012 09:11 PM
Ch 34, Printing alniv BOOK: Visual Basic 2010 Programmer's Reference 5 May 15th, 2011 08:45 AM
Page 34 - Projects flapjack BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 April 3rd, 2010 07:50 AM



All times are GMT -4. The time now is 08:02 PM.


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