Testing for Null Properties on CellFormat

I am relatively new to using Aspose Words, but it seems there must be a better way to do this.

I’m trying to test for various properties on CellFormat. It seems that there is no good way to test if these are null without deliberately raising an exception.

This code throws a NullReferenceException
if (cell.CellFormat.Shading.BackgroundPatternColor != null )

I have worked around this type of problem in what I think is a bad and inefficient way such as

private bool IsBorderVisible(Border border)
{
    bool visible = false;

    try
    {
        visible = border.IsVisible;
    }
    catch
    {
        visible = false;
    }
    return visible;
}

Additionally, other than testing each property for null, is there a way to see if the CellFormat has any properties with values? If I could do a simple test to see if the CellFormat was not using any properties, that would be ideal.

Thanks.

Hi Dan,

Thanks for your inquiry. Please use following code example to get the CellFormat’s properties value. Hope this helps you.

Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
Cell cell = (Cell)doc.GetChild(NodeType.Cell, 0, true);
GetProperties(cell.CellFormat);
public static void GetProperties(Object source)
{
    // Iterate through each property in the source object.
    foreach (PropertyInfo prop in source.GetType().GetProperties())
    {
        object value;
        try
        {
            value = prop.GetValue(source, null);
        }
        catch (Exception)
        {
            continue;
        }
        if (value != null)
        {
            Console.WriteLine("Property Name : {0}, Property Value : {1} ", prop.Name, prop.GetValue(source, null));
        }
    }
}

Thanks Tahir.

I went to try this and it is telling me " The type or namespace name ‘PropertyInfo’ could not be found (are you missing a using directive or an assembly reference?)

I tried including every one of the Aspose. Words. ??? namespaces in my using statements and none seem to work. Which using statement should I be using?

Thanks,
Dan

Tahir,

Please ignore my last message - I figured out that it was System.Reflection

Sorry,
Dan

Tahir,

I ran this code and though it works, it still has the same problem as my original code in that there is no way to test for null except by throwing an error. This is very expensive from a time standpoint.

Is there not a simple test to see if something is not null like this and it just returns true or false without raising an error to do it.

if (cell.CellFormat.Borders.Bottom != null)

For a product of this level and price, I should not have to rely on Catch to see if something is null

thanks,
Dan

Hi Dan,

Thanks for your inquiry. You can remove the try catch block from the code example shared here and check the property’s value using If condition. Please note that a class may have properties which have null value. You can check a property value by using If condition.

Could you please share some more detail about your test case? We will then provide you more information on this.