Pergunta

how to stop execution of method if property has null ?

my code

string projectName = "";
    public string _ProjectName
    {
        get { return projectName; }
        set 
        {
            try
            {
                if (value != string.Empty)
                    projectName = value;
                else
                    throw new Exception("Enter project name");
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
                return;
            }
        }
    }

if projectName is not null or empty print() can executed

void print()
{
  MessageBox.Show(projectName);
}

how to do it... thanks

Foi útil?

Solução

Using return; in a void stops further execution, so working it into the code is as simple as this;

void print()
{
    if(projectName == null || projectName == string.Empty)
        return;
    MessageBox.Show(projectName);
}

This will prevent the execution of MessageBox.Show(projectName); if the conditions projectName == null || projectName == string.Empty are not met.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top