1 / 45

How to use the AVR *String Data Type Effectively

How to use the AVR *String Data Type Effectively. …and other tidbits on formatting numbers and dates. What this session covers. How to use the .NET framework’s StringBuilder class Reducing error-prone concatenation with the String.Format method Effective numeric and date formatting techniques

topaz
Download Presentation

How to use the AVR *String Data Type Effectively

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How to use the AVR *String Data Type Effectively …and other tidbits on formatting numbers and dates

  2. What this session covers • How to use the .NET framework’s StringBuilder class • Reducing error-prone concatenation with the String.Format method • Effective numeric and date formatting techniques • Numeric-to-string assignment in AVR • The difference between *String and *Char

  3. Working with strings A little closer look… What happens when we do this: Does the value of X change from ASNA to Palooza? DclFld X Type( *String ) X = “ASNA" X = “Palooza"

  4. Working with strings No because strings are a special case data type. They are known as immutable, meaning that they can’t be changed. This means that each assignment of a new value to X causes the previous instance to be destroyed. A new instance of X is created in memory with the new value.

  5. Working with strings Typical ways of working with strings: Use the Concat opcode Simply add one string to another using a ‘+’ sign Working with large, complex strings can cause noticeable performance issues. Let’s take a look at an example.

  6. Using the Concat opcode Given the following simple code: How long do you think it would take for the application to perform this operation? Each iteration through the loop destroys the previous instance of X and creates a new one. DclFld X Type( *String ) Do FromVal( 1 ) ToVal( 50000 ) Concat Suf( "A" ) Target( X ) EndDo

  7. Using the ‘+’ sign Given this code: Nearly the exact same result. DclFld X Type( *String ) Do FromVal( 1 ) ToVal( 50000 ) X = X + “A” EndDo

  8. System.Text.Stringbuilder The framework has a much better, and faster way to work with those strings! Instead of declaring a work variable as a string data type… …declare it using a StringBuilder class DclFld X Type( *String ) DclFldX Type( System.Text.StringBuilder ) New()

  9. System.Text.Stringbuilder So how much data can the StringBuilder class handle? The default minimum capacity is 16 bytes or 32,767 characters The default maximum capacity is 32 bytes or 2,147,483,647 characters However, you can change these default capacities to suit your needs The default size may not do you much good so it might be a good idea to go ahead and create it with a larger buffer. DclFld X Type( System.Text.StringBuilder ) New(512)

  10. System.Text.Stringbuilder The StringBuilder class has an Append method that allows you to add/change the variable without destroying and recreating a new variable each time. DclFld X Type( System.Text.StringBuilder ) New(512) Do FromVal( 1 ) ToVal( 50000 ) X.Append("A") EndDo

  11. System.Text.Stringbuilder What happens if the string being built reaches and exceeds the current size of the buffer? The StringBuilder class will increase its capacity automatically. If you do happen to exceed the maximum capacity, an exception will be thrown.

  12. Some things to keep in mind… The stringbuilder class is great: When you are working with large, complex strings When you need to append data to a string LOTS of times, such as in a large loop Requires you to create and instance the stringbuilder class in memory, which is a bit more taxing than just declaring a *String data type.

  13. Working with strings Complex string building and concatenation can put your sanity to the test. Keeping track of beginning and ending quotation marks as well as line continuations is just the beginning! In-line formatting? Passing and retrieving data needed from other routines or classes? You may get passed the compiler, but what about runtime errors and error handling? DclFld MyWorkerString Type( *String ) MyWorkerString = "<table width='300' border='1' cellspacing='0' cellpadding='3'>“ ++ "<tr>"MyWorkerString = MyWorkerString + "<th colspan='2'>Using the ‘+’ operator</th>“ ++"</tr>"MyWorkerString = MyWorkerString + "<tr>"

  14. System.String .NET’s string class in the framework is more than just a data type. It is also an object that contains several methods available to help when working with strings. Many of these methods are available for use statically, meaning that you don’t have to instance the class in memory to use them in your code.

  15. String.Format method The string class in the framework provides an overloaded format method with replaceable arguments. Notice the reference to the static string class to expose the method for use.

  16. String.Format method You can also do in-line formatting of the data as well.

  17. Numeric Types In AVR for .NET, there really isn’t any such thing as a ‘packed’ field.(The days of worrying about disk space are over!) From a compiler perspective, *Packed, *Zoned and *Binary are all System.Decimal data types. And remember, they are objects too! • Why is this important? because a quick look at the intellisense shows tons of built-in formatting, conversion methods and properties.

  18. ToString Method ToString is a multipurpose method provided by the .NET framework that can: Format numbers, dates, times Provide a human readable string that represents the type of an object (or any other value if the method has been Overridden in the class) ToString often provides many different overloaded methods depending on the type and context of the object being used.

  19. Standard numeric formatting C or c = Currency D or d = Decimal E or e = Scientific ( exponential ) F or f = Fixed-point G or g = General N or n = Number P or p = Percent R or r = Round-trip X or x = Hexadecimal Be sure to review the help file for specific information about each of these.

  20. A simple example of standard formatting Using the ToString method with the C format specifier on a positive number for currency

  21. A simple example of standard formatting Using the C format specifier on a negative number Notice the parenthesis?

  22. Be Aware… With numeric formatting, the output can be different from one machine to another! This is based on the Regional Options in the Control Panel. Computers using different cultures or different currency settings will display different patterns.

  23. Custom numeric formatting 0 = Zero placeholder # = Digit placeholder . = Decimal point , = Thousand separator and number scaling % = Percentage placeholder \ = Escape character 'ABC' “ABC” = Literal string ; = Section separator Other = All other characters E0 E+0 E-0 e0 e+0e-0 = Scientific notation

  24. A simple example of custom formatting Using the digit (#) and zero (0) placeholders

  25. Conditional custom formatting Different formatting can be applied to a string based on whether the value is positive, negative, or zero. To produce this behavior, a custom format string can contain up to three sections separated by semicolons.

  26. Conditional custom formatting The first section applies to a positive number.

  27. Conditional custom formatting The second section applies to a negative number.

  28. Conditional custom formatting The third section applies for a number that is zero.

  29. Date Formatting In addition to the powerful ToString method and custom and standard formatting for numbers, .NET also provides equally powerful options for working with dates and times. Just like a *Packed field relates to a common type in .NET, so do AVR’s *Date, *Time and *Timestamp. Let’s review some background on this.

  30. Date/Time in RPG When ILE RGP was introduced, the world became a better place to live in. In addition to all things ILE (modules, service programs) it also introduced date and time data types. Despite that, most dates and times are still stored on the iSeries as numeric data types to this day. Why is that? Changing the structure of the database is not always feasible. We could just do what we needed to with an RPG data structure.

  31. Date/Time in RPG RPG data structures in .NET however pose an interesting issue. The concept of an overlapping data structure is supported in AVR for .NET through the Overlay keyword. But an RPG data structure cannot be passed from one class to another. In addition, there are much better and intrinsic ways to work with dates in AVR for .NET than using data structures. You will write less code than using data structures!

  32. Date/Time in .NET .NET doesn’t distinguish between date and time values like RPG does. .NET only uses one data type, DateTime, to represent both times and dates. A foreign concept for many, but Visual RPG for .NET helps handle this for RPG programmers in a more graceful way.

  33. How does it work in Visual RPG for .NET? Visual RPG for .NET offers three date and time related data types: *Date *Time *Timestamp In all three cases however, the underlying data type is really .NET’s System.DateTime(Remember the common type system!)

  34. How does it work in Visual RPG for .NET? In the case of *Date, the time values are set to minimal values and ignored. In the case of *Time, the date values are set to minimal values and ignored. And *Timestamp uses both of them.

  35. Best practices The hands-down best way to work with numeric dates is to convert them to Date/Time data types. Using the methods and properties available it will make formatting and duration based operations quite simple. Once formatted, convert them back to numeric types for storage in the database. If you’re lucky enough to be using genuine date data types in your iSeries, skip steps one and three; you’ve already got it made! AVR reads them directly.

  36. Converting numeric datesto .NET DateTime The DateTime data type provides a ParseExact method to assist in converting. The ParseExact method accepts three parameters: A string containing a date and time to convert The expected format for the string in the first parameter An IFormatProvider object that supplies culture-specific format information about the string.

  37. Converting numeric datesto .NET DateTime The ToString method is used on the *Packed data type to convert it to a string for the first parm. If *Nothing is used for the culture specific parm, it will default to current system’s specifications.

  38. DateTime .NET’s DateTime data type is actually an object provided by the framework. It’s extremely powerful and does all the hard work for you! Comparative operations Duration based methods Properties for extracting datespecific information Formatting operations

  39. Converting DateTimeback to numeric Using the ToString method again, the DateTime data can be converted into the appropriate format and assigned back to the numeric type.

  40. Keeping it in perspective Changing your database to use date or time data types may not be feasible. If not, then keep them numeric. Keep in mind that a numeric ‘date’ and its format are two different things! Working with numeric ‘dates’ with traditional RPG methods does impose a bit of work though. Visual RPG still offers up the traditional ways of working with dates and times, but .NET provides a much more graceful way with the DateTime type in the framework.

  41. *String versus *Char AVR’s *String and *Char data types equate to a System.String in the common type system. But notice that VB and C# have no equivalency for AVR’s *Char?

  42. *Char versus System.Char The .NET framework does provide a System.Char data type but: This represents only a single Unicode character AVR’s *Char represents a finite-length string based on its definition

  43. Numeric to String assignment C# and VB.NET (with option strict on) do not allow implicit assignment of numerics to strings

  44. Numeric to String assignment – using the Move op-code If using the Move operation instead of ‘=‘, the target must be a fixed length variable, following the rules as in RPG.

  45. Numeric to String assignment – using ‘=‘ AVR does allow you to do this implicit assignment because of its support for the RPG Move operation. • In this example, notice the assignment being done with ‘=‘.

More Related