Click here to Skip to main content
16,012,468 members
Home / Discussions / C#
   

C#

 
GeneralRe: Cool Idea Of The Day Pin
Malcolm Smart9-Oct-07 10:25
Malcolm Smart9-Oct-07 10:25 
GeneralRe: Cool Idea Of The Day Pin
eggsovereasy9-Oct-07 11:06
eggsovereasy9-Oct-07 11:06 
QuestionGet Domain Names on specific IP Address Pin
Davids Maguire9-Oct-07 10:00
Davids Maguire9-Oct-07 10:00 
AnswerRe: Get Domain Names on specific IP Address Pin
Peter Vertes9-Oct-07 10:02
Peter Vertes9-Oct-07 10:02 
AnswerRe: Get Domain Names on specific IP Address Pin
mr_lasseter9-Oct-07 15:36
mr_lasseter9-Oct-07 15:36 
QuestionHow to access this variabel? Pin
Joplinazz9-Oct-07 9:48
Joplinazz9-Oct-07 9:48 
AnswerRe: How to access this variabel? Pin
Anthony Mushrow9-Oct-07 9:52
professionalAnthony Mushrow9-Oct-07 9:52 
AnswerRe: How to access this variabel? Pin
Scott Dorman9-Oct-07 9:59
professionalScott Dorman9-Oct-07 9:59 
First, please use the <pre> tags to format large code blocks like this.

From the nature of this question, it would appear that you're pretty new to programming and not just C#. Do you understand what the compiler error is saying in "plain english"? "Scope" essentially refers to anything inside a { } pair, so the class forms one scope and each method inside that class forms another scope (each method is it's own scope). Scope is limited as you go further into the class, so things inside a method are not visible to things outside of the method.

As long as you understand that concept, if you take another look at your code you should see the following things (as they relate to your error message):
  1. The variable rightAnswer is defined inside a function named getQ.
  2. The function button1_Click tries to use the variable rightAnswer


Given the rules of scope I mentioned, the variable is only defined within the getQ method and is not visible outside of that method.

In order to do what you want, the variable must be defined outside of any methods, but inside the class, which gives it a "class-wide" scope. Since it is defined at a higher scope, it is visible to any methods defined. Generally, class variables like this are defined at the top of the class (just under the public partial class Form1 : Form line but before the constructor.


Scott.

—In just two days, tomorrow will be yesterday.

[Forum Guidelines] [Articles] [Blog]

QuestionInstallation System Pin
Ken Mazaika9-Oct-07 9:30
Ken Mazaika9-Oct-07 9:30 
AnswerRe: Installation System Pin
il_masacratore9-Oct-07 21:36
il_masacratore9-Oct-07 21:36 
Question...(object sender, System.EventArgs e) Pin
Imran Adam9-Oct-07 8:43
Imran Adam9-Oct-07 8:43 
AnswerRe: ...(object sender, System.EventArgs e) Pin
Not Active9-Oct-07 8:59
mentorNot Active9-Oct-07 8:59 
GeneralRe: ...(object sender, System.EventArgs e) Pin
Imran Adam9-Oct-07 9:04
Imran Adam9-Oct-07 9:04 
AnswerRe: ...(object sender, System.EventArgs e) Pin
Scott Dorman9-Oct-07 9:49
professionalScott Dorman9-Oct-07 9:49 
GeneralRe: ...(object sender, System.EventArgs e) Pin
Imran Adam9-Oct-07 10:17
Imran Adam9-Oct-07 10:17 
GeneralRe: ...(object sender, System.EventArgs e) Pin
Scott Dorman9-Oct-07 10:27
professionalScott Dorman9-Oct-07 10:27 
QuestionDataGridView HeaderCell Painting Pin
cris34569-Oct-07 7:30
cris34569-Oct-07 7:30 
Questionsocket server disconnect Pin
gizmokaka9-Oct-07 7:25
gizmokaka9-Oct-07 7:25 
AnswerRe: socket server disconnect Pin
led mike9-Oct-07 7:54
led mike9-Oct-07 7:54 
GeneralRe: socket server disconnect Pin
gizmokaka9-Oct-07 8:09
gizmokaka9-Oct-07 8:09 
GeneralRe: socket server disconnect Pin
led mike9-Oct-07 8:29
led mike9-Oct-07 8:29 
Questionlabel text distorted in windows form Pin
sudhirkamath9-Oct-07 6:28
sudhirkamath9-Oct-07 6:28 
AnswerRe: label text distorted in windows form Pin
Juraj Borza9-Oct-07 9:44
Juraj Borza9-Oct-07 9:44 
GeneralRe: label text distorted in windows form Pin
Peter Vertes9-Oct-07 10:06
Peter Vertes9-Oct-07 10:06 
QuestionDatabase Backups Multi-Threading Pin
jikubhai9-Oct-07 5:37
jikubhai9-Oct-07 5:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.