how can i do 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)
 
Jankis,

Things declared as class types will be passed by reference.
Things declared as struct types, or primitives like int, will be passed by
value. If you need these to pass by reference use the keyword ref, e.g.
void fnX(ref int y) { }.

Brendan
 
[Please see my sig - don't reply to me by mail and on the newsgroups at
the same time. It only causes confusion.]

Jankis said:
Thanks your answer bad, standard reference isn't way.

Pardon? I don't understand.
I need reference over next class.

<snip>

Basically you're not going to manage that. Once you've returned from a
method or constructor, any value type is effectively immutable - you
can't change it at a later date from elsewhere.

If you want that kind of behaviour, you'll need some kind of wrapper,
so you pass a reference to the wrapper to the B constructor, and then
you can modify the contents of the object referred to by that reference
at a later date.

To be honest though, that doesn't sound like a very clean way of coding
it - I'm not sure I understand (in human terms) what you're really
trying to do. I suspect you'd be better off using a map of some
description though.
 
Hi.
Thanks your answer bad, standard reference isn't way.
I need reference over next class.

class ABase
{
SetValue(object dest)
{
dest = 10;
}
}

class A : ABase
{
int a;
B b = new B(ref a);

SetValue(b); // in this method i need set property a
// this way set only property mdest in class B
}

class B
{
int mdest;
B(ref int dest)
{
mdest = dest;
}
}

down is complete sample why i need ref over class.


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
// Realy Count is About 100

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;
}
 
Back
Top