Some theory and a question

I'm curious about controlling the number of objects in existence at any given time. To examine this control, I've partially developed a small till program.

Within this program, I have a key object (a class) called stockItem. stockItem has four attributes and a boolean value, covering price, name, size and barcode number. The boolean value (itemIsValid) denotes whether the store sells this particular item, ergo whether or not it exists in the database.

The class stockItemRetrieval (http://ift.tt/1OuR10r) handles getting the item from the database, and telling the accessor whether or not the operation was successful. I handle this inside a boolean operator called reqSuccessful. If the item does not exist, reqSuccessful returns false.

However, in the event that reqSuccessful returns true, there now exists an object (a stockRecord) in memory.

The class above stockItemRetrieval is salesProcessing (http://ift.tt/2349Ykv). Here, I call reqSuccessful after creating an instance of stockItemRetrieval. If reqSuccessful is determined to be true, I then assign values from stockRecord to the three lists, which will then be used to display data in the form.

However, each time I call the object StockRecord from stockItemRetrieval, I am effectively creating an additional stockRecord object.

How, if possible, do I access the object that I created within the if statement? Creating four separate objects per product seems awfully wasteful, especially considering they all have the same attribute.

Furthermore, is the problem the way I have organised my code?

by r0tekatze via /r/csharp

Leave a Reply