0

I'm fairly knowledgeable with PHP and very new to C#. Essentially what I'm trying to do is convert the value of a string into that actual name of a variable that already exists. An example would be the following:

string[] Bob = { "Bob", "Belcher", "800-123-12345", "13483" };
string[] James = { "James", "Bond", "555-123-6758", "13484" };
string[] Clark = { "Clark", "Kent", "111-222-3333", "13485" };
string input = Console.ReadLine();
// User types in Bob

// Some magic happens and Bob appears in my WriteLine
Console.WriteLine(Bob[3]);

In this example I have three arrays with information about three people including: first name, last name, phone number and employee number. In my application the user enters their first name and the program spits back their employee number. My problem is getting "Bob" that has been typed by the user to produce Bob the variable.

I know in this specific example I could use an If statement or Switch. But I don't want to be doing that if there is a larger quantity of data.

2
  • 1
    Normally you wouldn't use variable names for that. A dictionary that maps employee names to employee information would be more suitable here: Dictionary<string, string[]> - or rather, Dictionary<string, Employee>. Commented Feb 4, 2016 at 9:58
  • you can't create dynamic variable name dear Commented Feb 4, 2016 at 9:58

5 Answers 5

1

I suggest implementing a Person class:

  public class Person {
    private static List<Person> s_Persons = new List<Person>();

    public String FirstName {get; private set;}
    public String LastName {get; private set;}
    public String Phone {get; private set;}
    public int EmployeeNumber {get; private set;}

    public Person(String firstName, String lastName, String phone, int employeeNumber) {
      FirstName = firstName;
      LastName = lastName;
      Phone = phone; 
      EmployeeNumber = employeeNumber;

      s_Persons.Add(this);
    }

    public override String ToString() {
      return String.Join(" ", FirstName, LastName, Phone, EmployeeNumber);
    }

    public static IReadOnlyList<Person> Persons {
      get {
        return s_Persons; 
      }
    }
  }

And then you can use Linq for various queries:

  var Bob = new Person("Bob", "Belcher", "800-123-12345", 13483);
  var James = new Person("James", "Bond", "555-123-6758", 13484);
  var Clark = new Person("Clark", "Kent", "111-222-3333", 13485);

  // Let's print out all the persons with "Bob" first name
  var allBobs = Person.Persons
    .Where(person => person.FirstName = "Bob")
    .OrderBy(person => person.LastName);

  Console.Write(String.Join(Environment.NewLine, allBobs));

  // Let's find Employee number by his/her first name and ensure that there's
  // only one such a person:

 int number = Person.Persons
   .Where(person => person.FirstName = "Bob")
   .Single()
   .Select(person => person.EmployeeNumber);
Sign up to request clarification or add additional context in comments.

Comments

0

The variable variable $$input construct from PHP doesn't work in C#. What you could try in this case is keep your arrays in a dictionary and use the input to look up the entry.

Dictionary<string, string[]> values = new Dictionary<string, string[]>()
{
    { "Bob", new string[] { "Bob", "Belcher", "800-123-12345", "13483" } },
    { "James", new string[] { "James", "Bond", "555-123-6758", "13484" } },
};
string input = Console.ReadLine();
Console.WriteLine(values[input][3]);

Comments

0

Put your arrays in a dictionary:

IDictionary<string,string[]> byName = new Dictionary<string,string[]> {
    {"Bob", Bob}
,   {"James", James}
,   {"Clark", Clark}
};

Now you can do this:

if (byName.ContainsKey(input)) {
    Console.WriteLine(byName[input][3]);
} else {
    Console.WriteLine("Wrong name: {0}", input);
}

Depending on the value of input you'll get the correct array.

Typically, though, you would not make individually named variables to represent individual objects in a collection, and you wouldn't store a person's info in a flat array of strings, using a custom Person class instead.

Comments

0
    string[] Bob = { "Bob", "Belcher", "800-123-12345", "13483" };
    string[] James = { "James", "Bond", "555-123-6758", "13484" };
    string[] Clark = { "Clark", "Kent", "111-222-3333", "13485" };

    // create a collection of arrays
    var list = new List<string[]> { Bob, James, Clark };

    string input = Console.ReadLine();
    // User types in Bob

    // Search in the list. Compare user input to [0] element of each string array in the list.
    var userChoice = list.FirstOrDefault(x => x[0] == input);

    if (userChoice != null)
    {
        Console.WriteLine(userChoice[3]);
    }

Comments

0

Make a list of your string arrays and loop over. like:

List<string[]> list = new List<string[]>();
list.add(Bob);
foreach(string[] s in list)
{
  if(s[0] == userinput)
     Console.WriteLine(Bob[0]);
}

The Code isnt tested just by the way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.