How to use the AVR *String Data Type Effectively

45
ASNApalooza 2007 ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights reserved. How to use the AVR How to use the AVR *String Data Type *String Data Type Effectively Effectively …and other tidbits on formatting numbers and dates 1

description

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 - PowerPoint PPT Presentation

Transcript of How to use the AVR *String Data Type Effectively

Page 1: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

© Copyright 2007 by ASNA. All rights reserved.

by Mike MarloweASNA Instructor/Pre-Sales Engineer

© Copyright 2007 by ASNA. All rights reserved.

How to use the AVR *String Data How to use the AVR *String Data Type EffectivelyType Effectively

…and other tidbits on formatting numbers and dates

1

Page 2: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

What this session coversWhat 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

Page 3: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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"

Page 4: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 5: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 6: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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

Page 7: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

Using the ‘+’ sign

• Given this code:

• Nearly the exact same result.

DclFld X Type( *String )

Do FromVal( 1 ) ToVal( 50000 ) X = X + “A”EndDo

Page 8: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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 classDclFld X Type( *String )

DclFld X Type( System.Text.StringBuilder ) New()

Page 9: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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)

Page 10: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Do FromVal( 1 ) ToVal( 50000 )

X.Append("A")EndDo

DclFld X Type( System.Text.StringBuilder ) New(512)

Page 11: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 12: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 13: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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

Page 14: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 15: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 16: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

String.Format method

• You can also do in-line formatting of the data as well.

Page 17: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 18: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 19: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 20: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

A simple example of standard formatting

• Using the ToString method with the C format specifier on a positive number for currency

Page 21: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

A simple example of standard formatting

• Using the C format specifier on a negative number

• Notice the parenthesis?

Page 22: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 23: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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

Page 24: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

A simple example of custom formatting

• Using the digit (#) and zero (0) placeholders

Page 25: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 26: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

Conditional custom formatting

• The first section applies to a positive number.

Page 27: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

Conditional custom formatting

• The second section applies to a negative number.

Page 28: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

Conditional custom formatting

• The third section applies for a number that is zero.

Page 29: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 30: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 31: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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!

Page 32: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 33: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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

Page 34: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 35: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 36: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 37: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 38: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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 date

specific information– Formatting operations

Page 39: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 40: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 41: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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

Page 42: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

*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

Page 43: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

Numeric to String assignment

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

Page 44: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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.

Page 45: How to use the AVR *String Data Type Effectively

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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 ‘=‘.