Hutility

Dynamic Fields in Views (1.0.5 feature)

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 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

No Intellisense or Compile-Time Checking

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

Note that this means the code does not generate compiler errors, but it can cause runtime errors. In addition, if you try and assign a string to an int, this will also cause a runtime error.

Summary

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

Leave A Comment

All fields marked with an asterisk (*) are required