+ 1

How can I use a string to make an object name?

I would like to get input from a Console.ReadLine put into a string variable. I would use the variable to name the object, and then be able to call s method using a variable.Method() format. Is this possible, and if so how?

14th May 2019, 9:52 PM
Douglas
Douglas - avatar
9 Answers
+ 1
it is possible, 1 name the output of the console.readline eg Name=console.Readline(); 2. use a pointer eg string *p p= &name 3. use it in your object method eg Class *P{ int age; string name; public void sayhi() { console.writeline("hi") } } if you still need any clarification, notify me
14th May 2019, 11:34 PM
John Paul
John Paul - avatar
+ 1
Actually, I was trying to get the computer to generate it's own object, with the name inputted by the user. I just don't know how to make the variable data the object name.
15th May 2019, 12:56 AM
Douglas
Douglas - avatar
+ 1
String username = Console.ReadLine() Cat c1 = new Cat () But I want Cat username = new Cat()
15th May 2019, 1:01 AM
Douglas
Douglas - avatar
+ 1
It was to make an account for each person. It's just a test project, so it doesn't need done. Do you have a better idea on how it should work? I was trying to make a program that would make an account and use a balance variable in the class to adjust the balance of each account. Everything was going well except a way to make the account.
15th May 2019, 1:44 PM
Douglas
Douglas - avatar
+ 1
It's there a idea on how to advice my goal without using objects?
16th May 2019, 9:56 PM
Douglas
Douglas - avatar
0
This is really bad habbit. Object names are solely for the developper. You refer to the object with this name if you could make this variable you will have trouble refering to the object. Cat username = new Cat(); A local variable or function named 'username' is already defined in this scope you already defined a string with the name "username" so now the compiler thinks you want a second object with the same name. How would you memorize the name of the object, when you want to use it later on. Console.WriteLine ( username.Name); What happens when a user create 2 objects and give both objects the same name. https://code.sololearn.com/cK1IrGTYuuL4 Why do you want it ?
15th May 2019, 7:12 AM
sneeze
sneeze - avatar
0
Welcome to the world of OOP A class is a blue print of the object you want to make. The object is a instancee of the class. The good thing of the object is that it is reuseable. You can you it for one person, do what ever you need to do and use it for the next persion. So you do not need to personalize the object. That is what you do in the properties of a object.
16th May 2019, 9:51 PM
sneeze
sneeze - avatar
16th May 2019, 10:03 PM
sneeze
sneeze - avatar
0
The idea is good to do with objects. You just need to learn how to work with objects. It is not impossible.
16th May 2019, 10:16 PM
sneeze
sneeze - avatar