• Jake Vinson (unregistered)

    Well, whenever I have to check if a variable equals 8, I call the variable that I'm checking against D.

    <pre>If 8==D Then...</pre>

  • Jake Vinson (unregistered)

    Boy, given that I can't use HTML in these comments, I sure look like a boner.

  • Mike Dunn (unregistered)

    An old friend of mine used "gac" for his loop counters. "gac", of course, stood for General All-purpose Counter.

  • Steve Tibbett (unregistered)

    Nice thing about this code is that you get an initialized array of 8 empty strings.. saying string[8] array; gets you 8 null strings. Different thing.

    You could always add a Debug.Assert(args.Length == 8); line to make it obvious that you're expecting the size to be 8.. :)

  • Steve Maine (unregistered)

    I don't think there's any guarantee as to how many strings the tmpEntry array will contain. It will be at least 9 -- one for the original contents of "entry", plus the 8 created by the commas.

    It could conceivably be many more, if the "entry" variable itself contains commas.

  • Jerry Pisk (unregistered)

    Saying string[] array = { "", "", ""...} will get you an array of empty strings, without the overhead of splitting a string.

  • Larry Osterman (unregistered)

    One possible alternative: "entry" contains a csv string that has up to 8 elements.

    By concatenating ",,,,,,,," to "entry", you guarantee that args.Length is at least 8.

    So IMHO, this is actually a clever, if inefficient way of parsing a variable length CSV input.

  • Centaur (unregistered)

    Larry Osterman,

    Yes, but whoever uses this “art” is clearly asking for a test case with a comma delimiting fields, another test case with a comma /within/ a quoted field, and a third test case with both. Something tells me it’s gonna fail some of them.

    foo,42
    "bar,baz"
    quux,"corge,xyzzy"

  • Centaur (unregistered)

    Oh, and by the way, Microsoft Excel, when instructed to save a worksheet to a CSV (comma-separated values) file, instead creates, so to say, a LOCALE_SLIST-separated values file. Which makes reliable CSV processing a pain because a file might have come from another machine, or be created by another user, whose list separator is different from ours.

  • Larry Osterman (unregistered)

    You're right Centaur. The code above's no way to parse CSV. But maybe the person writing the code knew something about his data? It's no excuse for a lack of robustness, but...

    But the code as posted IS potentially a legitimate solution to a valid problem. It's not just a stupid way of getting an array of 8 empty strings as was postulated in the original article.

  • Christian Romney (unregistered)

    private string AddToListArray()
    {
    return new String();
    }
    private string[] ArrayListToStringArray()
    {
    ArrayList ListArray = new ArrayList();
    for (int i = -1 + 1; i < 2 * 2 * 2; i++)
    {
    ListArray.Add(AddToListArray());
    }
    return ArrayListToStringArray.ToArray(string.Empty.GetType()) as string[];
    }
    private void foo()
    {
    string[] StringArrayFromArrayList = ArrayListToStringArray();
    }

    // Artful.

  • Christian Romney (unregistered)

    Damn. Got caught up in my own stupidity.

    private string AddToListArray()
    {
    return new String();
    }
    private string[] ArrayListToStringArray()
    {
    ArrayList ListArray = new ArrayList();
    for (int i = -1 + 1; i < 2 * 2 * 2; i++)
    {
    ListArray.Add(AddToListArray());
    }
    return ListArray.ToArray(string.Empty.GetType()) as string[];
    }
    private void foo()
    {
    string[] StringArrayFromArrayList = ArrayListToStringArray();
    }

    Hey maybe MSDN can add an Obfuscated C# Contest like they do (did?) in the C++ Users Journal!

  • Jason (unregistered)

    How about a Rube Goldberg C# contest instead. "Build a program that does something simple in the most steps without repeating a step."

  • Jason Olson (unregistered)

    I've actually seen code like this in our codebase. If you have your own report definition language for a report-driven application, this kind of code snippet can actually be useful. For instance, in your RDL a report element can have 5 parameters, you might want the actual report element definition to define only the parameters used.

    For instance, the element might only use the first 2 parameters, or no parameters at all. In that case, the code will "default" all missing parameters. This way, you don't have code like "if the user only specified 2 parameters, then do this, if there was just 1 parameter, do this, etc.". Clever when it is applied correctly. (And no, I'm not the author of the code in our code base). The surprising part to me is that in the context of a report definition language, the code made perfect sense and is actually quite understandable and maintainable.

  • Jason Olson (unregistered)

    I should add that the codebase containing this report code is a legacy codebase and is written in classic ASP.

  • Hassan Voyeau (unregistered)

    The first few comments that ridiculed this piece of code was unwarranted as the code was taken out of context. I would need more information before I criticise this code.

  • m (unregistered)

    Jason, why not use a standard recursive-descent parsing tree (bison, flex-style?)

  • AKG (unregistered) in reply to Jake Vinson

    ?? ??? ?? ???!

  • John Doe (unregistered)

    I've regularly seen

    SomeFunction(someArray)

    dim strArray
    for loop to concatenate each item in the array with a , into strArray
    strArray = strArray.Split(",")
    
    for loop to do something with strArray
    

    'no return because that would make too much sense. End Function

    mind you this is from people who have mid-senior titles...

Leave a comment on “Array Initializer”

Log In or post as a guest

Replying to comment #:

« Return to Article