Whenever you work with data, an integration, or code, you will invariably work with strings. Usually lots of strings.
In some cases, every data field you work with will be a string. In other cases, you'll need to convert strings to numbers, dollars, or dates. And sometimes you'll need to format strings, whether composing data into XML, formatting social security numbers, or working with different date formats.
In this video, I share 3 stories about working with strings in C# that I encountered recently on projects.
1. XML
What do you think when you see this code?
While it may be open to criticism, this code is in use today at a commercial software company, and it apparently works just fine. It could be written many different ways, but this code is about as simple as it gets, is easy to understand, and is easily maintained. So it has that going for it.
Fortunately, it has been a long time since I have had to directly assemble or serialize XML or JSON. My current favorite method for serialization is to not do it at all. Having worked with ASP.NET Core, I am perfectly happy to let the platform handle the serialization for me.
No Serialization Required |
I offer more commentary and background in the video.
2. C# Makes You Work for Your Strings
I am constantly working with strings in C#. And I frequently have to format string data. Capitalization, parsing, number formatting, date formatting, and formatting GL Accounts all require different techniques and tricks in C#. Some are simple and easy, while others can be tedious.
Regex can be passive aggressive |
Nope. In this example, Regex doesn't work. It refuses to format the account string with dashes.
Why?
CSV and Excel: Not a perfect partnership |
Notice anything odd about the GLAccount column? Do you see it? The leading zero truncation?
That quirk with CSV and Excel caused me to waste a few hours trying to figure out how to format a GL account.
So how do you deal with leading zero truncation in CSV files in your C# code? I suggest you do it carefully.
I demonstrate a few string formatting examples in the video and show how they behave differently, depending on the data type and expected length.
3. Dynamics GP Date Formats: A Love Story
If you use Dynamics GP, you will have fondness for the beloved Dynamics GP date formats.
It all started on January 1, 1900 |
The default date of 1/1/1900 is infamous.
Dates and Times Shall Never Mix |
And the separate Date and Time fields are a joy to work with. Especially when the time is in UTC.
How do you generate and format your dates for Dynamics GP?
How do you generate UTC time for Dynamics GP time fields? Have you checked the UTC times carefully?
In the video, I show how the C# UTC time formatting can produce some incorrect results if you aren't careful.
Steve Endow is a Microsoft MVP in
Los Angeles. He works with Dynamics GP, SQL Server, .NET, Microsoft Flow,
PowerApps, and Dynamics 365 Business Central.
No comments:
Post a Comment
All comments must be reviewed and approved before being published. Your comment will not appear immediately.