Author Topic: Reference operator similar usage in C#  (Read 2648 times)

0 Members and 1 Guest are viewing this topic.

Spike Wilbury

  • Guest
Reference operator similar usage in C#
« on: August 11, 2009, 04:28:18 PM »
I know that in C# pointers can be use in unsafe code... i am confused here, this is what I am trying to do (i'm porting some C++ code to C#):

In C++ i can have a function like the one below:

static void angleMinLine (AcGePoint3d p1, AcGePoint3d p2, double& Ang, double& Angmin)
{
... here do something and change the values of Ang and Angmin
}

So, for example i can call the function angleMinLine(); like:

double Ang = 30.0;
double Angmin = 76.0;
angleMinLine(sp, ep, Ang, Angmin);

And the values of Ang and Angmin are modified.

How can i do the same in C# (do i place those variables as a part of the class instead?)

Thanks!
Luis.-

Draftek

  • Guest
Re: Reference operator similar usage in C#
« Reply #1 on: August 11, 2009, 04:44:08 PM »
Use the 'ref' keyword - from the help:

class RefExample
{
    static void Method(ref int i)
    {
        i = 44;
    }
    static void Main()
    {
        int val = 0;
        Method(ref val);
        // val is now 44
    }
}

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Reference operator similar usage in C#
« Reply #2 on: August 11, 2009, 04:48:26 PM »
interesting question - I hadn't thought of doing that.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Reference operator similar usage in C#
« Reply #3 on: August 11, 2009, 05:08:47 PM »
I use ref a lot when I need 2 answers,
if (!Util.Get.getpoint("\nPick the leader start point:", ref P1)) return;
Here getpoint returns a bool describing success  and P1 returns the chosen point

Spike Wilbury

  • Guest
Re: Reference operator similar usage in C#
« Reply #4 on: August 11, 2009, 05:29:14 PM »
Thanks!, i was right in there... 10-4 now

Use the 'ref' keyword - from the help:

class RefExample
{
    static void Method(ref int i)
    {
        i = 44;
    }
    static void Main()
    {
        int val = 0;
        Method(ref val);
        // val is now 44
    }
}

Spike Wilbury

  • Guest
Re: Reference operator similar usage in C#
« Reply #5 on: August 11, 2009, 05:29:53 PM »
I use ref a lot when I need 2 answers,
if (!Util.Get.getpoint("\nPick the leader start point:", ref P1)) return;
Here getpoint returns a bool describing success  and P1 returns the chosen point


that it is ! thanks.

sinc

  • Guest
Re: Reference operator similar usage in C#
« Reply #6 on: August 11, 2009, 08:42:56 PM »
You may also want to use "out".  With "ref", the values should already exist before the method call, while with "out", the method creates new values.  The compiler will support this.

For example, this will cause the compiler to complain about "Use of unassigned variables" for both 'val1' and 'val2':

Code: [Select]
void test(ref T arg1, ref T arg2)
{
}

void myMethod()
{
    T val1, val2;
    // val1 and val2 should be created here to prevent error
    test(ref val1, ref val2);  //  <---- compiler error is for this line
}

On the other hand, this one will warn you that 'arg1' and 'arg2' "must be assigned before control leaves the method".

Code: [Select]
void test(out T arg1, out T arg2)  //  <---- compiler error is for this line
{
    // arg1 and arg2 should be created here to prevent error
}

void myMethod()
{
    T val1, val2;
    test(out val1, out val2);
}

Spike Wilbury

  • Guest
Re: Reference operator similar usage in C#
« Reply #7 on: August 11, 2009, 09:13:40 PM »
Thank you Sinc,

I already did a test like that.

I need to change some existing values inside of functions.


Luis.

Draftek

  • Guest
Re: Reference operator similar usage in C#
« Reply #8 on: August 12, 2009, 08:06:06 AM »
I use 'ref' and 'out' frequently and then have the method return a bool whether it worked or not.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Reference operator similar usage in C#
« Reply #9 on: August 12, 2009, 05:55:04 PM »
I use 'ref' and 'out' frequently and then have the method return a bool whether it worked or not.

ditto ...
and the  'out' keyword in the calling statement is sufficient documentation usually.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.