it has few propertyes.. and one of them my be different for same artikl.. i don't how to explain this..
i have another class called Smetka (account) would be the english name.. that class holds list of objects of my first class
something like this
public class Smetka //(account) the english name for smetka..
{//... some code
List
// .. some code
}
public class Artikl
{
public string Name{get;set}
public int key{get;set}
public double amount{get;set}
public double stock{get;set}
}
i have dataBase where my Atril's are storred..
property called amount is not writen in the database.. and its value IS NOT set when I create the object.. i set the value later in the code.. now when i store some artikl.. i retreive the artikl from the database, i set it's amount property, than i check if amount is smaller then stock, then stock -=amount.. and finaly update chenges in the database..
hope you understand the purpose of my application.. now my problem is when i add Artikl in the art property of my smetka class, and if that Artikl already exist in the list, the amound property for all equals artik's changes, and it get equals the last artikl's property amount.. that's problem.. because you may first sell the first artikl, with amount of 3 peaces, than maybe on same account sell the 1 artikl with amount of 1 peacess.. , the program automatickli will change the amount property of all artikl's in the list..
how can i prevent this? how can i prevent amount property from all same objects from changes..
I hope you understand my problem, and i hope you will help me solve this.., sorry for my english..
Jaish MathewsPosted May 18, 2010, 11:47 AM
Aleksandar IlioskiPosted May 18, 2010, 11:19 AM
Jaish MathewsPosted May 18, 2010, 7:31 AM
Step1
Create below extension method in you Form or in side any utility class
public static class ExtensionMethods
{
// Deep clone
public static T DeepClone
{
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, a);
stream.Position = 0;
return (T)formatter.Deserialize(stream);
}
}
}
Below NameSpaces are mandatory
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
Step 2
Make your Article class serializable by adding just ne attribute, as then only it supports deep clone
[Serializable]
public class Article
...............
..................
Step 3
Now you can modify your button click like below which is creatng a deep copy of selected ListItem and you will automatically get fresh object each time.
private void button1_Click(object sender, EventArgs e)
{
Article artForSell = ((Article)listBox1.SelectedItem).DeepClone
artForSell.Amount = double.Parse(textBox1.Text);
ArtsOnAccount.Add(artForSell);
if (ArtsOnAccountChange != null)
{
ArtsOnAccountChange(sender, e);
}
}
Jaish MathewsPosted May 18, 2010, 7:12 AM
Here problem is that each time you are using same Ref. object. But you really need to create new object each time rather than using "SelectedItem" of ListBox. Selected item always same only. So correct it like below by create new object.
private void button1_Click(object sender, EventArgs e)
{
Article artForSell = (Article)listBox1.SelectedItem;
Article newArtForSell = new Article { Key = artForSell.Key, Name = artForSell.Name, Price = artForSell.Price, Stock = artForSell.Stock };
newArtForSell.Amount = double.Parse(textBox1.Text);
ArtsOnAccount.Add(newArtForSell);
if (ArtsOnAccountChange != null)
{
ArtsOnAccountChange(sender, e);
}
}
Aleksandar IlioskiPosted May 18, 2010, 1:59 AM
to see the bug..
run the exe, or build the project, select Milk in ListBox1, than insert 1 for amount, and than press ADD button,
than again, select Milk in ListBox1, for amount insert for example 3, and than press ADD button..
In the ListBox2 shoud be 2 Items:
but instead of that, after adding the second Milk, ListBox2 Contains:
Hope you see the code and help me solve this.. thanks
Jaish MathewsPosted May 18, 2010, 1:11 AM
Aleksandar IlioskiPosted May 17, 2010, 5:08 PM
John WiesePosted May 17, 2010, 4:48 PM