Search code examples
c#stringgeneric-listsharepoint-listclass-members

How can I Non-kludgily/tediously prevent the attempt to write empty strings to class members?


I've got a class that represents a Sharepoint List like this:

private class ListColumns
{
    public String li_requestDate { get; set; }
    public String li_paymentAmount { get; set; }
    public String li_payeeName { get; set; }
    public String li_remitAddressOrMailStop { get; set; }
    . . .

I read items that had previously been saved to the Sharepoint List like this:

private List<ListColumns> ReadFromList()
{
    List<ListColumns> lcList = new List<ListColumns>();

    using (SPSite site = new SPSite(siteUrl))
    {
        using (SPWeb web = site.RootWeb)
        {
            SPList list = web.Lists[listTitle];
            SPListItemCollection SpListColl = list.Items;
            foreach (SPListItem item in SpListColl)
            {
                ListColumns lc = new ListColumns();
                lc.li_requestDate = item["RequestDate"].ToString();
                lc.li_payeeName = item["PayeeName"].ToString();
                lc.li_remitAddressOrMailStop = item["RemitAddressOrMailStop"].ToString();
                . . .

This code crashes if a value being assigned to a class member is an empty string - IOW, attempts to assign an empty string to any of these class members results in ReadFromList()'s catch method acting as if a GOTO statement pointing to it had been reached (and that sans the ingathering of $200 -- or any amount, for that matter).

I can prevent this with kludgy/tedious code like so:

if (!(String.IsNullOrEmpty(item["RequestDate"].ToString())))
{
    lc.li_requestDate = item["RequestDate"].ToString();
}
if (!(String.IsNullOrEmpty(item["PayeeName"].ToString())))
{
    lc.li_payeeName = item["PayeeName"].ToString();
}
. . .

(etc. etc. ad nauseum ad finitum adwords with friends &c)

...but have an inkling there must be a "more better" way. Can anyone concretely corroborate my intuition?

UPDATE

Trying Alex's code:

item.SetAsString("RequestDate", x => lc.li_requestDate = x);

I still get, "Object reference not set to an instance of an object" if the value is empty.

MatteoSP's code:

Apply(item, "RequestDate", x => lc.li_requestDate = x);

...doesn't even compile; I get two err msgs:

The best overloaded method match for 'DirectPaymentSectionsWebPart.DPSVisualWebPart.DPSVisualWebPartUserControl.Apply(System.Collections.Generic.IDictionary, string, System.Action)' has some invalid arguments

-and:

Argument 1: cannot convert from 'Microsoft.SharePoint.SPListItem' to 'System.Collections.Generic.IDictionary'

UPDATE 2

My kludgy/verbose code, which I thought would at least work:

if (!(String.IsNullOrEmpty(item["RemitAddressOrMailStop"].ToString())))
{
    lc.li_remitAddressOrMailStop = item["RemitAddressOrMailStop"].ToString();
}

...also fails with the obtuse "Object reference not set to an instance of an object" objection.

Will it be necessary to exalt Kludginess even higher by writing a " " (space) to every otherwise-would-be-empty value when writing to the List?

UPDATE 3

I also tried the idea here posted by Shaw, but with the same old result (crash, with "Object reference not set to an instance of an object")


Solution

  • How about using an extension method

    public static class ListItemExtensions
    {
        public static void SetAsString(this SPListItem item, string colName, Action<string> destSetter)
        {
            if (item != null)
            {
                var col = item[colName];
                if (col != null)
                {
                    var colVal = col.ToString();
                    if (!string.IsNullOrEmpty(colVal))
                        destSetter(colVal);
                }
            }
        }
    }
    

    Usage:

     item.SetAsString("RequestDate", x => lc.li_requestDate = x);