scott.j.lockhart
scott.j.lockhart

Reputation: 173

Using a string (such as user input) to call information from an array or variable

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.

Upvotes: 0

Views: 172

Answers (5)

hegu_141
hegu_141

Reputation: 64

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.

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186833

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);

Upvotes: 1

Alex Isayenko
Alex Isayenko

Reputation: 679

    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]);
    }

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727047

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.

Upvotes: 0

Mark
Mark

Reputation: 1371

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]);

Upvotes: 0

Related Questions