Nullable Int Type to be associated with a value within a Loop - What is your favourite approch?

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.

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.

THanks – GibboK Jun 16 at 12:31 For IOrderedDictionary, you can just use Contains msdn.microsoft. Com/en-us/library/… – David Kemp Jun 16 at 12:38 GridViewDeleteEventArgs does not contain e. ContainsKey .. – GibboK Jun 16 at 12:42 Thanks TK I read your post now!

– GibboK Jun 16 at 12:57.

Answers for your Questions All value type in . Net are implemented as structure. And all structure must be initialized before usage.

If you are interested in checking if that value exists or not then nullable is a correct choice. Else no issues with int imageId = 0 You can use below code int imageId = 0; if( e. Contains("ImageContentId") ) { //If you are sure value is int imageId = (int)e"ImageContentId"; }.

You could use Linq to find the entry: var foundKey = e.Keys. FirstOrDefault(k => k.Key. Equals('ImageContentId')); Then the ternary expression to get the value: int?

ImageId = foundKey == null? (int? )null : Convert.

ToInt32(foundKey. Value); However, what type is the Value? If you're sure it's an int, you can cast it, rather than calling Convert.

ToInt32. Also, Convert. ToInt32 returns 0 for some cases where you might want (int?)null.

My favorite approach to this kind of problem is not to rely on overloading the value-holding variable with information regarding whether a value was found. I do this explicitly. Bool value_present = false; int imageId; foreach (DictionaryEntry valueEntry in e.

Keys) { if (valueEntry.Key. Equals("ImageContentId")) { value_present = true; imageId = Convert. ToInt32(valueEntry.Value.ToString()); break; } } And then test value_present before using imageId.

That said, and as others have noted, this is crazy. Don't loop like this, just call e.Keys.Contains() Don't convert to a string and then back to an int. If it's an int, cast it.

This would allow you to drop the loop construct altogether. Also if you know that the Value is a string you might be interested in the Int32. TryParse method e.g. The only reason that you would want to use an int?

Is if the data you're manipulating could be uninitialised. (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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions