Pujo,
Code:
[COLOR=blue]public class[/color] MainProj
{
[COLOR=blue]public static void[/color] Main(){
aku ad = [COLOR=blue]new[/color] aku(5);
ArrayList intArr = [COLOR=blue]new[/color] ArrayList();
intArr.Add(ad);
[COLOR=green] // The line below fails because intArr[0]
// returns an object. In order to use as
// an aku object you must first cast it
// into one.[/color]
[b]intArr[0].NilaiA = 4;[/b][COLOR=green] //Wrong code[/color]
[COLOR=green] // The correct way is this:[/color]
aku ad2 = (aku)intArr[0];
ad2.NilaiA = 4;
}
}
In the code above, the line in bold is wrong. The ArrayList class returns an object so you must convert it into an aku if you want to use it.
Now, in order for you to use the code I gave in my other post, you must implement the ICloneable interface in your aku class. Implementing this interface is very simple because it only has one method, Clone. Take a look at this code:
Code:
[COLOR=blue]public class[/color] aku [b]: ICloneable[/b]
{
[COLOR=blue]private int[/color] a;
[COLOR=blue]public[/color] aku(int d){
a = d;
}
[COLOR=blue]public int[/color] NilaiA{
[COLOR=blue]get[/color]{
[COLOR=blue]return[/color] a;
}
[COLOR=blue]set[/color]{
a = [COLOR=blue]value[/color];
}
}
[COLOR=green] // Implementation of ICloneable[/color]
[COLOR=blue]public object[/color] Clone()
{
[COLOR=blue]return this[/color].MemberwiseClone();
}
}
There are two things you must keep in mind: (1)The Clone method returns an object, so before you do anything with it, you must cast it into the specific type - in this case aku; (2)The implementation of the ICloneable interface that I wrote above is the most simple one, which creates a
shallow copy of the object being cloned. That means that if your object (the one you're cloning) has references to other objects, you must clone them maually in the Clone method and assign them to the object your cloning. For example, if your aku class has a property called BKU, which returns a bku object, this property will not be cloned by the Clone method above. In order to clone it properly,
you must implement the ICloneable interface in the bku class and clone the BKU property (which returns a bku object) in the Clone method of the aku class. Take a look at the example below:
(It is assumed that the bku class is defined somewhere else and that it implements the ICloneable interface).
Code:
[COLOR=blue]public class[/color] aku [b]: ICloneable[/b]
{
[COLOR=blue]private int[/color] a;
[b][COLOR=blue]private[/color] bku b;[/b]
[COLOR=blue]public[/color] aku(int d){
a = d;
}
[COLOR=blue]public int[/color] NilaiA{
[COLOR=blue]get[/color]{
[COLOR=blue]return[/color] a;
}
[COLOR=blue]set[/color]{
a = [COLOR=blue]value[/color];
}
}
[b][COLOR=blue]public[/color] bku BKU{
[COLOR=blue]get[/color]{
[COLOR=blue]return[/color] b;
}
[COLOR=blue]set[/color]{
b = [COLOR=blue]value[/color];
}
}[/b]
[COLOR=green] // Implementation of ICloneable[/color]
[COLOR=blue]public object[/color] Clone()
{
aku a = [COLOR=blue]this[/color].MemberwiseClone();
[COLOR=green] // Clone the BKU property[/color]
[b]a.BKU = this.BKU.Clone();[/b]
[COLOR=green] // Return the fully-cloned aku[/color]
[COLOR=blue] return[/color] a;
}
}
I hope all this makes sense to you!
JC