480 likes | 861 Views
Getting to know AVR for .NET data types. What 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
E N D
What 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?
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!
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.
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.
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!
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.
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!)
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.
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.
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.
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.
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
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.
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.
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!
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??
.NET = Not ExtremelyinTelligent 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.
Casting sender First declare what actual type of object to look for.
Casting sender Second, cast sender to that type Casting is accomplished with the *As keyword in AVR
Casting sender Once ‘casted’ to the appropriate type, we can now get to its members.
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?
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?
Getting what you want X is the bag and all you want is that picture!
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.
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.
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.
A simple example of standard formatting Using the ToString method with the C format specifier on a positive number for currency
A simple example of standard formatting Using the C format specifier on a negative number Notice the parenthesis?
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.
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
A simple example of custom formatting Using the digit (#) and zero (0) placeholders
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.
Conditional custom formatting The first section applies to a positive number.
Conditional custom formatting The second section applies to a negative number.
Conditional custom formatting The third section applies for a number that is zero.
Array replacement formatting Another powerful and easy custom formatting technique
*Boolean versus *Ind There’s an important difference between these two data types that you should be aware of.
*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.
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.
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.
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
Why would you ever use *OneChar? Before we answer that, let’s take a closer look at the difference between *Char and *OneChar.
Using *OneChar There are places in the .NET framework that call for a Char data type (or an array of Char’s)
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.