Hi to all, Plz help me
Can we assign the intger value in an object again and again?
using System;
struct MyStruct
{
public int x = 25;
public void SetX(int i)
{ x = i; }
}
class MyClient
{
public static void Main()
{
MyStruct ms = new MyStruct();
ms.SetX(100);
// Store this object somewhere(say arraylist) then again collect this object and
// assign different value.
ms.SetX(102);
}
}
AlanPosted Jun 4, 2007, 9:05 AM
You therefore have to remove the original struct object from the ArrayList and replace it with another object in which the field has been incremented, which is what you've ended up doing.
Bit of a nuisance but one of the small prices we have to pay for having both a unified type system and the performance benefits of value types :)
Rohan MPosted Jun 4, 2007, 4:12 AM
After posting i got idea to replace the object with new one. Where in the newer object the value is incremented!
Deleted old object and inserted the new object at same position.
For timebeing this works..
Please tell me is this a correct solution or else?
THanks a lot,
Rohan MPosted Jun 4, 2007, 3:41 AM
Thanks a lot for the reply.. The actual problem is i want to increment the integer.
Like
I'm storing the objects of this structure in an array _alText. Then while retrieving the object i find unboxing error.
I want to increment rPointer.
private struct StructR
{
public string rText; public ArrayList rArray; public int rPointer;}
((
StructR)_alText[0]).rPointer = ((StructR)_alText[0]).rPointer + 1;// Error for this line.
Also if i add text in rArray then it does not give error and i can retrieve this back.
e.g
((
StructR)_alText[0]).rArray.Add(rParent.Text);Wht is the solution for incrementing ?
AlanPosted Jun 2, 2007, 5:29 PM
You can assign to the integer field x again and again, but there are some things to note:
1) As MyStruct is a struct, you can't give its fields initial values 'in situ'. The following line will therefore give a compiler error:
public int x = 25;
Instead, the field is given the default value for an integer (which is 0) though, if you want to give it some other initial value, you can use the constructor to do this.
2) When you add the struct object contained in the variable 'ms' to an ArrayList, a copy is made and then this copy is 'boxed' on the heap. Consequently, when you recover this object from the ArrayList by 'unboxing' it, it will not be the same object as the one contained in 'ms'. Thus, when you change the latter's 'x' field, it will not affect the 'x' field of the former. You'll, hopefuly, see what I mean by compiling and running this revised version of your program:
using System;
using System.Collections;
struct MyStruct
{
public int x;
public MyStruct(int x)
{
this.x = x;
}
public void SetX(int i)
{ x = i; }
public int GetX()
{ return x;}
}
class MyClient
{
public static void Main()
{
MyStruct ms = new MyStruct(25);
Console.WriteLine(ms.GetX()); // 25
ms.SetX(100);
// Store this object somewhere(say arraylist) then again collect this object and
// assign different value.
ArrayList al = new ArrayList();
al.Add(ms);
ms.SetX(102);
MyStruct ms2 = (MyStruct)al[0];
Console.WriteLine(ms2.GetX()); // 100
Console.WriteLine(ms.GetX()); // 102
Console.ReadLine();
}
}