tostring


N

nicol

hi
i know ToString is one of methods in object class . & i use it in my
program but i want to show it results when i run program(show it on
screen)

using System;

namespace inheritance_2
{
class Program
{
static void Main(string[] args)
{
point a = new point(3, 7);
a.ToString(); //***********
}
}
}
public class point
{
protected int x, y;
public point()
{
}
public point(int xvalue, int yvalue)
{
x = xvalue;
y = yvalue;
}
public override string ToString()
{
//return base.ToString();
return "[" + x + "," + y + "]"; // whant to show this
result
}
} // end class point

and i want to know what is the usage of ToString
thanks
 
Ad

Advertisements

N

nicol

All that ToString() does is provide some kind of representation of an
object as a System.String object.  That class doesn't do any kind of
output at all by itself.

If you have a console application, such as it appears in the code you
posted, then you can use the System.Console class, with its Write() and
WriteLine() methods to print text in the console window.

If you have a GUI application, you'll have to use the API relevant to
your program.  For example, in a Forms application, a Label or TextBox
control might be used to display the text, by setting the Text property.
  In WPF, a TextBlock might be what you want.

It all just depends on how you want to present the text and what kind of
program you're writing.

Pet


i write in console app but i don't know how to use it in main()
static void Main(string[] args)
{
point a = new point(3, 7);
a.ToString(); //***********
}
is it correct ?
 
Z

Zach

X.ToString(); gives a string representation of X
Say you have int Y = 3;
Then you cannot show Y in a textbox unless you first convert Y to a string
by means of Y.ToSyring();
There are things you can put inbetween the brackets.

In EN-US Culture
double arg = 123,456,789.00
Console.WriteLine(arg.ToString("C"));$123,456,789.00
Console.WriteLine(arg.ToString("E")); 1.234568E+008
Console.WriteLine(arg.ToString("P")); 12,345,678,900.00%
Console.WriteLine(arg.ToString("N")); 123,456,789.00
Console.WriteLine(arg.ToString("F")); 123456789.00
 
Ad

Advertisements

N

nicol

X.ToString(); gives a string representation of X
Say you have int Y = 3;
Then you cannot show Y in a textbox unless you first convert Y to a string
by means of Y.ToSyring();
There are things you can put inbetween the brackets.

In EN-US Culture
double arg = 123,456,789.00
Console.WriteLine(arg.ToString("C"));$123,456,789.00
Console.WriteLine(arg.ToString("E")); 1.234568E+008
Console.WriteLine(arg.ToString("P")); 12,345,678,900.00%
Console.WriteLine(arg.ToString("N")); 123,456,789.00
Console.WriteLine(arg.ToString("F")); 123456789.00


hi
i know ToString is one of methods in object class . & i use it in my
program but i want to show it results  when i run program(show it on
screen)
using System;
namespace inheritance_2
{
   class Program
   {
       static void Main(string[] args)
       {
           point a = new point(3, 7);
           a.ToString();      //***********
       }
   }
}
public class point
{
   protected int x, y;
   public point()
   {
   }
   public point(int xvalue, int yvalue)
   {
       x = xvalue;
       y = yvalue;
   }
   public override string ToString()
   {
       //return base.ToString();
       return  "["  +  x  +  ","  +  y  +  "]"; // whant to show this
result
   }
} // end class point
and i want  to  know  what  is the usage of  ToString
thanks

thanks i think i get what u mean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top