h0w readkey works?

S

sahel

Hi ;
I wrote a program that it has error if u have time plz help me;

It must get 5 char from user (without pressing Enter key)
Than shows each of the char in different line
But it has this error : Error 1 Cannot implicitly convert type
'System.ConsoleKeyInfo' to 'char'

using System;
class Program
{
static void Main(string[] args)
{
ConsoleKeyInfo d;
char [] string1=new char [5];
for (int i = 0; i < 5; i++)
{
d= Console.ReadKey(false);
string1=d;
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(string1);
}

}
}
 
A

Arne Vajhøj

I wrote a program that it has error if u have time plz help me;

It must get 5 char from user (without pressing Enter key)
Than shows each of the char in different line
But it has this error : Error 1 Cannot implicitly convert type
'System.ConsoleKeyInfo' to 'char'

using System;
class Program
{
static void Main(string[] args)
{
ConsoleKeyInfo d;
char [] string1=new char [5];
for (int i = 0; i< 5; i++)
{
d= Console.ReadKey(false);
string1=d;
}
for (int i = 0; i< 5; i++)
{
Console.WriteLine(string1);
}

}
}


Try:

string1=d;

->

string1=d.KeyChar;

Arne
 
S

sahel

I wrote a program that it has error if u have time plz help me;
It must get 5 char from user (without pressing Enter key)
Than shows each of the char in different line
But it has this error : Error      1       Cannot implicitly convert type
'System.ConsoleKeyInfo' to 'char'
using System;
     class Program
     {
         static void Main(string[] args)
         {
             ConsoleKeyInfo d;
             char [] string1=new char [5];
             for (int i = 0; i<  5; i++)
             {
                  d= Console.ReadKey(false);
                  string1=d;
             }
             for (int i = 0; i<  5; i++)
             {
                 Console.WriteLine(string1);
             }

         }
     }

Try:

string1=d;

->

string1=d.KeyChar;

Arne


thank Arne ;
what u did that now u r very good at c#
 
M

mick

sahel said:
Hi ;
I wrote a program that it has error if u have time plz help me;

It must get 5 char from user (without pressing Enter key)
Than shows each of the char in different line
But it has this error : Error 1 Cannot implicitly convert type
'System.ConsoleKeyInfo' to 'char'

using System;
class Program
{
static void Main(string[] args)
{
ConsoleKeyInfo d;
char [] string1=new char [5];
for (int i = 0; i < 5; i++)
{
d= Console.ReadKey(false);
string1=d;
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(string1);
}

}
}



string1 = d.KeyChar

mick
 
P

Peter Duniho

sahel said:
Hi ;
I wrote a program that it has error if u have time plz help me;

It must get 5 char from user (without pressing Enter key)
Than shows each of the char in different line
But it has this error : Error 1 Cannot implicitly convert type
'System.ConsoleKeyInfo' to 'char'

Start here:
http://msdn.microsoft.com/en-us/library/471w8d85.aspx

You have a number of questions that are probably better-answered by a
true beginners-oriented forum, book, training course, etc. But barring
that…

Functions have return value types. Functions that return nothing still
have a return type of "void" and any other function will return a value
of the type declared for the function.

You can see from the documentation above that the ReadKey() method does
not return a "char" type (where "char" is simply an alias for
"System.Char"). But, if you look closely at the documentation for the
type it _does_ return ("System.ConsoleKeyInfo"), you will find a
property (I will leave the finding of that property as an exercise for
you, the reader) in that type that returns the _actual_ "char" for the
key pressed, assuming one exists.

And note: there won't necessarily be a proper "char" value for every key
that might be pressed, because not every key on the keyboard corresponds
to a specific Unicode character. That's why ReadKey() returns a more
general-purpose value than just a plain character. So that console
applications can react precisely to specific key inputs, rather than
being limited to what can be represented as text (getch() "solves" this
by returning special character sequences instead…not a very good
solution, frankly).

Pete
 
P

Peter Duniho

sahel said:
what u did that now u r very good at c#

He practiced, and read the documentation.

He also learned to spell words correctly (in languages other than his
native one, no less!). You might try that sometime.
 
A

Arne Vajhøj

I wrote a program that it has error if u have time plz help me;
It must get 5 char from user (without pressing Enter key)
Than shows each of the char in different line
But it has this error : Error 1 Cannot implicitly convert type
'System.ConsoleKeyInfo' to 'char'
d= Console.ReadKey(false);
string1=d;

Try:

string1=d;

->

string1=d.KeyChar;

what u did that now u r very good at c#

You learn programming the same way you learn other things:
study and practice.

Arne
 

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

Similar Threads

char ----> int 12
char[] to char 2
Absolute newbie question 2
LINQ puzzle 2
for loop 3
small problem 5
inheritance problem 2
This spanish character string "ñ" cause something that I don't understand 7

Top