how can i do it in C# safe code.

  • Thread starter Thread starter Jankis
  • Start date Start date
J

Jankis

I have problem how can i do it in C# safe code.

class ABase
{
SetValueFromXX(string name,source,dest) // in C++ ( string name,source,
int *dest)
{
1. step
in source i find row with name

2. step
set value from source on finded row to dest, // in c++ *dest =
source.value
How can i do in safe c# - i need put value to property in class A
???
}
}

class A : ABase
{
int aa;
int ab;
int ac;

// and many next property

public int Count = 3;
B b=new B(Count);
b[0] = new B( "power" , here need reference to aa); // in C++ &aa;
b[1] = new B( "volt" , here need reference to ab); // &ab
b[2] = new B( "aper", here need reference to ac); // &ac

ReadData()
{
1. step
read Data from something .... to source object

2. step
set data from source to property over base class ABase

for(i=0;i<Count;i++)
SetValueFromXX(b.mName, source ,b.mS_A_R);
// this don't work and it is problem in c#. When i use ref
i can put data only in class B
// i need put data to property aa,ab,ac in this class.
}
}

class B
{
B(string name, ??? S_A_R) // in c++ (string name,int *refer);
{
mName = name;
mS_A_R = S_A_R; // in c++ mS_A_R = refer;
}
public string mName;
public something about reference ??? mS_A_R; // in c++ int
*mS_A_R;
}

Can you help me ???
thanks Jankis.
email : (e-mail address removed)
 
If you really need to treat a value type as a reference type, you
could create your own:

class RefInt
{
public int myInt;
}

Then, instead of declaring aa, ab, etc as int's, declare them as
RefInt's. You can then pass the RefInt object and any change to the
myInt value would be reflected in the original (since you are now
passing the object by reference instead of by value).

An alternative would be to set the values of aa, ab, etc after the
call to SetValueFromXX is made:

b[0] = new B("power",aa);
SetValueFromXX(b[0]);
aa = b[0].value; // where value is an int property of B, replacing
'SAR'
 
Actually, the way it's described, it wouldn't be sufficient. Consider:

public class MyClass1
{
public int myInt;
}

public class MyClass2
{
private int myInt2;

private void ChangeValue(ref int intValue)
{
intValue = 0; // or any new value
}

private void DoWork()
{
MyClass1 myClass1 = new MyClass1();
myClass1.myInt = myInt2;
ChangeValue(ref myClass1.myInt); // does not change myInt2
}
}

This is essentially the way he described the problem. To fix it, you
could either store myInt2 as an instance of MyClass1 (essentially
keeping a reference int) or add another line after ChangeValue(...)
that would set myInt2 to the new value of myClass1.myInt.
 
this is good idea bad, i need call method SetValueFromXX in the for(){ }

for(i=0;i<Count;i++)
{
SetValueFromXX( b );
switch(i)
{
case 0: aa = b.Value;break;
case 1:ab = b.Value;break;
case 2:ac = b.Value;break;
}
}
it is no so good - i have many many property in aa,ab,ac,ad .....,atc
Count is about 100.

thanks Jankis


bjs10 said:
If you really need to treat a value type as a reference type, you
could create your own:

class RefInt
{
public int myInt;
}

Then, instead of declaring aa, ab, etc as int's, declare them as
RefInt's. You can then pass the RefInt object and any change to the
myInt value would be reflected in the original (since you are now
passing the object by reference instead of by value).

An alternative would be to set the values of aa, ab, etc after the
call to SetValueFromXX is made:

b[0] = new B("power",aa);
SetValueFromXX(b[0]);
aa = b[0].value; // where value is an int property of B, replacing
'SAR'

"Jankis" <[email protected]> wrote in message
I have problem how can i do it in C# safe code.

class ABase
{
SetValueFromXX(string name,source,dest) // in C++ ( string name,source,
int *dest)
{
1. step
in source i find row with name

2. step
set value from source on finded row to dest, // in c++ *dest =
source.value
How can i do in safe c# - i need put value to property in class A
???
}
}

class A : ABase
{
int aa;
int ab;
int ac;

// and many next property

public int Count = 3;
B b=new B(Count);
b[0] = new B( "power" , here need reference to aa); // in C++ &aa;
b[1] = new B( "volt" , here need reference to ab); // &ab
b[2] = new B( "aper", here need reference to ac); // &ac

ReadData()
{
1. step
read Data from something .... to source object

2. step
set data from source to property over base class ABase

for(i=0;i<Count;i++)
SetValueFromXX(b.mName, source ,b.mS_A_R);
// this don't work and it is problem in c#. When i use ref
i can put data only in class B
// i need put data to property aa,ab,ac in this class.
}
}

class B
{
B(string name, ??? S_A_R) // in c++ (string name,int *refer);
{
mName = name;
mS_A_R = S_A_R; // in c++ mS_A_R = refer;
}
public string mName;
public something about reference ??? mS_A_R; // in c++ int
*mS_A_R;
}

Can you help me ???
thanks Jankis.
email : (e-mail address removed)
 
Back
Top