Top Banner
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. Getting to know AVR Getting to know AVR for .NET data types for .NET data types 1
47

ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

Jan 05, 2016

Download

Documents

Peter Robbins
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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

Page 2: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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?

Page 3: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

AVR’s data types relate to .NET’s common type system

Page 4: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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!

Page 5: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 6: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 7: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 8: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 9: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 10: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 11: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 12: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 13: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 14: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 15: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 16: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 17: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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!

Page 18: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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

Page 19: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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.

Page 20: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

Casting sender

• First declare what actual type of object to look for.

Page 21: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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

Page 22: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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.

Page 23: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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?

Page 24: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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?

Page 25: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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!

Page 26: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 27: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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.

Page 28: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 29: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 30: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 31: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 32: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 33: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

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

Page 34: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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 35: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

Conditional custom formatting

• The first section applies to a positive number.

Page 36: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

Conditional custom formatting

• The second section applies to a negative number.

Page 37: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

Conditional custom formatting

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

Page 38: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

Array replacement formatting

• Another powerful and easy custom formatting technique

Page 39: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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.

Page 40: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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.

Page 41: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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.

Page 42: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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.

Page 43: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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

Page 44: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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.

Page 45: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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)

Page 46: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

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.

Page 47: ASNApalooza 2007 © Copyright 2007 by ASNA. All rights reserved. by Mike Marlowe ASNA Instructor/Pre-Sales Engineer © Copyright 2007 by ASNA. All rights.

ASN

Apalo

oza

20

07

ASN

Apalo

oza

20

07

Another example - TrimEnd