Friday, November 18, 2005

Visual Studio 2005 Express Impressions

I have been evaluating Microsoft’s Visual Studio Express Editions during their Public Beta and after their initial release. These are scaled down, free editions of their respective Visual Studio counterparts, such as Visual Basic, C++, C#, and a separate version for web application development, aimed at standalone developers, hobbyist, and students. Considering Microsoft’s reputation, this is an uncharacteristic move for them, but a very good one in my opinion. This should allow for an even larger developer community to grow on the Windows platform. One of the biggest criticisms of Windows from the Open Source community is the availability of powerful, freely availably developer tools. For most *nix distributions, GCC comes with the packages, and has tons of additional IDE for development, while Windows typically has had to rely on commercial offerings. I feel that these new programs from Microsoft go a long way into addressing these issues (although not completely, you still need to register, and the license is only valid for 1 year).

In this article I will take a look at a simple program in Visual C++ to familiarize you with the Visual Studio environment. This program is a simple Stop Watch type program. The user will be able to enter in a number of Hours, Minutes, and Seconds, and when the time is up, a MessageBox will pop up informing the user that time is up.

First, build the form as shown below. I have included two buttons to start and stop the timer, three NumericUpDown components for setting the Hours, Minutes, and seconds, a text box to show a time countdown, and a progress bar to show overall progress. The Timer component is a non-visual component, so it shows up in a separate section on the bottom of the visual designer. Set the values for the components as you see fit. With the exceptions of the labels, I left the properties on all components default.



Under the class declaration for this form, I added the following two private variables for persistency.

//My private variables for this form. Set the time to end the
//timer and the total duration
private: System::DateTime endTime;
private: System::TimeSpan total_duration;

The function for the button click is as follows:

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
     System::DateTime currentTime;

     //Set the end time, used actual instance of endTime rather than
     //System::DataTime->Now. the reason I set this to an object
     //rather than just setting the values on assignment below
     //is to freeze the time so we do not have a shift in
     //seconds at the time of this function completing.

     currentTime = currentTime.Now;
     endTime = currentTime;

     //Add the number of hours, mins, and secs to the current time
     //to get the itme when the stop watch should stop
     endTime = endTime.AddHours(udHours->Value.ToDouble(udHours->Value));
     endTime = endTime.AddMinutes(udMins->Value.ToDouble(udMins->Value));
     endTime = endTime.AddSeconds(udSec->Value.ToDouble(udSec->Value));

     //set ticks to complete to the value of the timer duration
     total_duration = (endTime - currentTime);

     //Set the indicator text under the progress pane to the duration value.
     txtCountDown->Text = total_duration.ToString();

     //enable the timer
     timer1->Enabled = true;
}

Next, I use the following method for the timers onTick event.

private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
     System::Int32 amount_to_increment;

     //If time is up, stop the timer and show the message box, otherwise
     //continue updating the progress bars
     if (System::DateTime::Now >= endTime)
     {
          timer1->Enabled = false;

          System::Windows::Forms::MessageBox::Show("Times up", "Timer is up", System::Windows::Forms::MessageBoxButtons::OK );
     }
else
          {
               //Update the progress text box with the total duration time minus
               //the current system time
               txtCountDown->Text = (endTime - System::DateTime::Now).ToString();

               //set the total percentage of the progress bar to be
               // total_duration minus the current duration from the end. Divide
               //that result by the total duration and multiply the end result
               //by 100 to give use the percentage. Once we have that
               //figured, assign the overall value of the progress bar to that percent

               amount_to_increment = (
                    (total_duration -
                    (endTime - System::DateTime::Now)
                    ).TotalMilliseconds
                    /
                    total_duration.TotalMilliseconds
                    ) * 100;
               progressBar->Value = amount_to_increment;
               }
          }

And the final method is the Stop button. Below is the code for the Stop button.

private: System::Void cmdStop_Click(System::Object^ sender, System::EventArgs^ e) {
     //stop the timer, do not reset any values as this will be done
     //on the next start button push

     timer1->Enabled = false;
}

Below is a screenshot of the finished application.



Now my StopWatch is complete. There are a few things to note. In the Timer1 Tick method, I used a System::Int32 variable type instead of an int type. Both types will work interchangeably in this instance, however it seems that Microsoft prefers that types are referenced from the System namespace rather than using the intrinsic data types. I figured this out when I had some issues working with the double type. I also alternated between calling System::DateTime methods without an object instance and calling instantiated object functions.

For a free IDE, this is a very impressive offering. Visual Studio .Net has come a long way since the initial release in 2002. The Visual Designer is incredibly easy to work with, there is very decent debugging capabilities, such as being able to move the mouse over and getting a tree view of objects without needing to use the watch window. That is not to say I don’t have my gripes about it as well. For one, the code that it produces is ugly as hell. Its not as standards compliant than GCC, but I think a lot of that is because I have grown used to working with GCC so much. Also, when working with the Visual Designer, double-clicking on a component to bring up its default method creates the code in the header file, not in a separate implementation file. That is a little annoying. There is also a little too much of the form code and various other default .Net code visible by default. You can collapse those, but it’s still a bit of an eye sore. Overall, I do like this, and I am hoping that they release the SDK for Windows Mobile to this version of Visual Studio.

1 comment:

Anonymous said...

I've been programming using c and c++ since 1988 and never found it as difficult as it has become with Visual Studio 2005. We have about 800 programmers where I work, guess what, none want to use Visual Studio 2005 - they continue to use Borland or Visual C++ 6. And the 'newbies' coming out of University are of the same opinion.