Hutility

Common Implementation – Optional Field Mapping

HuLib has made Sage interactions much nicer by using an indexer to get fields from views rather than awkward functions, but there are still some annoyances. It would be nice if we didn’t always have to cast from an object, even when we are setting it to a variable of the same type.

string vendor = apibh["IDVEND"].ToString();
DateTime date = (DateTime)apibh["DATEINVC"];
decimal amount = apibh["AMTUNDISTR"];

apibh["AMTUNDISTR"] = amount;

Using the Dynamic Fields Object

Now HuLib has an alternative (though the above code is still perfectly valid). There is now a dynamic object called Fields, which allows you to get fields out without bothering with type casting:

string vendor = apibh.Fields.IDVEND;
DateTime date = apibh.Fields.DATEINVC;
decimal amount = apibh.Fields.AMTUNDISTR;

apibh.Fields.AMTUNDISTR = amount; // Assignment works too

The above code leverages the dynamic type functionality that was added to C# some years ago. There is no intellisense for this type and no compiler errors or warnings for whatever you add to it:

apibh.Fields.THISDOESNTEXIST; // this does not cause a compiler error

Summary

This is just a convenience extension to views for when you don’t feel like using indexers and casting. The implementation will function exactly the same.
Note: you cannot use Put without verification with Fields.

Leave A Comment

All fields marked with an asterisk (*) are required