Let’s continue to explore C#’s data types. This time around, I’ll introduce you to Booleans, strings, and a very important concept—methods.
This TechTip will start where the previous one left off, so if you haven’t read it, it would be a good idea to do so now. I’ll start by adding a Boolean variable to the program I’ve created:
bool boolTest = true;
The Boolean data type also has an RPG relative, the IND data type. However, instead of holding an *On (‘1’) or *Off (‘0’) value, this data type expects the more obvious true and false values. Remember that C# is a case-sensitive language, so true is not the same as True. It’s annoying, but you’ll get used to it.
I won’t explain what you can do with Booleans, because it’s basically the same in every language, but there’s an important detail I must mention: In RPG, the equals sign (=) is used for both attribution and comparison operations. For instance, you can write something like some_var = 5 and if (some_var = 5) in RPG and the compiler will “know” that the first one refers to an attribution operation and the second to a comparison. However, in C# there’s a difference: You need to use a single equals sign (=) for attribution and a double equals sign (==) for comparison. This means C#’s attribution part is exactly the same as RPG’s, but the comparison is not. To compare the variable some_var with the digit 5 on an If statement, you’d need to write if (some_var == 5).
Boolean variables can be initialized on declaration, just like the other data types I presented before. I didn’t mention this on the previous TechTip, but you can also initialize a variable with an expression. In other words, you can do this:
int aDifferentInteger = 1 + 3;
Naturally, for Boolean variables, you can use a Boolean expression. For instance, the following line of code stores a Boolean value (true or false) resulting from the comparison of the variables usedForCurrency and integerTest:
bool aDifferentBool = (usedForCurrency == integerTest);
I’ll revisit Booleans later, when I discuss the flow control statements C# has to offer.
C# Strings Are Weird
The next data type is a bit trickier. C# strings are similar to RPG’s null-terminated VARCHAR in the sense that you don’t define their length and they end with a special character. However, strings are not a basic data type. A string is really an object (or complex) data type, with some interesting features.
Let’s start with the basic stuff. As you might have gathered, you declare and initialize a string just like any other data type:
string aTextTest = "Hi everyone!";
And you can show its contents just like any of the other data types I mentioned so far:
Console.WriteLine(aTextTest);
Sounds simple enough, right? Here’s where it gets weird. As I mentioned on the prequel article to this series, objects can contain both data and operations. Because strings are objects, they come with some handy operations that I’ll be exploring next.
Exploring the String’s Methods
An object usually has data and operations—or properties and methods, to use object-oriented nomenclature. Let’s start with some methods and leave the properties for later. In RPG, you can convert a string to uppercase with the %Upper built-in function (BIF). A BIF is an RPG language “thing” that exists by itself. In C#, there’s a method that does basically the same thing, but it exists in a certain context—in this case, the ToUpper() method is part of the string’s object definition, a bit like a procedure is part of a service program (via the module where the procedure resides). Here’s an example:
Console.WriteLine(aTextTest.ToUpper());
This is similar to the previous code sample, but it adds the .ToUpper()part and produces a different result. While the first version outputs "Hi everyone!", the value with which the variable was initialized, the second version outputs "HI EVERYONE!". As you might have gathered, the call to the method ToUpper() is what’s causing the different output. There are three important things to know about methods: They’re always part of an object; they require the opening and closing parentheses pair following the name of the method (that’s the way C# “knows” that you’re referring to a method, not a property), and some methods can have parameters, just like RPG functions and procedures.
Now let’s look at another example: the substring operation. RPG has a BIF, and, you guessed it, C# has a method. To be more precise, the string class, which is used as a blueprint for a string object, has a method. And this is how you use it:
Console.WriteLine(aTextTest.Substring(0, 2));
This will output “Hi”, because the aTextTest string contains "Hi everyone!" and the method’s parameters indicate that it should start in position 0 (the value of the first parameter) and return two characters (the value of the second parameter). Note that C# uses a comma to separate the parameters, instead of RPG’s semicolon.
That’s nearly all the space I have for now. Let me close this TechTip with an updated version of our program’s code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExploringDataTypes
{
class Program
{
static void Main(string[] args)
{
int integerTest = 0;
decimal usedForCurrency = 0;
float aDifferentNumberFormat = 0.01234567890F;
double andYetAnotherOne = 0.01234567890D;
bool boolTest = true;
int aDifferentInteger = 1 + 3;
bool aDifferentBool = (usedForCurrency == integerTest);
string aTextTest = "Hi everyone!";
Console.WriteLine("integerTest's value is " + integerTest
+ ", usedForCurrency holds " + usedForCurrency
+ ", aDifferentNumberFormat's value is " + aDifferentNumberFormat
+ ", while andYetAnotherOne stores " + andYetAnotherOne);
Console.WriteLine();
Console.WriteLine(aTextTest);
Console.WriteLine(aTextTest.ToUpper());
Console.WriteLine(aTextTest.Substring(0, 2));
Console.ReadKey();
}
}
Next time around, I’ll guide you on a hands-on exploration of the way Visual Studio helps you use an object’s properties and methods. Until then, feel free to leave your comments and questions in the Comments section below.
LATEST COMMENTS
MC Press Online