views:

1265

answers:

5

Evening all,

I've written a transfer application that needs to perform an update, then update a date corresponding to that update. I.e.

string content = string.Empty;
IIvdManager manager;
DateTime configDate;

if (table == Tables.Asset)
{
    content = WebService.GetTblAsset(companyId);
    manager = ivdData.Instance.Asset;
    configDate = ivdConfig.LAST_UPDATE_ASSET;
}
else if (table == Tables.Site)
{
    content = WebService.GetTblSite(companyId);
    manager = ivdData.Instance.Site;
    configDate = ivdConfig.LAST_UPDATE_SITE;
}
else if (table...)
{
    ...
}

if (content.Length > 0)
{
    if (UpdateManager(manager, content))
    {
     configDate = DateTime.Now;
    }

}

What I want is for the configDate property to update the corresponding Date Get/Set in my ivdConfig Static Class.

How can I do this?

A: 

It sounds as simple as you need to do this:

manager.Date = configDate;

Which means I'm almost certainly wrong....

ck
+1  A: 

Not that I think this is good code, but is this what you are looking for?

private DateTime configDate;

private DateTime ConfigDate
{
  get { return configDate; }
  set 
  {
    configDate = value;
    ivdConfig.TheDate = value;
  }
}
Brian Genisio
A: 

DateTime type is a value type, so the code above will just copy the value of ivdConfig.LAST_UPDATE_SITE to dateToUpdate and not the reference to.

To solve you problem you can do the following:

if (table == Tables.Asset)
{
    content = WebService.GetTblAsset(companyId);
    manager = ivdData.Instance.Asset;
}
else if (table == Tables.Site)
{
    content = WebService.GetTblSite(companyId);
    manager = ivdData.Instance.Site;
}
else if (table...)
{
    ...
}

if ((content.Length > 0) && UpdateManager(manager, content))
{
    DateTime updateDate = DateTime.Now;

    if (table == Tables.Asset)
    {
        ivdConfig.LAST_UPDATE_ASSET = updateDate;
    }
    else if (table == Tables.Site)
    {
        ivdConfig.LAST_UPDATE_SITE = updateDate;
    }
    else if (table...)
    {
        ...
    }
}
masterik
This is something that I wanted to avoid due to the number of tables I have to deal with. However, if that's the only way, then I guess I'm stuck to following this pattern!
GenericTypeTea
A: 

Well there does happen to be the PropertyInfo.SetValue method.

Jeremy
+1  A: 

Have you considered the state pattern?

Subclass your table (the table variable) and add a virtual method (Update() perhaps?) that you then override in each specific table type. This will completely remove the else ifs as it will just become:

table.Update();

Pass down any objects you need to this call and then get the value back from the table (as it can update its own specific date property within its implementation of Update()).

I apologise if I have the wrong end of the stick but i'm not 100% sure what you are asking to be honest.

Quibblesome