udf to create objects

May 17, 2015 at 11:33 AM
I am trying to create new objects through a UDF in c#. I have the list, class and some other functions all set up and compiled. When I go into excel and try to enter the details it tells me the name of the object has already been added to the list. Bear in mind I am new to this all, how can I create proper objects? Thank you for your help.

Assuming I am trying to create a bunch of bonds, with the inputs for them in excel cells, and then a call to a CreateBond function, requiring name, issue date, maturity date and coupon to start.
Coordinator
May 17, 2015 at 11:50 AM
I'm not sure what you mean by 'proper objects'.

I guess you can create 'bond handles' that you can later refer to, with code like this:

public class Bond { ... }

public static class BondManager
{
    static int _bondId = 0;
    static Dictionary<string, Bond> _activeBonds = new Dictionary<string, Bond>();

    public static string CreateBond(string name, DateTime issueDate ...)
    {
        Bond bond = new Bond(....);
        string bondHandle = "Bond: " + _bondId++;
        _activeBonds[bondHandle] = bond;
        return bondHandle;
    }

    public static double GetBondValueAtDate(string bondHandle, DateTime dateTime)
    {
        Bond bond = _activeBonds[bondHandle];
        return bond.GetValueAtDate(dateTime);
    }
}
And then on your sheet you'd have something like:
A1: =CreateBond("3 month", ...)
A2: =GetBondValueAtDate(A1, ...)

There are other issues, like how to clean up the list when you're no longer using an object. There are ways to do that, but a good start is to see whether this pattern fits what you want to do.

-Govert