• Welcome to Valhalla Legends Archive.
 

[c#]passing by reference

Started by mentalCo., December 09, 2004, 12:34 PM

Previous topic - Next topic

mentalCo.

ok heres my problem...

In c++ you can do something like this...


void something(char *crap){
memcpy(crap, "new value", 9);
}


void main(){
char MyValue[100];
something(MyValue);
cout<<MyValue;
}



'MyValue' would be printed as 'new value'

how do you do this in c#.

[MyndFyre edit: changed the subject]

MyndFyre

That's somewhat odd code for C#; you wouldn't copy string data.  You wouldn't want to anyway; .NET maintains a string table.

But, you can pass value-types by reference:


void something(ref int num) {
  num *= num; // squares it.
}

void Main(string[] args) {
  num = 5;
  something(ref num);
  Console.WriteLine(num); // outputs 25.
}
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.