Problem with a date public class

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
LVL 1
tommoranAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PoeticAudioCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mikal613Commented:
yeah hes right

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

Object Obj = null;
0
PoeticAudioCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Mikal613Commented:
i never sadi you could for the int.

I said for objects.

0
tommoranAuthor Commented:
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
Mikal613Commented:
good job Poetic :)
0
PoeticAudioCommented:
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
Mikal613Commented:
no problem
0
AdGrootCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.