Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder Cipher's Avatar
    Join Date
    Dec 2004
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C# Variables problem

    I'm currently new to C#, i'm VB Developer my question is that, must i gives values to varibales when i declare it, when i dont do that i have this error

    Use of unassigned local variable 'CatID'

    and here's what i do
    int CatID;
    i need a creative atmosphere

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Yes, C# doesn't allow you to use unitialized variables. Where are you setting the value of CatId? Are you trying to manipulate it before you set it? If you're setting the value within a conditional statement, it will throw that error because that condition might not always be met, so you can either initialize it, or have additional steps to set the value.

    Good luck;

  • #3
    Regular Coder Cipher's Avatar
    Join Date
    Dec 2004
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well here's my Code which throws the error

    int CatID;
    if (Request.QueryString["Ca"] != null) {
    CatID = int.Parse(Request.QueryString["Ca"]);
    }
    i need a creative atmosphere

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Yeah, it's in a conditional statement, therefore if that statement isn't met, the value of CatId will never be assigned. You can either initialize it when you declare it, or have an else clause which assigns it some value when the condition isn't met.

    Good luck;

  • #5
    Regular Coder Cipher's Avatar
    Join Date
    Dec 2004
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well i tried also this and it worked
    int CatID = new int();
    Thanks anyway
    i need a creative atmosphere

  • #6
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by Cipher
    Well i tried also this and it worked
    int CatID = new int();
    Thanks anyway
    I'd just like to point out that it seems a bit unnecessary, as it's the same as:
    int CatID = 0;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •