If I am calling a function:
public User GetUserById(int UserId) { User someUser = new User(); //Look up this UserId in the database ... someUser.Name = dbResult['Name']; return someUser; }
Let’s say I pass in a UserId that has no ‘User information’ associated with it.
Suddenly the ‘someUser’ I’m passing back is instantiated, but empty. What’s the best way to check that it’s ’empty’ so I don’t try and display data from it?
The caveman in me wants to check if someUser.Name has a length of greater than zero. I know that’s awful, so if you know what I’m missing – I’d appreciate your help!
The real question is, why would
GetUserById()return an uninitialized object if the ID is invalid? Wouldn’t it be better to throw an exception or – at worst – returnnull?