Home All Groups Group Topic Archive Search About

how to cast an int to a string?

Author
14 Dec 2006 10:12 PM
Rich
Hello,

I created a simple winform app in C# 2005 that contains one button that I
just want to do a console.writeline...
....
namespace testCshap1 {
    public partial class Form1 : Form {
        public Form1() {InitializeComponent();}

        private void button1_Click(object sender, EventArgs e)
        {
            int y = 0;
            Console.WriteLine(y.ToString);
        }
    }
}

How do I print out the value of y?  I tried String(y).  nothing.  How to do
this?

Thanks,
Rich

Author
14 Dec 2006 10:15 PM
pagates
Hi Rich,

Try y.ToString()

Look up the ToString method, and you will see how to add formatting, etc.

PAGates

Show quoteHide quote
"Rich" wrote:

> Hello,
>
> I created a simple winform app in C# 2005 that contains one button that I
> just want to do a console.writeline...
> ...
> namespace testCshap1 {
>     public partial class Form1 : Form {
>         public Form1() {InitializeComponent();}
>
>         private void button1_Click(object sender, EventArgs e)
>         {
>             int y = 0;
>             Console.WriteLine(y.ToString);
>         }
>     }
> }
>
> How do I print out the value of y?  I tried String(y).  nothing.  How to do
> this?
>
> Thanks,
> Rich
Are all your drivers up to date? click for free checkup

Author
14 Dec 2006 10:22 PM
Rich
Thank you.  I already tried y.ToString.  The error message I am getting is
this:

"Argument 1 cannot convert from method to group bool"

I am invoking my procedure from a button on a form - C#2005.  What do I need
to do so that I can print the value of y to the console from the winform app?



Show quoteHide quote
"pagates" wrote:

> Hi Rich,
>
> Try y.ToString()
>
> Look up the ToString method, and you will see how to add formatting, etc.
>
> PAGates
>
> "Rich" wrote:
>
> > Hello,
> >
> > I created a simple winform app in C# 2005 that contains one button that I
> > just want to do a console.writeline...
> > ...
> > namespace testCshap1 {
> >     public partial class Form1 : Form {
> >         public Form1() {InitializeComponent();}
> >
> >         private void button1_Click(object sender, EventArgs e)
> >         {
> >             int y = 0;
> >             Console.WriteLine(y.ToString);
> >         }
> >     }
> > }
> >
> > How do I print out the value of y?  I tried String(y).  nothing.  How to do
> > this?
> >
> > Thanks,
> > Rich
Author
14 Dec 2006 10:25 PM
Stephany Young
As you've already been told, it's:

  y.ToString()

not

  y.ToString


