11

I want to check if my Listentry = null but I don't know how.

Note: ID is a Integer

Here is my Code

if(MeineGaeste[i].ID !=null)
{
    i++;
}
else
{
    MeinGast.ID = i++;
    test = false;
}
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Yannik
  • 887
  • 3
  • 9
  • 29
  • 4
    Integers cannot be null. Is it declared as `int` or `int?` ? – Blorgbeard May 25 '16 at 20:55
  • 1
    If the `ID` member is __declared__ as `int`, then the comparison `ID != null` is meaningless. You will get a compile-time warning about it. – Jeppe Stig Nielsen May 25 '16 at 21:04
  • 3
    I’m voting to close this question because it's missing vital details like the data type of the variable in question. It also doesn't help that the presented code invokes invalid logic in the `else` condition (attempts to increment a `null` value, which should error out if invoked, assuming it compiles). In other words, it just doesn't make enough sense to be able to understand the author's intentions. – jpmc26 May 29 '21 at 12:46

4 Answers4

31

As stated, an int cannot be null. If a value is not set to it, then the default value is zero. You can do a check for 0 if that is what you think it is being set to...

Otherwise if you are keen on avoiding nullable integers, you would have to turn it into a string and then perform the check.

String.IsNullOrEmpty(ID.ToString());

Which returns a boolean so you can stick it into an if statement.

Ibn Masood
  • 1,093
  • 1
  • 14
  • 31
  • 1
    I can't make sense of your answer. If you avoid `int?` (which means `ID` is an `int`), then `ID.toString()` could never produce the empty string, making the check always false. How does it help to convert anything to a string in that case? You still can't detect a missing value if `0` is a valid value otherwise. – jpmc26 May 29 '21 at 12:51
  • 1
    @jpmc26 You are correct that an *unassigned* ID variable would be zero and would resolve to a string as "0". Answer should be: ID.ToString() == "0" or something like that. – Ibn Masood Jun 02 '21 at 14:01
  • And what advantage does that have over just checking the value directly with `ID == 0`? Converting to a string makes no sense here; there's no functional reason to do so. And this answer still does not solve the issue of how to represent or detect a missing value. – jpmc26 Jun 02 '21 at 17:34
  • Even worse, `ID.ToString()` would actually error out if `ID` *can* be `null`. – jpmc26 Jun 02 '21 at 17:45
  • I didnt say there was an advantage. The "answer" is that an int cannot be null and you can do a check for zero. Converting to a string is just a suggestion that may or may not be helpful, but to a noob asking this question, they may not know about this method. – Ibn Masood Jun 04 '21 at 16:30
  • "The 'answer' is that an int cannot be null and you can do a check for zero." Then why does your answer spend more words and space on converting to a string? "Converting to a string is just a suggestion that may or may not be helpful, but to a noob asking this question, they may not know about this method." That statement implies there is some functional use for converting it to string *for solving this particular problem.* Unless you can explain how it helps solve *this particular problem*, mentioning it here will only confuse the inexperienced developers you're talking about. – jpmc26 Jun 04 '21 at 16:37
  • int? i = null; Null it is. – TomeeNS Aug 14 '22 at 19:45
10

An int cannot be null. On the other hand a nullable int, int?, can be. That being said the check that you want to do is meaningless.

Changing the type of ID from int to int?, the check you do has a meaning and the compiler wouldn't complain about it.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • Thank ypu for you answer. But how I can use int? in my program.? – Yannik May 25 '16 at 21:00
  • Thank you for you answer but hiw I can use int? in my program? This is a litle Piece of my schoolproject. – Yannik May 25 '16 at 21:02
  • 1
    @You are welcome. That depends on what you want to do. To be honest with, I don't get what are trying to achieve. However, the first thing you might have to do, as I can infer from your post, is to change the type of ID from `int` to `int?` – Christos May 25 '16 at 21:03
  • so? private int? _ID; public int? ID { get { return _ID; } set { _ID = value; } } – Yannik May 25 '16 at 21:05
  • Exactly ! By the way since you don't have any logic in the setter or in the getter, you could eliminate the above to the following: `public int? ID { get; set; }` – Christos May 25 '16 at 21:06
  • I know but it is a Piece of a schoolwork :) I Need to do this – Yannik May 25 '16 at 21:08
  • The result is always true because an integer never can be null – Yannik May 25 '16 at 21:18
  • 1
    How is that possible? Since the type of ID is a nullable int, ID can be null. – Christos May 25 '16 at 21:26
6

you can use this ID.HasValue it return true when there is a value and false if null

Mahmoud Abas
  • 71
  • 1
  • 3
0

As a normal int type is a value type, it can not be null.

You can use the nullable type int? for the property ID which is basically a struct, that allows null or any int value. See MSDN or this question or this question for additional documentation.

If it is not possible to change the type of the ID property, a workaround might be to choose an integer as marker value which is guaranteed not appear in your model (for example Int32.MinValue, as most generated IDs seem to be positive integers). Don't do this if you don't have to, as this will most likely lead to errors. I would a least recommend to throw an exception if you serialize models with that marker value.

Community
  • 1
  • 1
ventiseis
  • 3,029
  • 11
  • 32
  • 49