2

I need to make this:

 string[][] anordinaryname = new string[][]
{
    new string[] {""},
    new string[] {""},
    new string[] {""},
    new string[] {""},
};

This:

 string[][] 9HM.A2 = new string[][]
{
    new string[] {""},
    new string[] {""},
    new string[] {""},
    new string[] {""},
};

i need it to be formatted exactly like this, but the 9 at the start and the . in the middle are causing issues. any fix?

The reasoning for this as i am making a timetable organisation for my school. this is a period, so i need the user to put into an input field "9HM.A2", and then go to this array to get information, as this is how it is shown on the timetables we are given. It is for a school project.

  • 4
    Welcome to Stack Overflow. No, `9HM.A2` is simply not a valid variable name. (The array aspect is irrelevant to that.) You say you *need* that variable name - could you edit your question to explain the motivation here? That requirement is infeasible - but if you could give more information about what's *causing* that requirement, we may be able to suggest alternatives. – Jon Skeet Apr 04 '19 at 08:57
  • 1
    Feel their is an X-Y problem here: WHY do you want it exactly as "9HM.A2"? – Meirion Hughes Apr 04 '19 at 09:02
  • I need to be able to get to it with that as the user will input 9HM.A2, i will then need to get to this array. so unless i make it "nineHMdotA2 (the only real alternative currently), i need to do this. i just need to know if this is possible – Lucas Farrugia Apr 04 '19 at 09:03
  • Possible duplicate of https://stackoverflow.com/questions/25217167/c-sharp-variable-names-with-special-character – trollingchar Apr 04 '19 at 09:04
  • Possible duplicate of [C# variable names with special character](https://stackoverflow.com/questions/25217167/c-sharp-variable-names-with-special-character) – derHugo Apr 04 '19 at 09:40

2 Answers2

4

No, you don't need your variables to be named like that. Variable names exist for the developer, not for the end user.

If you need to look up data based on a string, use a dictionary:

var timeTable = new Dictionary<string, string[][]>
{
    { 
        "9HM.A2", new string[][]
        {
            new string[] {""},
            new string[] {""},
            new string[] {""},
            new string[] {""},
        }
    },
    { 
        "8XX.Z3", new string[][]
        {
            new string[] {""},
            new string[] {""},
            new string[] {""},
            new string[] {""},
        }
    },

}

Then you can obtain the value by string:

if (timeTable.TryGetValue("9HM.A2", out var userTimeTable))
{
    // use userTimeTable
}

And you should reconsider whether a jagged array is the appropriate data structure for your needs; you'd perhaps rather create a class to hold that data.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 1
    upvote, i think this is the most sane solution. (also i have run out of points today, so i might as well run out of upvotes as well) – TheGeneral Apr 04 '19 at 09:14
2

You simply cannot have this variable name, full stop, end of story. Let's visit the documentation:

Identifier names

An identifier is the name you assign to a type (class, interface, struct, delegate, or enum), member, variable, or namespace. Valid identifiers must follow these rules:

  • Identifiers must start with a letter, or _.
  • Identifiers may contain Unicode letter characters, decimal digit characters, Unicode connecting characters, Unicode combining characters, or Unicode formatting characters.

As you can see, full-stop/period is not on the list, in turn making your variable name 9HM.A2 invalid.

Rename 9HM.A2 to _9HM_A2 or _9HMA2 (for fun level 100).

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • 1
    That edit fixed that problem, have an upvote. However, now the OP's next question will be: _"From a string input "xyz", how do I get the contents of a variable named "xyz"?"_ – CodeCaster Apr 04 '19 at 09:18