0

C# create class with consol input

How to create a class object name (class name=new class) with consol. ReadLine(). I want to type a name on consol as new class and create a object with the typed name using System; using System.IO; using System.Linq; using System.Collections.Generic; namespace namess { public class ain { class person {public string name; public int hight; public person(String personName) {Console.WriteLine("class created"); name=personName; } } static void Main(string[] args) { //enter name for an object Console.Write("type obj name here :"); string objName=Console.ReadLine(); //enter person name Console.Write("type name of person: "); string personName=Console.ReadLine(); //create object person objName=new person(personName); } } }

11th Apr 2022, 11:51 AM
Nilam Rajap
7 Réponses
0
class Car { string color = "red"; static void Main(string[] args) { Car myObj = new Car(); string clr = Console.ReadLine(); Console.WriteLine(String.IsNullOrWhiteSpace(clr)?myObj.color:clr); } } https://www.w3schools.com/cs/cs_classes.php Good Luck
11th Apr 2022, 12:04 PM
SoloProg
SoloProg - avatar
0
SoloProg No I want to change the object (myObj) in to a string. So I can type in consol and create new one
11th Apr 2022, 12:40 PM
Nilam Rajap
0
// White Color .. meant other code Oo" class Car { string color=""; static void Main(string[] args) { Car myObj = new Car(); myObj.color = Console.ReadLine(); Console.WriteLine(String.IsNullOrWhiteSpace(myObj.color)?"white":myObj.color); } }
11th Apr 2022, 12:49 PM
SoloProg
SoloProg - avatar
0
SoloProg I updated my question with my code. Check my first Console.ReadLine (//enter name for an object), this I need make
11th Apr 2022, 12:55 PM
Nilam Rajap
0
Nilam Rajap , You have 2 variables with the same name objName How many classes do you have? Why do you want the user to enter the type object name?
11th Apr 2022, 4:39 PM
SoloProg
SoloProg - avatar
0
SoloProg Just to make a simple result sheet Enter user name as class object And result as object member
11th Apr 2022, 4:45 PM
Nilam Rajap
0
Nilam Rajap , I didn't really understand what you meant, and you didn't answer my questions. I hope that the others can solve your problem.
11th Apr 2022, 5:01 PM
SoloProg
SoloProg - avatar