• c# printing object to text box
    12 replies, posted
nooby question, how do you print an object into a text box? textbox.text = objectname.tostring ? I tried this but it says it cannot implicitly convert an object to a string
You can't print an object. You can only fetch one of its property values or action one of its methods to get a value.
Customer person = new Customer(fname_box.Text, sname_box.Text); output_lb.Text = person.ToString; ; Thats what i've got atm
[QUOTE=deamer44;24061916]Customer person = new Customer(fname_box.Text, sname_box.Text); output_lb.Text = person.ToString; ; Thats what i've got atm[/QUOTE] Customer needs to have properties so you can do this output_lb.Text = person.FirstName; output_lb.Text = person.Surname;
Also, ToString[B]()[/B]; It's a method. But it wont work as is, if Customer is your own class then you need to override the ToString method with something that will output what you want.
how can I combine both values from person.firstname and person.surname?
[QUOTE=deamer44;24062712]how can I combine both values from person.firstname and person.surname?[/QUOTE] result = Person.FirstName + Person.Surname; ? [editline]06:04PM[/editline] [code] class Customer { private string p_Firstname; private string p_Surname; public string FirstName { get { return p_Firstname; } set { p_Firstname = value; } } public string Surname { get { return p_Surname; } set { p_Surname = value; } } public string ReturnFullName() { return p_Firstname + " " + p_Surname; } }[/code] [code] Customer person = new Customer(); person.FirstName = "Dave"; person.Surname = "Smith"; label1.Text = person.ReturnFullName();[/code]
[QUOTE=CarlBooth;24062804]result = Person.FirstName + Person.Surname; ? [editline]06:04PM[/editline] [code] class Customer { private string p_Firstname; private string p_Surname; public string FirstName { get { return p_Firstname; } set { p_Firstname = value; } } public string Surname { get { return p_Surname; } set { p_Surname = value; } } public string ReturnFullName() { return p_Firstname + " " + p_Surname; } }[/code] [code] Customer person = new Customer(); person.FirstName = "Dave"; person.Surname = "Smith"; label1.Text = person.ReturnFullName();[/code][/QUOTE] Why are you doing a new method. Just override ToString. And why not do an overloaded constructor. And other stuff like checking to see if FirstName or SurName is null or empty, and initializing the values with dummy values in case none are specified. [code] class Customer { public Customer() { _firstName = "Testy"; _surname = "McTest"; } public Customer(string firstName,string surName) { FirstName = firstName; Surname = surName; } private string _firstName; public string FirstName { get { return _firstName; } set { if (!string.IsNullOrEmpty(value)) _firstName = value; else throw new ArgumentNullException("FirstName cannot be null or empty."); } } private string _surname; public string Surname { get { return _surname; } set { if (!string.IsNullOrEmpty(value)) _surname = value; else throw new ArgumentNullException("SurName cannot be null or empty."); } } public override string ToString() { return string.Format("{0} {1}", FirstName, Surname); } }[/code]
[QUOTE=ShaRose;24068422]Why are you doing a new method. Just override ToString. And why not do an overloaded constructor. And other stuff like checking to see if FirstName or SurName is null or empty, and initializing the values with dummy values in case none are specified.[/QUOTE] I was just trying to keep it simple for him
[QUOTE=CarlBooth;24070656]I was just trying to keep it simple for him[/QUOTE] Then why didn't you use just public string FirstName;? Why the backing store?
What you have there is converting the object itself to a string. Its like somebody giving you a blank white cube and telling you to read it. If you want to get data from person, you need to add a string value to it like firstname or lastname.
[QUOTE=c0baltha1l;24090966]What you have there is converting the object itself to a string. Its like somebody giving you a blank white cube and telling you to read it. If you want to get data from person, you need to add a string value to it like firstname or lastname.[/QUOTE] What?
[QUOTE=ShaRose;24097245]What?[/QUOTE] He's talking about the OP's current implementation. Not yours.
Sorry, you need to Log In to post a reply to this thread.