By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,373 Members | 1,792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

variable declare

P: 26
Dear all I got these error when I declare a variable inside one of the methods

Error 47 Use of unassigned local variable 'm'

However i don't get these error when I declare it at the top of the class

Any ideas
Feb 12 '09 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 750
PRR
You need to initialize variables in your method ... like
Expand|Select|Wrap|Line Numbers
  1. int i=0
however in the class all variables are set to default so there is no problem (when object is created)...
Expand|Select|Wrap|Line Numbers
  1. public int MyFun(int c)
  2. {
  3.  
  4. int i;
  5. //set i to something int i=0;
  6. //
  7. if(c>5)
  8. {
  9. i=10;
  10. }
  11. if(c==10)
  12. {
  13. i=1;
  14. }
  15. return i;
  16. // if c has some value like 1 i wont be set therefore you need to set i at the beginning 
  17. }
  18.  
"The C# compiler does not allow the use of uninitialized variables. If the compiler detects the use of a variable that might not have been initialized."
Feb 12 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.