views:

70

answers:

1

I am new to programming and am having difficulty with understanding object lifetime / instantiation.

I have a windows from with a datagridview and a panel.

The datagridview has a checkbox column and a list of names which is populated from a database.

The panel has a few text boxes (e.g. name, age, favourite sports team) and a 'save changes' button.

The idea is that when the user selects a person in the datagridview by checking the appropriate checkbox then the panel shows the relevant data for that person (name, age, fav sports team) by pulling the data from a database. If the user would like to update the person's details they can do so by typing in the textboxes and then clicking the 'save changes' button. All quite simple so far.

The way I have designed this is to have a 'panel' class and to create an instance when the user checks a checkbox. (NB - the reason I have created a 'panel' class is that I intend to replicate the datagridview and panel on other tabpages within my form and thought it would be useful to have a generic 'panel' class that I could re-use.)

My code looks a bit like this:

If CheckBoxClicked Then
Dim UpdatePanel As New UpdatePanel(MyForm.NameTextBox, MyForm.AgeTextBox, MyForm.FavSportTeamTextBox, MyForm.SaveButton)
UpdatePanel.GetData()
...

When the user clicks the SaveButton the data is updated to the database and I have a display message (simple textbox) which says "Update successful" and displays for 5 seconds before hiding itself.

Here is the problem:

Suppose a user checks a checkbox, reviews the data and does not make any changes, and then checks another checkbox and decides to update the data by clicking the 'save changes' button. What happens is that the "Update successful" message is displayed twice.

I think this is because every time the user checks a checkbox an instance of UpdatePanel is created. If a I select five different people using the checkboxes and then hit 'save changes' I get five "Update successful" messages.

I am not sure how to overcome this. It feels like each time a checkbox is clicked I need to check if an instance of UpdatePanel exists and then destroy it. I tried using UpdatePanel = Nothing and also investigated IDisposable and GC.Collect() but with no luck.

My questions are:

  1. How can I ensure that only one instance of the panel is created?
  2. If I create multiple instances of the same object with the same name do they all sit in memory or does the 'computer' only retain a reference to the most recently created? (Is there not a name conflict?)
  3. Have I gone down the wrong track with my deisgn pattern and use of the 'panel' class?

Thanks

A: 

Make sure you are checking whether there were any changes or not before updating the db. Probably any time user reviews a data you have something that adds single update. It would be easier to answer if you post more code as your explanations are not very clear and it is difficult to say where the problem is without seeing most of the code involved.

  1. Use the singleton pattern

  2. It depends on how you creating them. Object created in the method scope is marked to deletion and waits for GC to collect him. If you have your objects stored inside some variable of your WinForm it will keep them from GC. So by default object is dying but you can make it live longer. Also note that GC time is not determined and you never know when GC will do it`s work and destroy the objects.

  3. Its not clear what was done, so if you post the code Ill be able to answer this.

Yaroslav Yakovlev