1

I have a method that has 2 ref parameters:

public void ReplaceSomething(ref int code, ref string name)
{
    ...
}

I want to avoid this, as it is not a good design (and scales poorly). What are my options?

I've though about using an anonymous object, but that doesn't seem like a good idea, either.

Object something = new { code = 1, name = "test" };

ReplaceSomething(something);
Otuyh
  • 2,384
  • 5
  • 22
  • 40

6 Answers6

7

Are the code and the name closely linked together? If so, consider creating a type to put the two of them together. Then you can return a value of that type.

Alternatively, you might consider returning a Tuple<int, string>.

(In both cases you can accept an input parameter of the same type, of course. As you haven't shown any of your code, it's not really clear whether you use the existing values of the parameters, or whether they could basically be out parameters.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Author does not say which version of .NET he's using, which of course matters for the Tuple solution. You knew that, but others may not. – Andrew Feb 27 '12 at 18:50
  • @Andrew, but it's trivial to create your own `Tuple` on older versions of the framework. – svick Feb 27 '12 at 18:53
  • Excelent tip this Tuple! Thanks a lot! I'll read about it right now! – Otuyh Feb 27 '12 at 18:58
0

If you need to return these values, you can either use a tuple

public Tuple<int, string> ReplaceSomething(int code, string name)
{
    ...
}

Or create your own class-wrapper that holds the values as properties

public Foo ReplaceSomething(int code, string name)
{
    var foo = new Foo(){...};
    return foo;
}

class Foo
{
    public int IntValue{get;set;}
    public string StringValue{get;set;}
}
oleksii
  • 35,458
  • 16
  • 93
  • 163
  • I think the author's point, though, was that the values do in fact need to be returned somehow. – Andrew Feb 27 '12 at 18:48
  • But i need to return the values when i change them, thats why im passing by reference. Some other way to do that without create a global variable or passing by reference? – Otuyh Feb 27 '12 at 18:51
0

Based on your question, I could be way off. What do you mean by replacing ref? Are you looking to overload?

public void ReplaceSomething(int code, string name)
{
    // ...
}

public void ReplaceSomething()
{
    return ReplaceSomething(1, "test");
}

Edit:

ok, so you need to return the code and the name what are the calculations that need to be made? Jon Skeet's answer about a tuple could be right, or you might need a POCO that contains the code the name and the replaced

public void ReplaceSomething(int code, string name)
{
    var replaced = new Replaced();
    replaced.code = code;
    replaced.name = name;

    var r;
    // do some replacement calculations
    replaced.replaced = r;

    return replaced;

}

public class Replaced {
    public string name {get; set;}
    public int code {get; set;}
    public string replaced {get; set;}
}
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
0

Why don't you want to use ref arguments? That seems like a perfectly good way to change some caller values.

The other approach would be to implement a return value. Maybe you need to better explain what the problem is?

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • I just saw here that is not a good way to do that. http://stackoverflow.com/questions/3539252/when-is-using-the-c-sharp-ref-keyword-ever-a-good-idea – Otuyh Feb 27 '12 at 18:50
  • @Hor: I see. His main point is that he doesn't seem to like the ambiguity of `ref`, instead preferring `out`, which requires that the parameters are *always* updated by the method. I think `ref` is fine, but if you are, in fact, always updating the parameter values, then use `out` instead. – Jonathan Wood Feb 27 '12 at 18:53
0

If these values are tightly coupled and "belong together" you could define a custom class that holds your properties and either return a new instance (assuming its immutable) of that or update its properties:

class Code
{
  public int Value {get;set;}
  public string Name {get;set;}
}

public Code UpdateCode(Code code)
{
    ...
}
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
0

Why would you change it? ref parameters make sense at times, and if this is one of those times - use them. You could introduce a new class that contains that pair of values, which only makes sense if those values come together often.

I say, keep it.

zmbq
  • 38,013
  • 14
  • 101
  • 171