|
Last week we discussed strings, This week I thought it would be fun if we got into a programming with some standard components.
First we will need to start a new project. Power++ will create a new one for you when you start it (though this can be changed in the options), or you can choose New from the File menu. Drop the following components on the form (all from the standard page of the component toolbar.):
Now right-click on the command button, and choose Properties from the popup menu that displays. A tabbed dialog box should appear. The first page of this dialog box is entitled General. Inside a border panel there is a textbox that has a label over it named "Text", in this textbox, type in the words "Push Me". Now hit the OK button at the bottom of the dialog box. Now your button should have some text on it.
Now right-click on the command button again, only this time hold your mouse pointer over the item Events in the popup menu until another popup menu appears to the side. When it does, click on the item called Click. This will generate an event handler for the click event for the button in the code editor. Now you can start typing away with you code. Any code placed in this event handler will execute whenever the command button is clicked by the user of the program. Try typing the following:
WString strChkBox_1 = "You checked CheckBox # 1."; if( checkbox_1->GetChecked() && !checkbox_2->GetChecked()
) { return false;
WString strChkBox_2 = "You checked CheckBox # 2.";
WString strBoth = "You have both checkboxes checked.";
//checkbox 1 is checked and checkbox 2 is not
textb_1->SetText(strChkBox_1);
} else
if( checkbox_2->GetChecked() && !checkbox_1->GetChecked()
) {
//checkbox 2 is checked and checkbox 1 is not
textb_1->SetText(strChkBox_2);
} else
if( checkbox_1->GetChecked() && checkbox_2->GetChecked() )
{
//both checkboxes are checked
textb_1->SetText(strBoth);
} else {
//niether checkboxes are checked
textb_1->SetText("You should try to check one of the checkboxes.");
}
Now choose Run from the Run menu and feel free to play around with the results. Checkboxes are a great way to add functionality to a program. Try to see if you understand how the code is interacting with the controls. Once you exit your program, you can save your project (from the file menu) and then open it again and refer to it when you are thinking about adding checkboxes to you next application.
If there is a component you would like me to discuss please let me know.
Before we go, I want to thank Darryl Kruczkowski who wanted me to pass along a book that he thinks is really great: C++ for Dummies, by: Stephen Davis and Published by IDG Books.
If you have any questions, feel free to e-mail them directly to me at JStrande(at)compuserve.com. Thanks to all of you who e-mailed me questions / comments last week.
Thanks, See you next week...
Last week we introduced the int and char data types. Upon request I am going to get into the way Power++ deals with character arrays, or as we like to call them, strings. But first let's look at how to convert a number of type int to its string representation using the old C function itoa:
#include <stdlib.h>
// And somewhere in our code.... items = dw_1->RowCount(); // Set items equal to the number of rows in Datawindow
int items; // Declare a variable named items of type int
char temp[10]; // Declare an array of ten characters named tmpe
itoa( items, temp, 10 ); // Call the function itoa()
// (the third arguement is the base
radix notation)
textb_1->SetText( temp ); // Put the result in a text box
In order to use a string in C, as we did above, you create a character array. In Power++ there is a much easier and more flexible way using the WString class. You can declare one like:
WString myString;
or
WString myString = "This is going to be the initial contents of myString!";
You can also copy a character array into a WString as in:
char cString[10]; strcpy( cString, "Testing!" ); // Copy some text into our character array
WString pString;
pString = cString; // Now copy the contents of the character array to our WString
When calling an old C function that is expecting a const character array, passing along a WString will usually work without any special tweaking. In some special cases though, you may need to let your WString object know that you want the character array representation of its contents. In this case, you'd call its GetText() method, as in: MyOldCFunc( pString.GetText() ); See the Power++ help for information on manually modifying the text contained within the WString object. (You need to use the Lock() and Unlock() methods).
WString has a lot of useful functionality built it. To see it in detail, take a look at the WString topic in help. Here's a list of some of the WString methods to give you an idea:
void Clear(); // makes this string a null string
WBool GetEmpty(); // is string empty or null?
WBool GetNull(); // is string null?
WLong GetLong( WBool *error=NULL ); // convert string to long
number; store FALSE in error
// if string is not valid number
WString Left( WULong n );// extract n characters from left of
text
WString Right( WULong n ); // extract n characters from right
of text
WString Substring(WULong startAt, WULong numChars=USE_STR_LEN);
// extract substring
WULong Position( const WWidestChar ch, WULong startAt=0, WBool
ignoreCase=FALSE ); // search for char
WULong Position( const WChar *substring, WULong startAt=0, WBool
ignoreCase=FALSE ); // search for substring
WULong Position( const WString & substring, WULong startAt=0,
WBool ignoreCase=FALSE ); // search for substring
WBool Chop( WLong charPos ); // chop characters from beginning
or end
WString Strip( WBool fromBeg=TRUE, WBool fromEnd=TRUE ); // strip
spaces from beginning and/or end
WBool Trim( WBool fromBeg=TRUE, WBool fromEnd=TRUE ); // strip
spaces in place
WBool ToLowercase(); // convert to lowercase in place
WBool ToUppercase(); // convert to uppercase in place
Before we go, I want to thank Glenn Pope who wanted me to pass along the information that Powersoft offers a significant discount on products for students.
If you have any questions, feel free to e-mail them directly to me at JStrande(at)compuserve.com
Thanks to all of you who e-mailed me questions/comments last week.
Thanks, See you next week...
This week, in response to a request, I will start to delve into some C++ data types. This will not be a complete list, but rather a starting point. Just about any book on C++ will do this subject better justice than I can, but here is somewhere to start from. The data types I will discusss are fundamental data types; there are also derived data types, which we will cover later.
int
int brainCount;
brainCount = 5;
This is an example of a simple variable, it tells the program that it is storing an integer who's name is brainCount, and the integer's value is 5. We say that "brainCount is a variable of type int, storing the number 5." You should use useful names for your variables. For example, if you store an integer for the cost of a trip, you might call the variable costOfTrip. The four main integer number types are: char, short, int & long and each comes in signed and unsigned types (signed versions allowing for the the distinction between positive and negative numbers).
These are all signed types which means that they split their value equally between positive and negative numbers. For example a 2-byte int might run from -32768 to +32767. Unsigned types are the same as above, but can not hold negative values. The advantge is that you can increase the largest value that the variable can hold. If the signed can hold a value between -32768 to +32767, then the unsigned version can hold a value from zero up to 65635. You should use this only if your quantities are never going to represent a negative value. An integer constant is one that you write out explicitly, such as 49 or 1995.
char
Although the char type is used to store characters, such as letters and digits, it is really just another integer type. It's guaranteed to be large enough to hold all the letters, digits, punctuation and the like. In practice, most systems support fewer than 256 characters, so a single byte can usually represent the whole range. While char is most often used to handle characters, it can be used as an integer type smaller than short.
You have several options for writing char constants in C++ . The simplest choice for ordinary characters, such as letters, punctuation and digits is to enclose the characters in single quotes, as in: 'A'. char is neither signed or unsigned by default. You can assign it one or the other if you choose. The unsigned char type typically represents the range 0 - 255 and the signed char type represents the range -128 to 127.
Note that in Power++ you'll often see things such as WInt or WChar. What are these? For all our purposes, there is absolutely no difference between WInt and int. Similarly for WChar and char, WShort and short, WLong and long, and so on. If you see something like WUInt, it is equivalent to unsigned int.
Next week we will get into the bool and float types, and arithmetic operators.
If you have any questions, feel free to e-mail them directly to me at JStrande(at)compuserve.com. Thanks to all of you who e-mailed me questions/comments last week.
I started school two weeks ago with my first class on C++, so I thought why not follow along with the teacher. One problem, the first two lessons were on the topic that I am taking the class for: Object Oriented Programming. I can't very well talk about something I am just learning about, so I will go over some basics of the C++ language that the teacher discussed the other day.
This is the one thing every single C++ program shares, a "main" function (or a "WinMain" for a Win32 program). Power++ hides this from us. If you want to see it, choose Options from the Tools menu. On the first page of the tabbed dialogue box (Editor) there is an option called "Show generated code". By the way, this is a great way to see how truly great code is written.
int main( void )
{
return 0;
}
The "main" function has two basic parts: the function heading:
int main( void )
And the function body; the part inside the brace brackets: { }. Main can return an integer as indicated by int. "main" is the name of the function, and the void inside the parentheses means that this function does not take any arguments, or in other words, does not accept any information.
You can use comments inside your code to make notes to your self, or to a future programmer debugging your program. You can use to front slashes to indicate that everything on that line after the slashes is a comment and should be ignored by the compiler:
SomeCode(); // My cool comments about it
Here is a simple example of a fully functional C++ console-mode program:
int main( void )
{
int fleas; // Create a variable which can store integers
// (variable of type int named 'fleas')
fleas = 28; // Assign a value to the variable
cout << "my cat has ";
cout << fleas; // Display the value of fleas
cout << "fleas.\n";
return 0;
}
The output of this masterpiece of a program is: my cat has 28 fleas.
Programs often use statements that call functions to perform various feats and tasks. When the program begins execution, the computer goes along executing each of the statements in main(). When it reaches a function call, the computer temporarily abandons main() and exeutes the statements in the called function, and returns where it left of in main when it is done.
To call a function name, use the function name followed by parentheses. e.g. Scroll() Many functions require info from the calling function (main for example) to pass such info to a function; place it within paratheneses as in Scroll( 5 ). The passed information are called function arguments. Take for example the follwing function declarations, and the way you would call them (in the comments):
showtime( void ); // No arguments. ex: showtime();
add_em_up( int x, int y ); // Some arguments. ex: add_em_up( 5, 6 );
int add_em_up( int x, int y ); // Some arguments and a return value. ex:
// int sum;
// sum = add_em_up( 5, 6 );
Well a short lesson this week, but I hope an informative one. If you have any questions, feel free to e-mail them directly to me at JStrande(at)compuserve.com.
Thanks to all of you who e-mailed me questions / comments last week.
Last week we wrote a very simple application that required no code! This week we are going to take a step backwards to discuss "Putting together a Power++ resource library".
Some of what we covered last week was covered in chapters 1 & 2 of the Power++ programmers guide. Upon the advice of a coworker, Andres Galeano (thank's AG), I have been reading through the programmers guide. The people at Powersoft really did a good job on this manual. This is the number one resource you have at your disposal, (I will get into the specifics in a little bit).
Web Related Areas
I might be biased, but you are already on the "Absolute", best web reference for Power++ programmers, Absolute Power++, by Bill Klein, is the place to go for news, code and everything else you need. (a shameless plug never hurt anyone). The next web site that I really like is The Power++ discussion list archives. This site is huge; it contains months and months of questions and answers on every Power++ topic imaginable, if you have a question, I bet it has been asked, and most likely answered on this site.
Powersoft has itself dedicated a couple of pages on their web site for Power++. They are as follows:
These are actually really good references. I found some really good info on these pages, especially Dr. Power++. If you know of a web site I missed, let me know.Newsgroups
Not much to say, other than that these are well supported, and I have actually seen questions answered by the actual programmers who wrote Power++ (can't get better than that). The newsgroups are located on the news server forums.powersoft.com and are as follows:
Power++ Books
This will be short, there are only two that I know of. The first is from Que, called: Special Edition - Using Optima++. Written by: Raghuram Bala ISBN 0-7897-0894-9. This is the first book (the only one out at the time) I read about Power++ (Optima++ at that time). The book says it is for people who know C++ well; take them at their word. At the time It confused me very much, this book is not a good book for starting with, after you are more comfortable with the language give the book a try, it has good information on advanced topics.
The second book is a very recent one, it is from McGraw Hill called Power++ Developers Professional Reference written by Peter J. Horwood (Member of Team Powersoft) ISBN 0-07-913255-3. This is actually a great book, but one word of caution: It focuses over half of it's attention on Datawindows, if you are into database programming, check this book out. It does go over much, much more than just Datawindows and even includes a section on the fundamentals of C++, (If you need this, I recommend one of the two books below) It was written to address the average programmer, but the concepts are presented clearly enough for the layperson. All and all this truly is a well written book, and well worth the money.
C++ Books
There are certainly enough books out there to teach you the language C++, but let me recommend two that gave me great help learning the language: Simple C++, Published by the Waite Group Press and written by Jeffrey M. Cogswell. ISBN 1-878739-44-1 $16.95. This is a great starting point for the C++ language. The concepts behind the language are presented so clearly that you don't feel intimidated by the overwhelming nature of the topic.
The second was the best money I have ever spent on a computer book, Bar None. I could dedicate a web site just on how great this book is. Master C++ for Windows, also from the Waite Group Press, written by Rex Wollard - Training Innovations. ISBN 1-57169-000-X $34.95. What I liked about this book so much was that it came with a Computer Based training program on C++, This program is great!!!. In two weeks with this program I had a great foundation in the language, and had not even touched the book. I Highly recommend this book if you are just starting out with C++, or you need a refresher course. The contents of the book include a reference overview of the language, programming exercises and a great alphabetical function reference library (more on this in the upcoming weeks).
Resources you already have
First, the books that came with the program. These are great references. I highly recommend you at least skim through them and become familiar with where info is in them. I will try in the upcoming weeks point out where the current topic is in the guides. Second, the help files. I am the worst at not using them, but the answer to almost every question you have is in there!! Third, The sample applications that come with the program. Wow, these are well written little applications that really address the programming of the components or objects that they are written for. These are a great resource for becoming familiar with these components.
One other thing you have at your disposal is the Reference Card, I don't want to get to in depth with this one yet, It will be part of an upcoming article in a week or two. The reference card is located under the Help menu and it contains a lot of info, so check this out if you haven't done so.
That's about it for this week. Next week we will get into adding some code to last weeks program, so if you haven't done it yet, give it a shot. If you have any questions, feel free to e-mail them directly to me at JStrande(at)compuserve.com.
P.S. Thanks to all of you who e-mailed me questions/comments last week.
See you next week...