How to quickly number arrays?

asked 2022-06-24T07:04:53.44+00:00
Stuart Coutts 61 Reputation points

Manually renumbering arrays takes up more time than I want to spend and I'm wondering if there is a better, more efficient way of doing it.

If I am numbering an array...

arrExample(0) = "something"
arrExample(0) = "something else"
arrExample(0) = "another thing"
arrExample(0) = "maybe one more"
arrExample(0) = "nope... this is the last one"

...is there an easy way of telling VS2015... hey, gona please change all the "(0)" to...

arrExample(0) = "something"
arrExample(1) = "something else"
arrExample(2) = "another thing"
arrExample(3) = "maybe one more"
arrExample(4) = "nope... this is the last one"

...and if there is a way to do the same for when one needs to add a new line in the middle, like...

arrExample(0) = "something"
arrExample(0) = "something new"
arrExample(1) = "something else"
arrExample(2) = "another thing"
arrExample(3) = "maybe one more"
arrExample(4) = "nope... this is the last one"

...and then have to correct the numbers to...

arrExample(0) = "something"
arrExample(1) = "something new"
arrExample(2) = "something else"
arrExample(3) = "another thing"
arrExample(4) = "maybe one more"
arrExample(5) = "nope... this is the last one"

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
2,403 questions
{count} votes

1 answer

Sort by: Most helpful
  1. answered 2022-06-24T07:21:06.007+00:00
    Paul Mac 11 Reputation points

    It really depends on how you will be consuming the array at the other end but you could do something like the following:

    string[] arrExample = {
    "something",
    "something new",
    "something else",
    "another thing",
    "maybe one more",
    "nope... this is the last one"
    };

    This removes the need to number the array entries individually.