| |
|
|
| |
|
|
| |
| |
|
|
BA 371 - VC#.Net Introduction
The material presented in these and the following labs briefly
refreshes and then builds on the things you learned and practiced in
BA302. In BA371 and BA372 we continue to build your programming skills
so that a. you develop a good understanding of what programmers do and
what some of their techniques are and b. you get ready to implement a
prototype of the class project twords the end of BA372.
The labs are self guided in that both questions and their answers are
provided. It is really up to you
to learn this material and get the programming concepts under control.
Your instructor will help you where and when needed, but you should
make it your job to understand these exercises.
Important: The exercises are
structured so that pretty much everything you need to successfully
complete a VC#.Net homework assignment has been dealt with by the time
the homework is due; that is, if you keep up with the labs!!
Although you could 'run' through these labs quickly by cutting the various segments of code from these pages and pasting them into the VS IDE, you would learn very little doing so. We therefore strongly recommend that you study this material while exercising it.
Carefully read the examples, questions and answers.
Cut and paste code, but carefully read and try understanding the code before you compile and run.
Try answering the various questions stated in the labs yourself before checking out the answers.
Experiment with the codes! Don't take them for granted. Play and experiment with them and try to understand the resultant behavior.
A VC#.NET (BA272) refresher first; the Hello, World...
compiled and run
from the command line
In this course you'll do almost all of your VC#.Net programming in
a large and
complex, graphics-based Integrated Development Environment (IDE) called
Visual Studio. However, although these environments offer a lot of
programming 'amenities' including automatic code generation, the codes
still must be compiled and linked into an executable; the environment
simply calls the compiler into action through the operating system and
hands it over the code to compile.
To get a basic sense on how this (behind the scenes) process works
--in this course we'll do our best to take the 'magic' out of things--
let's do a small example manually; i.e.,
write some code and compile and link it manually; i.e., through the
operating system's command line rather than going through a large and
complicated IDE wit lots of 'project' administration. Here's the code:
class Hello
{
static void Main()
{
System.Console.WriteLine("\nHello, World...\n");
}
}
Store this code as ASCII (text) in a file; e.g., hello.cs
Start a command-line (DOS) window off of the Start
--> All Programs --> Microsoft Software --> Microsoft Visual
Studio xxxx --> Visual
Studio Tools --> Visual Studio xxxx Command Prompt.
(NOTE!!! a regular 'vanilla' DOS command window may not work).
Compile the source into an executable by calling the compiler (vbc) and handing it over the source code:
>csc hello.cs
run the executable:
>hello
Visual Studio Integrated Development Environment
(IDE)
Start VS. (NOTE!!! If this is the first time that you
start VS, then you will need to select Visual C# as the primary
area of work).
Using File --> New Project..., set up a new Console
Application.
To do this, click or expand the Visual Basic area of the tree on
the left side of the New Project
dialog box.
On the right side of the screen select Console Application.
Name the application or leave the default name of ConsoleApplication1.
Select OK.
(NOTE!!! If you intend to save your lab, then select File --> Save All, pick a
location and click Save.
Notice that in the Solutions Explorer window, a
project is part of a solution. In VS solutions are complete,
integrated software systems which may contain several executable files,
the source codes of which are managed in projects.
Erase the dummy source code from the edit window, and replace it with the code from the previous exercise. Save all.
Close the project and exit VS. Then startup VS
again and make sure all components are still in place (reload your foo.vbproj
project file project from File --> Recent projects.
Double-click on the foo.vb source file entry in the Solutions
Explorer window to load the file into the source code editor
window. If this does not work, carefully go over steps 2-5 again.
Compile the project and build the executable (Build --> Build "Application Name"). In the Error List there will be an error (if you do not run into this error, make sure that you started a Console Application, not a Windows Application):
Error 1 'Sub Main' was not found in 'ConsoleApplication1.foo'.
Reason: we have not defined the top (Hello) class yet. In Solution
Explorer
window set ConsoleApplication1 --> Properties -->
Application Tab --> Startup object to Hello).
Recompile and notice how ConsoleApplication1.exe
is created in ...\hello\bin\release).
Start a (normal) command-line (DOS) window, cd to the ...\foo\binrelease\ directory and run the executable:
>hello
You may also run the executable directly from the IDE (Debug
--> Start without Debugging (If
your IDE does not have a Start
without Debugging option under
the Debug menu, use its shortcut: Cntrl-F5).
Calling a function in VC#.NET:
Replace the code in hello.cs with the following:
class Hello
{
static void Main()
{
System.Console.WriteLine("Hello World...");
System.Console.WriteLine();
do_this();
System.Console.WriteLine();
} /* end Main() */
static void do_this()
{
int first = 2147483647;
int second = 0;
int result;
result = first + second;
System.Console.WriteLine(System.Convert.ToString(result));
System.Console.WriteLine();
System.Console.WriteLine("Press Enter to Exit");
System.Console.ReadLine();
} /* end do_this() */
} /* end class Hello */
Code contains a function do_this().
do_this() declares
three 32-bit (4-byte)
signed integers.
first is set to (or 'assigned') 2147483647.
second is set to 0.
result is used to store the result of first + second.
do_this() is 'called' from Main().
Compile and run from the IDE.
Now modify the program so that second is initialized to 1, rather than 0.
Compile and run. What do you observe? (What behavior would you expect?).
(Make sure you understand
this behavior!!)
Change result to a long, recompile and run.
Also change either second or first into a long. Recompile and run. Can you explain what just happened?
Functions in VC#:
Notice how in the above code do_this() is called from inside Main().
Void functions: execute pieces of code, then 'go
away':
void my_function()
{
some code;
some more code;
}
int my_function()
{
some code;
some more code;
return(some_value);
}
Example:
class Hello
{
static void Main()
{
int my_var;
System.Console.WriteLine("Hello World...");
System.Console.WriteLine();
my_var = do_this();
System.Console.WriteLine(System.Convert.ToString(my_var));
System.Console.WriteLine();
System.Console.WriteLine("Press Enter to Exit");
System.Console.ReadLine();
}
static int do_this()
{
int first = -99;
return(first);
}
}
Passing parameters 'by value' and 'by reference':
Suppose we have a variable int16 foo in Main() and we want the function or subroutine do_this() to multiply its value.
class Hello
{
static void Main()
{
int my_var = 10;
System.Console.WriteLine("Hello World...");
System.Console.WriteLine();
do_this(my_var);
System.Console.WriteLine(System.Convert.ToString(my_var));
System.Console.WriteLine();
System.Console.WriteLine("Press Any Key to Exit");
System.Console.ReadLine();
}
static int do_this(int foo)
{
foo = foo * 10;
return(foo);
}
} /* end class something */
What do you observe?
- foo is not multiplied by 10 when printed out in Main(). Cause: When passing a variable, only the value of the variable is passed, not the variable itself (a memory copy local to the function is created).
To access the passed variable itself, you must pass a reference to the variable . Try this!! make sure that you can have do_this() modify the value of my_var in Main(). Do not continue until you know how to do this!!
- Notice that regardless how passed (by value or by reference), the variable names in the calling and called functions can be different (my_var in Main() is called foo in do_this()) Do you understand why this works? Do you understand why it would be entirely impractical if this would not work?