Checking if an object exists before returning?

Hey,

I have a multiform program. On one form, i wish to return a ListViewItem, to the callling form, but ONLY if an instance of the class was made (i.e. only if the object exists).

It goes like this...

Top of code in secondary form:

ListViewItem myItem;

...

public ListViewItem GetItem()
{
if (myItem != null)
return myItem;
else
return null;
}

Later on in the code of the secondary form:

void ButtonAddClick(object sender, System.EventArgs e)
{
myItem = new ListViewItem();
//myItem no longer = null, so will return when form is OK'd (via the OK button)
}

Is something like this possible Please help :)

Cheers,

Jonathan


Answer this question

Checking if an object exists before returning?

  • TOM A.

    Hi,

    i've managed to get the effect i wanted, using the same method you mentioned. Embarrasingly, it was my poor coding and not that it was difficult to implement. Thank you for the quick response.

  • Lowell M

    #Jonathan wrote:

    I have a multiform program. On one form, i wish to return a ListViewItem, to the callling form, but ONLY if an instance of the class was made (i.e. only if the object exists).

    How do you want it to behave when the item hasn't been created

    #Jonathan wrote:

    public ListViewItem GetItem()
    {
    if (myItem != null)
    return myItem;
    else
    return null;
    }

    You could just as well write that as

    public ListViewItem GetItem()
    {
    return myItem;
    }



  • Checking if an object exists before returning?