2

I'm sorry for the vague title, I think you really have to see the snippet to know what I mean:

float[] foo = new float[3]; FillFoos(foo); return foo; 

I'd like to have that in one line (I use this snippet very often). How would that be possible?

5
  • Note: Of course, I can't edit FillFoos to simply return a float[]. Commented Jan 4, 2011 at 20:18
  • Why you can't? just return updated input values. Commented Jan 4, 2011 at 20:20
  • And your reason for a one liner is ? Commented Jan 4, 2011 at 20:21
  • It's a wrapper over C++ DLL code. I have access to FillFoos only, I can't edit it. My reason for a one liner is that I have this code structure over 100 times in my code. Commented Jan 4, 2011 at 20:24
  • 1
    if you haven't already, check out this stack-exchange proposal. I think you'll find it useful. Commented Jan 14, 2011 at 7:38

5 Answers 5

3

If you can't alter the FillFoos function, then your snippet is as short as it can be.

You could, of course, do this:

float[] foo = new float[3]; FillFoos(foo); return foo; 

But that's still three statements and is fairly tough to read.

Sign up to request clarification or add additional context in comments.

Comments

3

You could just create a function:

public float[] GetFoos() { float[] foo = new float[3]; FillFoos(foo); return foo; } 

EDIT: If you need to change the size of the array and the method to populate the array then you could do this:

public float[] GetFoos(int count, Action<float[]> populateAction) { float[] items = (float[])Array.CreateInstance(typeof(float), count); populateAction(items); return items; } 

then you can call it like this:

float[] items = GetFoos(3, FillFoos); 

You can even make it generic:

public T[] GetFoos<T>(int count, Action<T[]> populateAction) { T[] items = (T[])Array.CreateInstance(typeof(T), count); populateAction(items); return items; } 

3 Comments

It's not always foo, not always 3 floats, and not always FillFoos. It's different functions that fill float arrays.
You can make the method Generic, and accept the array length as an argument.
And add a delegate for FillFoos' replacement? This can't work, FillFoos is not always 1 parameter long, sadly. :/
2

In C#, you could make a generic function that allocates an array and uses a supplied delegate to fill it:

 public static T[] AllocAndFill<T>(Action<T[]> fillAction, int count) { T[] array = new T[count]; fillAction(array); return array; } 

And use it like this do:

 var result = AllocAndFill<float>(FillFoos,3); 

3 Comments

That's the most logical solution, unfortunately it won't work in my context, as Action has many more possible signatures. Accepted nonetheless.
Well, you can make function overloads that allow you to call fill functions with extra parameters, or use lambda syntax to write an action inline. I can elaborate if you're interested...
No need to elaborate, I understand. Thanks.
0

Wrap it in another method.

T[] GetObjects<T>(int length) { T[] foo = new T[length]; FillFoos(foo); return foo; } 

Now, instead of using that snippet everywhere, just call GetObjects<Foo>(3).

Comments

0

If you can't change FillFoos, then you could write some kind of helper method (perhaps as an extension method on whatever object contains FillFoos).

public static class Extensions { public static float[] SuperFoo(this FooObject foo, float[] floats) { foo.FillFoos(floats); return floats; } } 

Then:

return fooObj.SuperFoo(new float[3]); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.