O'Reilly Forums: Head First C# Typing Program - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Head First C# Typing Program C# syntax (Keys)random.Next(65, 90));

#1 User is offline   SSparks 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 21-March 11

Posted 21 March 2011 - 01:35 PM

Can you define what this syntax is up to.

listBox1.Items.Add((Keys)random.Next(65, 90));

I understand everything except for the "(Keys)" part and its relationship
in the syntax being right before the "random.Next". I get the overall purpose of the code statement which is to pick a random # that then corresponds to a Key value that gets sucked into the listBox1 control. But what is the '(keys)' syntax referred too? What I have seen so far this type of syntax would suggest a casting of the random.Next(65,90) class-method. Or is this a receptor of the returned random number from random.Next somehow? Any examples in MSDN? Hard to search with (Keys)class.method().

Thanks!
0

#2 User is offline   pepita 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 19
  • Joined: 05-March 08

Posted 21 March 2011 - 11:27 PM

"Keys " is an enumeration like you will see later in the book.
You have something like :

public enum Key {

A =0
B=1
.........
........
}

If you want to get "B" , you need an explicit cast from number "1".
smile.gif
0

#3 User is offline   SSparks 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 21-March 11

Posted 22 March 2011 - 03:03 PM

Thanks. This sends me in a good direction to understand.
0

#4 User is offline   Diaz 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 30-September 12

Posted 30 September 2012 - 01:44 AM

Hi,
I tried this exercise. However, there seem to be an issue with

Quote

listbox1.Items.Add((Keys)random.Next(65,90));


line. I can see that keys being counted under key_down event. But no letter is displayed in listbox1 in the form.
Can someone please point out, what I would be doing wrong?
I rebuilt the form twice.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Post 2: I managed to worked it out. However with following two things needed to be done.

1.

public Form1()
        {
            InitializeComponent();
            KeyPreview = true;

        }


without the above the programme doesn't recognize correct keys when pressed.
with it, unable to see all the letters but only one at a time.

2. timer object's Enabled property has to be set to TRUE.

And with all of these settings, the programme gives an error at,

difficultyProgressBar.Value = 800 - timer1.Interval;


Argument out or range exception handled...

Can someone please advise me, why I needed to do the above property /code changes to make my programme work - instead of just using the code using the book. Why am I getting argument out of range exception here?

This post has been edited by Diaz: 30 September 2012 - 02:05 AM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users