Connecting Tech Pros Worldwide Forums | Help | Site Map

Ref paramater made ugly

Brad Wood
Guest
 
Posts: n/a
#1: Nov 16 '05
I have a method that takes a ref string parameter. This is because it
will be called in loops and most of the time it will not need to modify
the paramater at all, so I preferred not to return a newly allocated
string object.

I need to pass items of an array list (can't use specialized collection
in compact framework) to this method. I can't pass the array item
directly because I get a compiler error (a ref or out argument must be
an lvalue). So this is what I'm doing:
string temp = (string)array[i];
obj.refMethod ( ref temp );
array[i] = temp;

I didn't find a performance problem with this, but is there a better way
to write this?

David Browne
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Ref paramater made ugly



"Brad Wood" <bradley_.wood_@ndsu_.nodak_.edu> wrote in message
news:%23ijuQY%232EHA.3596@TK2MSFTNGP12.phx.gbl...[color=blue]
>I have a method that takes a ref string parameter. This is because it will
>be called in loops and most of the time it will not need to modify the
>paramater at all, so I preferred not to return a newly allocated string
>object.
>
> I need to pass items of an array list (can't use specialized collection in
> compact framework) to this method. I can't pass the array item directly
> because I get a compiler error (a ref or out argument must be an lvalue).
> So this is what I'm doing:
> string temp = (string)array[i];
> obj.refMethod ( ref temp );
> array[i] = temp;
>
> I didn't find a performance problem with this, but is there a better way
> to write this?[/color]

No. ArrayList only looks like an array because of C# indexers.

string temp = (string)arrayList[i];

is just syntactic sugar for

string temp = (string)ArrayList::get_Item(i);

and
arrayList[i] = temp
is
ArrayList::set_Item(i, temp);

There's no better way to write this.

Even if the C# compiler somehow allowed you to write

obj.refMethod ( ref arrayList[i]);

It would still have to call get_Item/set_Item, and would also need a
temporary variable to hold the result.

David


William Stacey [MVP]
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Ref paramater made ugly


Not sure its better. But you could propably get rid of the ref method with
something like:

string temp = (string)array[i];
string result = obj.RefMethod( temp ); // Returns null if no change needed.
if ( result != null )
array[i] = result;

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Brad Wood" <bradley_.wood_@ndsu_.nodak_.edu> wrote in message
news:#ijuQY#2EHA.3596@TK2MSFTNGP12.phx.gbl...[color=blue]
> I have a method that takes a ref string parameter. This is because it
> will be called in loops and most of the time it will not need to modify
> the paramater at all, so I preferred not to return a newly allocated
> string object.
>
> I need to pass items of an array list (can't use specialized collection
> in compact framework) to this method. I can't pass the array item
> directly because I get a compiler error (a ref or out argument must be
> an lvalue). So this is what I'm doing:
> string temp = (string)array[i];
> obj.refMethod ( ref temp );
> array[i] = temp;
>
> I didn't find a performance problem with this, but is there a better way
> to write this?[/color]

Closed Thread