This is an artsy name for a blog. But I think it sums up the puzzle I faced when I try to do deep copy. We will need a Copy Constructor and a Clone (or Copy) method that is polymorphed (virtual) to make deep copy work under polymorphism situations.
Imagine that: You need a deep copy mechanism, and you have an class hierarchy to do the copy. When you do copies, you may get a reference to one of the high level classes. But obviously you need to copy the entire object. So you need to have:
1. A copy constructor on every class.
2. A polymorphed Clone (or Copy) method on every class.
The Clone method should call the copy constructor and the copy constructor will in turn call its parent's copy constructor so all properties are deep copied.
Tuesday, August 24, 2010
Be careful of MemberwiseClone
If you have noticed, there is a protected method, MemberwiseClone, on Object. It is tempting to use it as it provides a quick way to do shallow copy, which is quite useful sometimes. But I fell into a trap yesterday and finally figured out the root during my walk today. :)
The problem comes from a combination of two factors:
1) MemberwiseClone: I am using it as a cheap way to copy Drag and Dropped content.
2) Parent property: One of the property of all Expression objects that I can copy, is the Parent property that points back to the containing object. This is because of the necessacity to quickly, and reliabilty, find out the parent for certain actions. These actions include Insert an Expression before another, and find out what is the Operator of the Logical expression to display a little "And" or "Or" before the sub expressions.
Long stories short: I drag and dropped a Binary expression "(a+b)" with the "b" being an EmptyExpression. Everything is fine, it seems. But when I drop a second Binary that is the same, problem shows. Because I did a MemberwiseClone on the first Binary, the "b" component of the first is pointing back at the "original" Binary, which is conveniently thrown away at this point. Now when the second Binary is dropped into the "b". "b" went back to the Parent, in this case thrown away, to update its property. Because the thrown away Parent is not show on the UI, it seems nothing is changed. Surprise!
But the worse things comes after: When the old Parent is updating, it dutifully put the "b"'s Parent to nothing, as it is useless now. If I drop another Expression into "b" (which is still shown on the screen), its Parent is empty, and crash happens when it tries to ask its parent to replace itself with the dropped Expression.
This is a twisted explaination. Hopefully I have sometime to draw a Visio to showcase the problem.
The problem comes from a combination of two factors:
1) MemberwiseClone: I am using it as a cheap way to copy Drag and Dropped content.
2) Parent property: One of the property of all Expression objects that I can copy, is the Parent property that points back to the containing object. This is because of the necessacity to quickly, and reliabilty, find out the parent for certain actions. These actions include Insert an Expression before another, and find out what is the Operator of the Logical expression to display a little "And" or "Or" before the sub expressions.
Long stories short: I drag and dropped a Binary expression "(a+b)" with the "b" being an EmptyExpression. Everything is fine, it seems. But when I drop a second Binary that is the same, problem shows. Because I did a MemberwiseClone on the first Binary, the "b" component of the first is pointing back at the "original" Binary, which is conveniently thrown away at this point. Now when the second Binary is dropped into the "b". "b" went back to the Parent, in this case thrown away, to update its property. Because the thrown away Parent is not show on the UI, it seems nothing is changed. Surprise!
But the worse things comes after: When the old Parent is updating, it dutifully put the "b"'s Parent to nothing, as it is useless now. If I drop another Expression into "b" (which is still shown on the screen), its Parent is empty, and crash happens when it tries to ask its parent to replace itself with the dropped Expression.
This is a twisted explaination. Hopefully I have sometime to draw a Visio to showcase the problem.
Monday, August 23, 2010
Fully Qualified Type Name in Silverlight
You can ignore the Culture and PublicKey, and they will be defaulted to "neutral" and "null" respectively. However, you must have the following three components in order for Type.GetType(typename) to return you the correct type (otherwise, you will get a Null/Nothing, which is very frustrating).
1. FullName (Namespace + Class name, etc. Complicated when it is a nested class, or generic class, etc.)
2. Assembly
3. Version
An example is:
"DMS.RuleEditor.ViewModel.Logical, RuleEditorViewModel, Version=1.0.0.0"
1. FullName (Namespace + Class name, etc. Complicated when it is a nested class, or generic class, etc.)
2. Assembly
3. Version
An example is:
"DMS.RuleEditor.ViewModel.Logical, RuleEditorViewModel, Version=1.0.0.0"
Find out position of an UIElement in Silverlight
Use GeneralTransform to get the position of the UI Element:
1 GeneralTransform objGeneralTransform = myObj.TransformToVisual(Application.Current.RootVisual as UIElement);
2 Point point = objGeneralTransform.Transform(new Point(0, 0));
3 double myObjTop = point.Y;
4 double myObjLeft = point.X;
1 GeneralTransform objGeneralTransform = myObj.TransformToVisual(Application.Current.RootVisual as UIElement);
2 Point point = objGeneralTransform.Transform(new Point(0, 0));
3 double myObjTop = point.Y;
4 double myObjLeft = point.X;
Monday, August 2, 2010
Generic.xaml in Silverlight 4
After much pain and suffering, :), I found that the Generic.xaml has to be put under Themes folder for it to work in Silverlight 4. It makes sense... but the postings on the internet are not helping. Some say it must be under the root folder, and others only addressing WPF. Am I the only person trying to do a custom control in SL4?
Friday, June 25, 2010
Mysterious connection trouble on Intel WiFi Link 1000 BGN
I experienced a mysterious connection trouble on my Windows 7 laptop in the last two days. The symptom is a very slow and unstable connection. Since I had encountered similar problems on other computers before, I assumed it is a spyware infection. This specific machine has the Microsoft Essentials anti-virus that I am not trusting much. However, the frustration continues as I just can not connect to internet or home network and download anything large. At the end, I resorted to downloading to another home computer and used wired connection on this machine to get the file running. It took some effort (probably because I had WiFi and Wire on at the same time) but finally finished.
After I run Spybot S&D: Surprise! I have zero problems!
Not even a tracking cookie? At this point, I started wondering if my previous assumption was all wrong. I tried "ping -t" to my local router and found the connection is not stable at all, at it drops the ping every once a while. Then I suddenly realized, upon looking at the connection properties, that I changed some settings on the wireless card for my new Wireless-N network. And surely, it turns out that the "Throughput Enhancement" in Advanced was turned to "Enabled". After I set it back to "Disabled", the connection is back to normal.
This is a lesson that I have unlearned multiple times. And I am sure I will unlearn it again in the future: Always jog down the changes that you are making, especially if you don't know what it really means...
After I run Spybot S&D: Surprise! I have zero problems!
Not even a tracking cookie? At this point, I started wondering if my previous assumption was all wrong. I tried "ping -t" to my local router and found the connection is not stable at all, at it drops the ping every once a while. Then I suddenly realized, upon looking at the connection properties, that I changed some settings on the wireless card for my new Wireless-N network. And surely, it turns out that the "Throughput Enhancement" in Advanced was turned to "Enabled". After I set it back to "Disabled", the connection is back to normal.
This is a lesson that I have unlearned multiple times. And I am sure I will unlearn it again in the future: Always jog down the changes that you are making, especially if you don't know what it really means...
Labels:
connection,
Intel WiFi card,
spyware,
unstable connection
Thursday, April 22, 2010
"Only models that include foreign key information are supported." error in Entity Framework 4
This is an error I encountered in EF4 within Visual Studio 2010, when I use the "Model First" approach and created a database from a theoretical model. Ironically it will not compile after that because of the error. Microsoft still has a lot of work to do here.
Solution:
1. Create the property within the entity for the FK using the same name.
2. In mapping details, map the new property to the underlying table field.
3. Select the original association and click the Referential Constraint and choose the right principal and dependent.
4. Now select the association again, you will see a message akin to "Mapping not allowed for exposed foreign key... Delete the mapping...".
5. Click on Delete the mapping link.
6. Recompile.
Viola! World peace...
Solution:
1. Create the property within the entity for the FK using the same name.
2. In mapping details, map the new property to the underlying table field.
3. Select the original association and click the Referential Constraint and choose the right principal and dependent.
4. Now select the association again, you will see a message akin to "Mapping not allowed for exposed foreign key... Delete the mapping...".
5. Click on Delete the mapping link.
6. Recompile.
Viola! World peace...
Subscribe to:
Posts (Atom)