Like this article? We recommend

Like this article? We recommend

Share Trading Company

If we have a share trading company in which every customer can buy shares of
different companies with different numbers of shares, we have to go with jagged
arrays. In the code presented below, I've asked the number of customers to
be place in memory (if memory is available to store those records, of course);
then we'll send them to the database in a batch.

In this problem, every customer can buy the shares of different companies
whose names are different lengths. The number of companies associated with every
customer can also differ.

Therefore, to optimize the use of available memory, we cannot go with
multi-dimensional arrays because we don't have the fixed number of elements
associated with each customer, and we want to allocate the memory for every
associated element to the customer when the data is being inputted.

In this solution, we use a method named "SendToDatabase", which
shows that we are sending the data stored in memory to the database in a batch,
as we are passing the jagged arrays as reference parameters to the method. We
can enhance this application according to our application needs; this example is
just to show the concept of jagged arrays as the best solution to real-life
problems.

The application designed is a self-explanatory console application that shows
only the optimized part. The database connectivity and visual interface are not
part of our topic, and have been portrayed by "SendToDatabase" methods
and console messages, which can be replaced by required components.