Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6031799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:14:19+00:00 2026-05-23T05:14:19+00:00

I use C# and ASP.NET. I use this code pasted below in my application

  • 0

I use C# and ASP.NET.

I use this code pasted below in my application with no problems, but I would like to know if there is a better way to achieve the same result.

What I need to do:

I need set a variable int imageId using a Loop.
Variable imageId it is outside the Loop so can be used in other part of my code.

My questions:

  • Why if I use just int imageId; the compiler complain (ERROR: Use of unassigned local variable ‘imageId’ )?
  • I use a nullable int type instead of an int imageId = 0; so I can set it on a null value .. (to me seems semantically correct) would you recommend that? If no why.
  • What is you favorite approach to this kind of problem?

Thanks guys for your support!!!!

           int? imageId = null;
            foreach (DictionaryEntry valueEntry in e.Keys)
            {
                if (valueEntry.Key.Equals("ImageContentId"))
                {
                    imageId = Convert.ToInt32(valueEntry.Value.ToString());
                    break;
                }
            }

Please give me a sample of your code thank!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:14:20+00:00Added an answer on May 23, 2026 at 5:14 am

    You could use the IOrderedDictionary method Contains. This would allow you to drop the loop construct altogether. e.g.

    if( e.Keys.Contains("ImageContentId") )
    {
        imageId = Convert.ToInt32( e.Keys["ImageContentId"].ToString() );
    }
    

    Also if you know that the Value is a string you might be interested in the Int32.TryParse method e.g.

    int imageId ;
    if( Int32.TryParse(e["ImageContentId"].ToString(), out imageId ) )
    { }
    

    The only reason that you would want to use an int? is if the data you’re manipulating could be uninitialised. If you’re parsing data and it’s garbage you have 2 options:
    (NB: This assumes that you don’t want to be throwing Exceptions RE the bad data).

    1) Use a default value – This has the benefit of being inherently tolerant of bad data.

    2) Use the int? construct – This has the advantage of informing the rest of the code that the data in known to be bad and ensures that they are designed to cope with null values.

    As for which is better… that can only be answered within the constructs of the system. If you’re performing interop with external dll’s that don’t support nullable types then the decision is pretty clear!

    As for the compiler error: if you’re initializing imageId within an if statement there will be occasions where the initialization branch won’t be taken e.g. a bad value. To ensure that imageId is always initialized, the compiler will force you to initialize it outside of a conditional code path. Generally I always try and initialize a variable when I declare it. e.g.

    int imageId = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to use this code to bind my asp.net menu control to
My ASP.NET always starts like this with the internal server: http://localhost:50793/Website/ I use FckEditor
In ASP.NET MVC (default routing),I'd like to use a URL like this to return
I am creating a model binder to use with asp.net mvc. This is what
I want to use this pure HTML/CSS template for my ASP.NET website: http://sub3.tanguay.de I
In ASP.NET MVC, we're required to use the suffix Controller for all controllers. This
I am trying to use JQuery in my ASP.Net 2.0 website in this scenario:
how can i use request.querystring in asp.net.i have a linkbutton and this is in
Is it possible to use this Role Provider AspNetWindowsTokenRoleProvider with ASP.NET FORMS Authentication (via
In C# ASP.NET MVC application I use Link to SQL to provide data for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.