We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Problem with a date public class

tommoran
tommoran asked
on
Medium Priority
540 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
Comment
Watch Question

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT

Commented:
yeah hes right

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

Object Obj = null;
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.
CERTIFIED EXPERT

Commented:
i never sadi you could for the int.

I said for objects.

Author

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
CERTIFIED EXPERT

Commented:
good job Poetic :)
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.
CERTIFIED EXPERT

Commented:
no problem

Commented:
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;
      }
}
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.