Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace structures
{
    struct nav
    {
        string name;
        int age;
        float sal;

        public nav(string s,int i,float sl)
        {
            name=s;
            age=i;
            sal=sl;

        }
        public void showdata()
        {
        Console.WriteLine("Name:"+name);
        Console.WriteLine("Age:"+age);
        Console.WriteLine("Sal:"+sal);
        }


    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            nav nv=new nav("David Billa";,26,9000);
            nv.showdata();
        }
    }
}



what i meant to ask here is now i cannot use show data functionality to display the values of the NV object in a text box cause they are combo values i.e. string,int...etc. how to make them in display!


What i wanted to achieve here is the values that can be displayed when nv.showdata() is used...i want those values to be displayed in a text box,label,msg box etc!


i know that the following code will display the output of the program in a console(dos). what is my question here is what should me done to get the output in textbox.msg box etc. how ever i have two ideas!

1) to redirect the console output into a textbox
2) rewrite the method to display the output in a textbox,msg box etc

which would be better! i know the first one though! but i guess its not a correct way!
Posted
Updated 15-Apr-12 2:36am
v5
Comments
VJ Reddy 15-Apr-12 7:30am    
Edit: pre tag for C# code added.
OriginalGriff 15-Apr-12 7:43am    
Rewrite it to do what? It is not clear what you are trying to achieve.
Use the "Improve question" widget to edit your question and provide better information.
kalaisw 19-Jun-12 1:08am    
"David Billa"

The best way is probably to have ShowData return a value: a string or an array of strings perhaps:
C#
public string showdata()
    {
    StringBuilder sb = new StringBuilder();
    sb.AppendFormat("Name: {0}\n", name);
    sb.AppendFormat("Age:  {0}\n", age);
    sb.AppendFormat("Sal:  {0}\n", sal);
    return sb.ToString();
    }
However, what I would do is change the name to ToString, and override the default version:
C#
public override string ToString()
    {
    StringBuilder sb = new StringBuilder();
    sb.AppendFormat("Name: {0}\n", name);
    sb.AppendFormat("Age:  {0}\n", age);
    sb.AppendFormat("Sal:  {0}\n", sal);
    return sb.ToString();
    }
Then you can print it:
C#
Console.Write(nc);

or whatever you want to do with it!
 
Share this answer
 
Comments
VJ Reddy 15-Apr-12 9:05am    
Good answer. I did not refresh the page before I posted my answer. Hence, I could not see your solution. 5!
By the time I posted my answer, OriginalGriff has already posted his answer. However, I think this answer may also be helpful.
Return a string of values from the ShowData method. It is better to rename it as GetData to make the intent of method obvious.
C#
public string GetData()
{
    return string.Format("Name:{0}{3}Age:{1}{3}Salary:{2}", 
            name, age, sal, System.Environment.NewLine);
}
//To display it in a TextBox
TextBox1.MultiLine=true;
TextBox1.Text = nv.GetData();
//To display it in a label
Label1.Text = nv.GetData();
 
Share this answer
 
v2
Comments
OriginalGriff 15-Apr-12 9:39am    
Happens to me all the time! :laugh:
Nelek 15-Apr-12 19:23pm    
You are not the only ones :)
mpvkrishnadhar 15-Apr-12 22:29pm    
thanks! guys! both the solutions are working fine!
VJ Reddy 15-Apr-12 23:23pm    
You're welcome.
Any reason for down voting.
mpvkrishnadhar 15-Apr-12 23:55pm    
nope! i have already voted it! just did not know how to do it properly! now i have learned it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900