asnapalooza 2007 © copyright 2007 by asna. all rights reserved. by mike marlowe asna...
TRANSCRIPT
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.
Getting to know AVR for .NET Getting to know AVR for .NET data typesdata types
1
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
What this session coversWhat this session covers
• AVR’s data type relationship to .NET Framework data types
• How to effectively work with date data types• Why casting is necessary• Effective conversion and formatting techniques• *String and *Integer4: Why they are best for worker
variables• The important difference between *Boolean and *Ind
data types• Why would you ever need to use *OneChar?
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
AVR’s data types relate to .NET’s common type system
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
What does this mean for you?
• AVR’s fully compliant participation with the common type system provides:
• Interoperability with other .NET languages• A world of enhanced formatting and conversion
capabilities surfaced by the framework• Less code to write!
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.
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.
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!
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.
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!)
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.
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.
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.
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.
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
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.
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.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Why casting is necessary
• In .NET, the basic building block is the System.Object.
• This is zygote of life in .NET – all classes derive from this.
• There are many times in which .NET needs to know what type of object are do you want (or intend) to work with.
• Use the thermos example!
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
An example of casting – “control arrays”
• Ever pay attention to sender in those click events? What is sender?
• Sender tells the event who (or which control) caused the event to fire.
• But what type is sender??
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
.NET = Not Extremely inTelligent
• Sender is of type System.Object!
• If you need to get the members of the button that caused the event to fire, you must tell .NET that sender is a button.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Casting sender
• First declare what actual type of object to look for.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Casting sender
• Second, cast sender to that type• Casting is accomplished with the *As keyword
in AVR
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Casting sender
• Once ‘casted’ to the appropriate type, we can now get to its members.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Another way to look at it…
• What can we put in a thermos?– Hot soup– Hot coffee– Cold Ice Tea– Cherry Kool-Aid
• If I handed you a paper cup, a ceramic mug and the closed thermos, would you know which to type of container to use?
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Another way to look at it…
• A paper bag contains the following:– A rock– A pair of socks– A laser pointer– A picture of Roger wearing a sarong
• If the top of the bag was folded over and you couldn’t see its contents, how do you know what’s in it?
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Getting what you want
• X is the bag and all you want is that picture!
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.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
ToString Method
• ToString is a ‘powerhouse’ method provided by the .NET framework with two main purposes:– 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.
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.
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
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?
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.
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
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
A simple example of custom formatting• Using the digit (#) and zero (0) placeholders
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.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Conditional custom formatting
• The first section applies to a positive number.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Conditional custom formatting
• The second section applies to a negative number.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Conditional custom formatting
• The third section applies for a number that is zero.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Array replacement formatting
• Another powerful and easy custom formatting technique
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
*Boolean versus *Ind
AVR Type .NET Type Value*Ind System.Char ‘0’ or ‘1’
*Boolean System.Boolean 0 or 1 (special case integer)
• There’s an important difference between these two data types that you should be aware of.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
*Boolean versus *Ind
• Many AVR programmers use *Ind because of its familiarity, but it is not available in other .NET languages.
• Other languages typically use the Boolean data type for testing true/false, on/off conditions.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Interoperability issues
• Consider the following:– You create an AVR class
that passes an *Ind data type to VB.NET or C# class.
– The receiving class is expecting a Boolean type and attempting to evaluate it in a true/false condition.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Interoperability issues
• The programmer consuming your AVR class would have to not only pay close attention, they would have to compare the returned value to a character value.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Worker variables
• Stop worrying about memory usage and disk space!• Hands down, *String and *Integer4 are the best
types of variables for everyday programming use.• *String provides you with a variable having no
maximum boundaries.• *Integer4 provides you with a work field capable of
holding a number up to 2,147,483,647.– For most work, this is plenty big enough– But be aware of potential issues such as with phone
numbers• 9,179,891,234
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Why would you ever use *OneChar?
• Before we answer that, let’s take a closer look at the difference between *Char and *OneChar.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Using *OneChar
• There are places in the .NET framework that call for a Char data type (or an array of Char’s)
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Using *OneChar – Trim
• This means that the method is looking for a System.Char type, NOT a System.String which is what AVR’s *Char data type is.
ASN
Apalo
oza
20
07
ASN
Apalo
oza
20
07
Another example - TrimEnd