views:

114

answers:

3

I'm trying to convert this code over to C# and was wondering what is the equivalent to Javascript's "Array.push"? Here's a few lines of the code i'm converting:

 var macroInit1, macroInit2;
    var macroSteps = new Array();
    var i, step;

    macroInit1 = "Random String";
    macroInit2 = "Random String two";
    macroSteps.push(macroInit1 + "another random string");
    macroSteps.push(macroInit2 + "The last random string");

for (i=0; i<10; i++)
{
   for (step = 0; step < macroSteps.length; step++)
   {
     // Do some stuff 
      }
   }
+7  A: 

You could use a List<string>:

var macroInit1 = "Random String";
var macroInit2 = "Random String two";
var macroSteps = new List<string>();
macroSteps.Add(macroInit1 + "another random string");
macroSteps.Add(macroInit2 + "The last random string");
for (int i = 0; i < 10; i++)
{
    for (int step = 0; step < macroSteps.Count; step++)
    {

    }
}

Of course this code looks extremely ugly in C#. Depending on what manipulations you are performing on those strings you could take advantage of the LINQ features built into C# to convert it into a one-liner and avoid writing all those imperative loops.

This is to say that when converting source code from one language to another it's not a matter of simply searching for the equivalent data type, etc... You could also take advantage of what the target language has to offer.

Darin Dimitrov
+3  A: 

You can replace that either with

  • List<string> macroSteps for a type-safe list-of-string

or

  • ArrayList macroSteps. for a flexible list-of-object
Henk Holterman
and use .Add where you'd use Push()
Rune FS
+1  A: 

It can be much more clean, declarative and nice in C#, for example:

//In .NET both lists and arraus implement IList interface, so we don't care what's behind
//A parameter is just a sequence, again, we just enumerate through
//and don't care if you put array or list or whatever, any enumerable
public static IList<string> GenerateMacroStuff(IEnumerable<string> macroInits) {
{
    return macroInits
                .Select(x => x + "some random string or function returns that") //your re-initialization
                .Select(x => YourDoSomeStuff(x)) //what you had in your foreach
                .ToArray();
}

And it can be used then:

var myInits = new[] {"Some init value", "Some init value 2", "Another Value 3"};
var myMacroStuff = GetMacroStuff(myInits); //here is an array of what we need

BTW, we can suggest you a solution how to "do stuff" properly and nicely if you just describe what you want, not just show us a code we don't have any clue how to use and ask how to translate it literally. Because a literal translation can be so unnatural and ugly in .NET world, and you will have to maintain this ugliness... We don't want you to be in this position :)

Patrol02
+1 for LINQ (although it may be beyond the OP's current knowledge): Remember that you can use `.Select(YourDoSomeStuff)` instead of `.Select(x => YourDoSomeStuff(x))`
Callum Rogers
Yes I remember. But if you are new in LINQ it might be hard to understand that .Select(DoSomeStuff) is actually a method group.
Patrol02