Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am a novice programmer. I have a few single value return "gets" that return ID's that looks like this:

C#
Controller.GetCustomerIDByCustomerName(item.Customer.ToLower().Trim(), out int CustomerID);

                    Controller.GetCustomerIDByCustomerName(item.FinalCustomer.ToLower().Trim(), out int FinalCustomerID);

                    Controller.GetHaulierIDByName(item.HaulierName.ToLower().Trim(), out int HaulierID);

                    Controller.GetCustomerDepotIDByName(item.DepotName.ToLower().Trim(), CustomerID, out int CustomerDepotID);


I want to check that CustomerID/ FinalCustomerID etc... have value before continuing with the code. I'm not sure what the best/ most efficient way to do this is.

What I have tried:

I have tried a method that checks on the data:

C#
private bool ValidData()
{
    bool ReturnValue = true;

    if (CustomerID == null)
    {
        cGlobal.ShowWarningMessage("Customer Required", "Customer Required");
        ReturnValue = false;
    }

    if (ReturnValue)
    {
        if (FinalCustomerID == null)
        {
            cGlobal.ShowWarningMessage("Final Customer Required");
            ReturnValue = false;
        }
    }

    if (ReturnValue)
    {
        if (HaulierID == null)
        {
            cGlobal.ShowWarningMessage("Haulier Required");
            ReturnValue = false;
        }
    }

    if (ReturnValue)
    {
        if (CustomerDepotID == null)
        {
            cGlobal.ShowWarningMessage("Customer Depot Required");
            ReturnValue = false;
        }
    }

    return ReturnValue;
}


and then declaring the variables at the top of the page like so:

C#
public int? CustomerID;
        public int? FinalCustomerID;
        public int? HaulierID;
        public int? CustomerDepotID;


However this doesn't work.

Any advice on how to do this would be appreciated.
Posted
Updated 14-Jun-22 1:26am
Comments
Richard MacCutchan 14-Jun-22 7:01am    
I do not think you should declare variables as nullable if they are not allowed to be null.
Will Sewell 14-Jun-22 7:08am    
I want them to be allowed to be null so I can display a message box saying that you require that value to be entered. For some context the whole thing is trying to import columns from a .CSV. So if that column is blank I want to say that it has to have value.
Richard MacCutchan 14-Jun-22 7:16am    
It is not clear from the code above, where these values get set, so the reason for them being nullable is not obvious.
Will Sewell 14-Jun-22 7:19am    
I can appreciate that.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900