I would like to know an alternative, more elegant way to write the following methods.
I am especially not enthusiastic of the nested if
statement or if
for validate null values.
Now there are 3 if
statement for null validation.
Any method:
private void LoadAlertas()
{
// *** OMITTED ***
// Nullable object must have a value
var PortalUser = HttpContext.Current.User.PortalUser();
if (string.IsNullOrWhiteSpace(PortalUser)) throw new ArgumentNullException("HttpContext.Current.User.PortalUser is null");
var personalData = PersonalData.getPersonalData(PortalUser);
if (personalData == null) throw new ArgumentNullException("PersonalData.getPersonalData is null para " + PortalUser);
if (!personalData.iID_PersonalData.HasValue) throw new ArgumentNullException("personalData.iID_PersonalData is null");
var userLogado = Bll.PortalUser.getUsarioPortalByIdPersonalData(personalData.iID_PersonalData.Value);
Another method:
protected void Btn_Accion_Click(object sender, EventArgs e)
{
// *** OMITTED ***
// Nullable object must have a value
var PortalUser = Page.User.PortalUser();
if (string.IsNullOrWhiteSpace(PortalUser)) throw new ArgumentNullException("Page.User.PortalUser is null");
var personalData = MyPortal.AdminManager.Bll.PersonalData.getPersonalData(PortalUser);
if (personalData == null) throw new ArgumentNullException("PersonalData.getPersonalData is null para " + PortalUser);
if (!personalData.iID_PersonalData.HasValue) throw new ArgumentNullException("personalData.iID_PersonalData is null");
var userLogado = MyPortal.AdminManager.Bll.PortalUser.getUsarioPortalByIdPersonalData(personalData.iID_PersonalData.Value);
var userConsultado = setuserConsultado();
userLogado
used for? Why would any of these variables benull
? Where is the data taken from? A Web API, a database, an user input? Also, I think you're using ASP.Net, please add this tag to your question. \$\endgroup\$