Show quoteHide quote
"Rich" <R***@discussions.microsoft.com> wrote in message
news:78239815-C8A1-459B-BB59-5083868B5AA2@microsoft.com...
> Thank you.  I already tried y.ToString.  The error message I am getting is
> this:
>
> "Argument 1 cannot convert from method to group bool"
>
> I am invoking my procedure from a button on a form - C#2005.  What do I
> need
> to do so that I can print the value of y to the console from the winform
> app?
>
>
>
> "pagates" wrote:
>
>> Hi Rich,
>>
>> Try y.ToString()
>>
>> Look up the ToString method, and you will see how to add formatting, etc.
>>
>> PAGates
>>
>> "Rich" wrote:
>>
>> > Hello,
>> >
>> > I created a simple winform app in C# 2005 that contains one button that
>> > I
>> > just want to do a console.writeline...
>> > ...
>> > namespace testCshap1 {
>> >     public partial class Form1 : Form {
>> >         public Form1() {InitializeComponent();}
>> >
>> >         private void button1_Click(object sender, EventArgs e)
>> >         {
>> >             int y = 0;
>> >             Console.WriteLine(y.ToString);
>> >         }
>> >     }
>> > }
>> >
>> > How do I print out the value of y?  I tried String(y).  nothing.  How
>> > to do
>> > this?
>> >
>> > Thanks,
>> > Rich
Author
14 Dec 2006 10:31 PM
Jon Skeet [C# MVP]
Rich <R***@discussions.microsoft.com> wrote:
> Thank you.  I already tried y.ToString.  The error message I am getting is
> this:
>
> "Argument 1 cannot convert from method to group bool"

No, look carefully. The suggestion was not to use y.ToString, it was to
use y.ToString(). You're calling a method, so you need the brackets.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
14 Dec 2006 11:49 PM
Rich
I'm guilty.  I'm a VB guy (a lazy one - VB puts all that extra stuff in there
for you - including lower and upper case - I guess I have some bad habits now
- and to make it worse - VB2005 holds your hand almost every inch of the way
- even with option strict on.).

Show quoteHide quote
"Jon Skeet [C# MVP]" wrote:

> Rich <R***@discussions.microsoft.com> wrote:
> > Thank you.  I already tried y.ToString.  The error message I am getting is
> > this:
> >
> > "Argument 1 cannot convert from method to group bool"
>
> No, look carefully. The suggestion was not to use y.ToString, it was to
> use y.ToString(). You're calling a method, so you need the brackets.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>
Author
14 Dec 2006 10:33 PM
Rich
hmmm, it turns out that a straight forward

Console.Writeline(y) ;

will suffice.  Interesting.  C#2003 I had to do the y.ToString thing.

Show quoteHide quote
"Rich" wrote:

> Thank you.  I already tried y.ToString.  The error message I am getting is
> this:
>
> "Argument 1 cannot convert from method to group bool"
>
> I am invoking my procedure from a button on a form - C#2005.  What do I need
> to do so that I can print the value of y to the console from the winform app?
>
>
>
> "pagates" wrote:
>
> > Hi Rich,
> >
> > Try y.ToString()
> >
> > Look up the ToString method, and you will see how to add formatting, etc.
> >
> > PAGates
> >
> > "Rich" wrote:
> >
> > > Hello,
> > >
> > > I created a simple winform app in C# 2005 that contains one button that I
> > > just want to do a console.writeline...
> > > ...
> > > namespace testCshap1 {
> > >     public partial class Form1 : Form {
> > >         public Form1() {InitializeComponent();}
> > >
> > >         private void button1_Click(object sender, EventArgs e)
> > >         {
> > >             int y = 0;
> > >             Console.WriteLine(y.ToString);
> > >         }
> > >     }
> > > }
> > >
> > > How do I print out the value of y?  I tried String(y).  nothing.  How to do
> > > this?
> > >
> > > Thanks,
> > > Rich
Author
14 Dec 2006 10:39 PM
Jon Skeet [C# MVP]
Rich <R***@discussions.microsoft.com> wrote:
> hmmm, it turns out that a straight forward
>
> Console.Writeline(y) ;
>
> will suffice. 

Yes, indeed it will. (Well, with a correction from Writeline to
WriteLine.)

> Interesting.  C#2003 I had to do the y.ToString thing.

No, you didn't.

Console.WriteLine (y);

would work on every version of C# from 1.0 onwards.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
15 Dec 2006 5:57 PM
Duggi
Hi Rich

The beauty of C# is you can use .ToString() on all the type of data you
have in your application. This is because all the data or types are
derived from System.Object and Object class has a ToString() static
method.

If the data is a reference type you will get the Type name as string
from ToString(). If the data is value type it will be boxed to an
object and the value will be converted to string and returns it. (I
think you are familiar with boxing and unboxing concepts). This is how
the magic happens.

well System.Console.WriteLine(y) will also do the task at hand... again
this is because y will be boxed to an object and y.ToString() will be
called to return a string because WriteLine() requires a string to
display.

I prefer System.Console.WriteLine(y.ToString())., because it is much
readable.

Thanks
-Srinivas.




Thanks
-Srinivas.


Rich wrote:
Show quoteHide quote
> Hello,
>
> I created a simple winform app in C# 2005 that contains one button that I
> just want to do a console.writeline...
> ...
> namespace testCshap1 {
>     public partial class Form1 : Form {
>         public Form1() {InitializeComponent();}
>
>         private void button1_Click(object sender, EventArgs e)
>         {
>             int y = 0;
>             Console.WriteLine(y.ToString);
>         }
>     }
> }
>
> How do I print out the value of y?  I tried String(y).  nothing.  How to do
> this?
>
> Thanks,
> Rich
Author
16 Dec 2006 7:56 PM
Jon Skeet [C# MVP]
Duggi <DuggiSrinivasa***@gmail.com> wrote:
> The beauty of C# is you can use .ToString() on all the type of data you
> have in your application. This is because all the data or types are
> derived from System.Object and Object class has a ToString() static
> method.
>
> If the data is a reference type you will get the Type name as string
> from ToString(). If the data is value type it will be boxed to an
> object and the value will be converted to string and returns it. (I
> think you are familiar with boxing and unboxing concepts). This is how
> the magic happens.

There may not be any boxing involved. For instance:

int i = 5;
string x = i.ToString();

doesn't require any boxing.

> well System.Console.WriteLine(y) will also do the task at hand... again
> this is because y will be boxed to an object and y.ToString() will be
> called to return a string because WriteLine() requires a string to
> display.

Nope, there won't be any boxing there either - it'll call
Console.WriteLine (Int32), which in turn will call
TextWriter.Write(Int32), which will call
Int32.ToString(IFormatProvider) - no boxing involved anywhere.

> I prefer System.Console.WriteLine(y.ToString())., because it is much
> readable.

More readable than

Console.WriteLine (y);

?

In what way, out of interest? The latter seems to express everything of
interest with minimal fuss.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Bookmark and Share