[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with a date public class

Posted on 2006-04-03
9
Medium Priority
?
527 Views
Last Modified: 2008-02-01
I keep getting an error that I am using an unasigned local variable with the following.  Can anyone tell me what I am missing?

public class IsYear
{
    private bool ValidYear;
      public IsYear(string sYear)
      {
      int I;
      ValidYear = true;
      try
      {
          I = int.Parse(sYear);
      }
      catch
      {
          ValidYear = false;
      }
      if (I < 1000)
      {
          ValidYear = false;
      }
      if (I > 2099)
      {
          ValidYear = false;
      }
      }

      public static implicit operator bool(IsYear id)
      {
          return id.ValidYear;
      }
}

The error, looks like this.

Compiler Error Message: CS0165: Use of unassigned local variable 'I'

Source Error:

 

Line 27:           ValidYear = false;
Line 28:       }
Line 29:       if (I < 1000)
Line 30:       {
Line 31:           ValidYear = false;

Thanks in advance,
Tom
0
Comment
Question by:tommoran
9 Comments
 
LVL 6

Accepted Solution

by:
PoeticAudio earned 1000 total points
ID: 16362492
I has no value so it can't compare to anything.

if when you declare I you do int I = 0;  instead of just int I;  then you will compile
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16362508
yeah hes right

you have to "USe" all your variables and if its aobject you do

Object Obj = null;
0
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16362553
Unfortunately you will not be able to do

int i = null;

because integer is a value type, so you will get a different error. But what Mikal said will work for non value type objects.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 48

Expert Comment

by:Mikal613
ID: 16362570
i never sadi you could for the int.

I said for objects.

0
 
LVL 1

Author Comment

by:tommoran
ID: 16362595
That worked like a champ.  I actually had it set that way previously.  I guess it was deleted by mistake and I just needed another set of eyes to look at it.

Thanks all
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16362596
good job Poetic :)
0
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16362603
I wasn't saying that you were wrong, I was just pointing out why int = null will not work in this case... just in case the author were to try that.
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 16362606
no problem
0
 
LVL 5

Expert Comment

by:AdGroot
ID: 16362657
The problem is that if

I = int.Parse(sYear);

failed you come in the catch

In that case you didn't assign I

a way to solve the problem is too assign I in the catch with a default

I = 0;


or make de declaration

      int I = 0;



public class IsYear
{
    private bool ValidYear;
     public IsYear(string sYear)
     {
      int I;
      ValidYear = true;
      try
      {
          I = int.Parse(sYear);
      }
      catch
      {
          I = 0;
          ValidYear = false;
      }
      if (I < 1000)
      {
          ValidYear = false;
      }
      if (I > 2099)
      {
          ValidYear = false;
      }
     }

      public static implicit operator bool(IsYear id)
      {
          return id.ValidYear;
      }
}
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question