-1

I want to be able to create and reference a class using input from the user. I'm new to coding, so I'm not quite sure the best way to go around this. If there is a better way of doing this without classes, I am open to suggestions.

class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("What is the name of the Salesmen?");
        string userInput = Console.ReadLine();
        //Create Class Instance
        Salesmen[userInput] = new Salesmen();
        //Access Class Methods or Variables
        [userInput].Age = 35;
    }
}
class Salesmen
{
    public int Age;
    public int Salary;
}
Derrick
  • 33
  • 3
  • Well, it would be not a quite right solution. – SᴇM Apr 10 '18 at 13:05
  • 2
    I'm not sure what you are trying to achieve? My best guess would be you want a `Dictionary` – UnholySheep Apr 10 '18 at 13:05
  • 1
    1) Try building your code. The compiler will tell you that you have numerous errors. 2) Never store age, store date of birth, Age can be calculated and doesn't become "stale". – Dragonthoughts Apr 10 '18 at 13:06
  • @UnholySheep I was about to post the same suggestion. – Zohar Peled Apr 10 '18 at 13:06
  • 1
    @UnholySheep Why use a dictionary? The name is clearly part of the `Salesmen` class, so add a property there instead. Also a dictionary doesn't allow for people with the same name. – DavidG Apr 10 '18 at 13:07
  • Quite frankly though, this question is too broad. @Derrick I suggest you go and run through some basic C# tutorials, they will serve you far better than getting a single answer here. – DavidG Apr 10 '18 at 13:09
  • How would I access Variables and methods inside the class then? If i just create a variable that holds the name, I could have a million different instances of the class called "newSalesmen". How would i differentiate between the different Salesmen? – Derrick Apr 10 '18 at 13:09

2 Answers2

1

Instances of classes are created with thenew keyword, and class members and functions are accessed with a period . (usually read as"dot").

Salesmen salesman = new Salesmen();
salesman.name = userInput;

The first line creates a new Salesmen object (or an instance thereof) and the second sets its name field to whatever the value of userInput is.

Since you haven't declared a field to hold that value, however, we'll need to modify your class so that it can store the name (I've also made the field names lowercase --that's the convention):

class Salesmen
{
    public int age;
    public int salary;
    public string name;
}

It's not clear from your question, but judging from the name you chose for the class, it would seem that you're looking to store more than one salesman object. In that case you'll need a data structure like an array, or a list. We'll rename your class to Salesman (singular), since every object will represent a single salesman and declare a list to hold all salespeople:

List<Salesman> salespeople = new List<Salesman>();
Salesman s = new Salesman();
s.name = userInput;
salespeople.Add(s);
ardila
  • 1,277
  • 1
  • 13
  • 24
0

First - the square brackets you're using, are for indexers, so it's not applicable at this point. Second - to instantiate an object, you need to use a constructor, like this:

Salesmen salesman = new Salesmen();

Where "salesman" (note case) is now the name of your new object. You can then access that object's members like this:

salesman.Name = userInput;

You'll have to add the 'Name' property to your Salesman class, for it to have a name. There are other ways to achieve this, but I think this will get you started.

Get familiar with the concept of classes and objects, in the long run it'll improve your code. Happy coding!

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • but if I just name the object "salesman", all of the objects would be named the same thing. How would i differentiate between the different objects? – Derrick Apr 10 '18 at 13:16
  • The object's name isn't important. If you want to work with a bunch of Salesmen, I suggest using a Guid to identify each one uniquely, or some form of ID number if it is globally unique. – trapsuutjies Apr 10 '18 at 13:24