Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
972 |
|
Part XII |
APPENDIXES |
|
|||
|
|
|
m_str = new String(str); |
|
|
|
|
} |
|
||
// Get function
char * CDisplayString::GetStr(void)
{
//unmanaged - just returning the string - now commented
//return m_str;
//managed code
char * pStr = 0;
try
{
//copies the contents of m_str into the Windows global heap,
//converting to ANSI format on-the-fly. It allocates
//the required native heap memory
pStr = static_cast<char*>(Marshal::StringToHGlobalAnsi(m_str).ToPointer());
}
// m_str is null
catch (ArgumentException* e)
{
Console::WriteLine(e->ToString());
}
// could not allocate enough memory on native heap catch (OutOfMemoryException* e)
{
Console::WriteLine(e->ToString());
}
return pStr;
}
//set function
void CDisplayString::SetStr(char * str)
{
974 |
|
Part XII |
APPENDIXES |
|
|||
|
|
|
|
// main entry point function int main(void)
{
CDisplayString aString(“This is the string set by the constructor”);
char * pStr = aString.GetStr();
// display it on the command line cout << pStr << endl;
// deallocate memory from Windows global heap for the string
Marshal::FreeHGlobal(pStr); pStr = 0;
// change the value
aString.SetStr(“... and this is the changed value.”);
pStr = aString.GetStr();
// display the changed value on the command line cout << pStr << endl;
////// call a method in a managed class
Console::WriteLine(“Call a managed function and display the return value” ); Console::WriteLine( callGcInt(20));
}
Although this book is totally centered around VC++.NET, I have dedicated one appendix for the new entrant in the languages family — Visual C#
.NET. As a programmer, I am sure that you would like to know what’s happening in the arena of programming, and currently that’s Visual C#.NET. So, go ahead and read through this appendix to find answers to some basic questions:
Why use Visual C# .NET?
What is Visual C# .NET?
How different or similar is Visual C# .NET to other languages?
How easy or difficult is it to program in Visual C# .NET?
Visual C# .NET – The Origin
For decades, programmers all over the world have been working with C and C++. The evolution of C++ popularized the concept of object-orientation. The advantage of C and C++ is that the code can access low-level system resources. However, developing applications using C or C++ is not all that easy. If you have worked with these two languages, you know that you have to develop code from scratch and write each single line of code manually, the obvious result of which is that the task is time consuming. For this reason, the need for a user-friendly rapid application development (RAD) tool was felt. A RAD tool is one that allows you to develop applications instantly. Microsoft, as usual, addressed this need by providing Visual Basic as part of Visual Studio, which is one of the best languages for RAD tools.
Microsoft kept refining Visual Basic and released different versions, version 4.0 through version 6.0, and continued to endeavor for a fine development environment for Windows.
At the same time, Sun Microsystems was developing its own simple and neat language, Java. The most significant factor that laid the foundation for Java’s tremendous success was its platform independence, marketed with the motto “write once, run anywhere.” Java was aimed for the Web. Sun promoted Java as the language for Internet programming.
BASICS OF C# Appendix E 977
Microsoft decided to follow the same goal as Sun — to ease the interaction between the world of developers and the world of the Web. But the approach that Microsoft adopted was different from that of Sun. Keeping in mind that it cannot wipe out the use of the different languages that have become part and parcel of the developers’ world, Microsoft came up with a new strategy — .NET. All languages in .NET are designed to compile to MSIL. Therefore, the developers still have the scope to pick and choose any language and still applications created in different languages can interact with each other.
Microsoft also wanted to bridge the gap between the existing languages. A language was needed whose features would be a mix and match of the best features of C, C++, Visual Basic, and Java. Microsoft’s solution for this is Visual C#.NET (C# is pronounced “C Sharp”). (I will refer to Visual C#.NET as C# here, and that is an accepted norm too.)
This appendix gives you an overview of the basics of C#. It begins by introducing the features and the basic concepts of the language and then proceeds to guide you to write a program in C#.
Introduction to C#
Visual C#.NET merges the power of C, the object-oriented features of C++, the simplicity and productivity of Visual Basic, and the elegance of Java. In addition, as the “.NET” in the name of the language indicates, it has been designed specially for the .NET environment.
The following is a list of C#’s similarities to and differences from other languages:
Unlike C++, C# forbids usage of pointers. This is where C# resembles Java. Accordingly, the code generated by C# is considered as safe code.
C# enforces initialization of variables before being used for manipulation.
Similar to C++, and unlike Java, C# supports operator overloading and type-safe enumerations.
Code in C#, when compiled, can be set to automatically generate XMLformatted code.
C#, following VB6’s way of designing a user interface, uses the toolbox to design a form.
978Part XII APPENDIXES
C# has striking similarities to VB.NET (also referred to as VB 7.0). But the grounds on which C# outscores VB.NET, currently (note this), is its support of some of the powerful features of C++.
C#’s one-stop coding approach (followed by Java) eases code maintenance. This is unlike C++’s chores of files (H and CPP) that had to be maintained.
Besides these features, C# also supports the features that are common to all .NET languages, such as garbage collection.
Moving ahead, I will quickly give you an overview of C#, including the components of C# and the control and flow structure.
Components of C#
The following are some of the basic components of the C# language:
Classes
Variables
Arrays
Structs
Enumerations
Interfaces
Namespaces
The basic concepts of these components are the same as in any other programming language. However, there are a few components that are slightly modified or enhanced in C#. I will discuss only those components in the following sections.
Classes
Because C# is a true object-oriented language, it supports classes. The concept of classes is the same as in C++. The difference lies in the syntax used to declare them. A class is declared using the class declaration statement:
<modifiers> class <class name>
{
------------------------------------
}
