Sunday, February 17, 2019

3 Fun Stories About Strings and C#

By Steve Endow

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 can create an object, set some properties, and ASP.NET Core handles the serialization, either in JSON or XML.

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
In the example above, I simply wanted to format the GL Account into a 2-4-4 format. Simple, right?

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
It all starts with the CSV data source that provided the GL account string.

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.

You can also find him on Twitter and YouTube






No comments:

Post a Comment

All comments must be reviewed and approved before being published. Your comment will not appear immediately.

How many digits can a Business Central Amount field actually support?

 by Steve Endow (If anyone has a technical explanation for the discrepancy between the Docs and the BC behavior, let me know!) On Sunday nig...