Categories
Technology Writing

Creating C# Applications: Chapter 3

When I work with more than one programming language at a time, such as Java and Perl or VB and C#, the biggest problem I have keeping the languages untangled is remembering to use a semi-colon (;) or not when ending the statement. No matter how hard I try to keep my language syntax separate, I always include the semi-colon when I shouldn’t (such as in VB), or not include it when I should (such as in C++ or Java or C#).

Regardless of the sophistication of a programming language, you must first become proficient with the operators and special characters supported in the language before advancing on its more escoteric elements, and this chapter provides coverage of C#’s operators and special character set — including the infamous semi-colon.

Arithmetic and Concatenation Characters

The simplest of the operators for C# are the arithmetic operators. The addition operator (‘+’) is used to add two numbers together, as it’s used in most other programming languages, such as C++. In addition, the + operator is an example of an overloaded operator within C#. What this means is that you can use it for adding numbers and you can also use it to add two strings together (concatenation), or add a string and a number. An overloaded operator is one that can work with different parameters, or parameter types. C# extends this capability by also translating parameters to compatible types — such as converting an integer to a string for concatenation when adding a string and an integer.

To demonstrate this capability, in the following code, the addition operator is used to add two numbers and two strings and print out the results of both operations.

using System;

class ArithTest 
{
   public static void Main() 
   {
        int i = 6;
	     string s1 = "Value";
        string s2 = " is ";

        // add two numbers
        i = i + 4;
        
        // add two strings
        s1 = s1 + s2;

        // add a string and a number
	     s2 = s1 + i;

        // output values
        Console.WriteLine(i);
        Console.WriteLine(s1);
        Console.WriteLine(s2);
      
   }
}

The result from running this application is:

10
Value is
Value is 10

The plus sign can’t convert automatically between the two operands if the result is ambiguous, or disallowed for implicit type conversion. If you try to add a double to an integer, as shown in the following line of code:

i = i + 4.15

An error results when you compile the application because you can’t implicitly convert a double to an integer.

The addition operator can also be used to perform delegate concatenation, covered in more detail in Chapter 10 “Delegates and Events”.

The addition operator is both a binary operator, meaning that there are two operands for an expression containing the operator, and a unary operator. A unary operator applies to only one operand. Applying the addition operator as a unary operator to an operand just returns the value of the operand:

nVal = i+;

The subtraction operator (‘-‘) is used to subtract one number from another, or to negate the value of an operand, as demonstrated in the following code:

using System;

class ArithTest 
{
   public static void Main() 
   {
        int i = 6;

        // subtract smaller value
        Console.WriteLine(i - 2);

        // subtract larger value
        Console.WriteLine(i - 7);

        // unary
        Console.WriteLine(-i);
      
   }
}

The following values are output when this application is run:

4
-1
-6

The subtraction operator, as with the other arithmetic operators, works with all numeric data types, such as integers, doubles, and decimals.

The multiplication operator (‘*’) multiplies two operands together, while the division operator (‘/’) divides the first number by the second. The modulus operator (‘%’) returns only the remainder after the first operand is divided by the second.

In the following code, each of the operators just described is used between two different operands, both of which are type double, and the results are printed out:

 
using System;

class ArithTest 
{
   public static void Main() 
   {
        double d1 = 4.15;
        double d2 = 1.23;

        //  multiply
        Console.WriteLine(d1 * d2);

        // divide
        Console.WriteLine(d1 / d2);

        // modulus
        Console.WriteLine(d1 % d2);
      
   }
}

The output of this application is:

5.1045
3.3739837398374
0.46 

The multiplication operator is another example of an overloaded operator; it’s also used when declaring pointers, and de-referencing these pointers to access the pointer values as you’ll see in the section “Membership, Array, and Casting Operators” later in this chapter.

With all arithmetic operations, if an overflow occurs because the result is too large for the allowed range of the data type, an OverflowException is thrown if one of the following conditions is met:

  • The checked compiler option is used and
  • The data types of the operands are integers (int, long, or ulong)
  • Or the operands are decimals

Use the checked compiler option when compiling the application in order to generate an OverflowException with integers. This compiler option turns on arithmetic checking:

csc /checked test.cs

In the following C# application, the OverflowException is captured and the error message printed out:

 
using System;

class ArithTest 
{
   public static void Main() 
   {
        int i = 2147148000;
        int i2 = 32000;

        try 
        {
           //  multiply
           i = i * i2;
           Console.WriteLine(i);
        }
        catch (OverflowException e)
        {
           Console.WriteLine("{0}", e);
        }
      
   }
}

When the application is compiled without the checked option, the following output results:

2144165888

Since integer arithmetic checking isn’t turned on, the significant high-order bits are discarded, and the resulting value is returned. When checking is turned on with the check compiler option, the following exception message is printed out:

System.OverflowException: An exception of type 
   System.OverflowException was thrown.
   at ArithTest.Main() 

The OverflowException will occur with all decimal values when the value is too large for the decimal format.

Float and double values don’t generate an overflow exception. Following the IEEE 754 arithmetic specification, values too large for the variable are set to infinity. If a value can’t be defined as a numeric, it is given the constant NaN — meaning Not a Number.

Arithmetic and concatenation operators are useful for converting values, but have no impact on an application’s process. In addition, each of the variables modified by the operators in this section first had to be assigned. Program flow controlled through the use of logical and condition operators and the assignment operators are discussed next.

Categories
Technology Writing

Creating C# Applications: Chapter 1

Introduction

There’s been considerable material on programming C# within the Visual Studio .NET environment, but not as much on C# as the first programming language based on the Common Language Infrastructure (CLI).

This online C# book provides an introduction to C#, the programming language. In addition, the book also takes a look at the CLI as well as the Common Language Runtime (CLR), using C# to demonstrate the various aspects of this standards-based development environment.

We’ll explore CLI/CLR in more depth in other chapters, but for now, to get you started, we’ll take a look at the basics necessary to get you up and running with C#.

The Basics

You can’t work with a programming language without learning the basics of creating an application in that language. Some languages, such the version of Basic found in Visual Basic, are a language and development environment tightly bound into one tool. Others, such as C++, can be bound to a development environment (ala Visual C++), or can be worked with using command line compilers and standard text editors, such as C++ within a Unix environment.

Fundamentally, you need to learn how to create the simplest form of an application in the language, compile it, and then run it. Once you have this information, you can then vary your program as you learn new and different aspects of the language.

This chapter provides an overview of how to create and compile a C# application using the minimum environment — in this case, using the .NET Framework SDK that’s downloadable, for free, from the Microsoft web site.

The Structure of a C# Application

Before installing the C# support environment, let’s take a look at a minimal C# application.

A C# application can consist of one or more modules — separately compiled functional units that don’t have a main entry point — libraries (comparable to COM components), and at least one application entry point. A C# file has an extension of .cs, and may, or may not, contain a Main application method.

A minimal C# application can have the following format:

using System;

class someClass
{
   public static void Main() 
   {
   }
}

As you can see, a minimal application doesn’t require a lot of code, requiring only the following:

  • A reference to the System namespace
  • A class
  • A main function within the class, that’s called when the application’s run

Applications written in C# access external functionality through the use of namespaces — a container for classes, structs, types, and other useful programming goodies (namespaces are described in greater detail in a later chapter, “C# Scoping and Namespaces”).

C# files contain one or more class definitions. In addition, the files can also contain other C# elements such as interface definitions, delegates, and enumerations. A stand-alone application also has a Main function, called when the application is run. However, to create and run the application, you first have to install the environment.

The C# Environment

As with Java, C# is dependent on a runtime environment being present in order to support the language. Java depends on the Java Virtual Machine (VM) and C# is dependent on the Common Language Runtime (CLR), a set of services providing security, type safety, cross-language exception handling, garbage collection, and other features. An application that contains code meant to be managed by the CLR is known as managed code; code outside of the CLR control is know, appropriately enough, as unmanaged code.

There are five different CLR hosts that support managed code:

  • ASP.NET (for server-side Web access)
  • Internet Explorer (for code implemented within the boundaries of the browser)
  • VBA (for script executed within the confines of Outlook or Office or other VBA enabled applications)
  • Windows Form Designer
  • Windows Shell EXE (for applications launched by a command line)

The last CLR runtime host is the one we’ll be using throughout this online book, primarily because this host allows us to focus on C# as a language, rather than C# as being part of ASP.NET or Web Services, and so on. Once you have a command of the language, then you can explore the other more complex uses of the language, such as those just mentioned.

In addition, you’ll be using the command line compiler when creating each of the test applications rather than creating the examples within Visual Studio. Again, this puts the focus on the language rather than the development environment. However, to run the application, you first have to compile it, which means you have to have both the infrastructure and compiler installed on your machine.

Categories
Just Shelley

X-Objects Introduction

Copy found on Wayback Machine.

Since Dynamic HTML was first introduced in 1997, I’ve always provided code that allows DHTML to be used with the two most popular browsers: Netscape’s Navigator and Microsoft’s Internet Explorer. To make cross-browser DHTML easier to work with, I created a set of cross-browser objects, which I’ve used for all of my DHTML effects.

These objects have now been updated to work with IE 6.0, Netscape 6.x, and the DHTML that should be supported with Mozilla 1.0 when it releases in 2002.

Cross-Browser out…cross-DOM in

Netscape 6.x is a complete re-architecture of the older 4.x browser. Originally the Netscape folks were incorporating new technologies such as CSS and XML into the existing Navigator, and were planning on rolling this out as Navigator 5.0. However, last year these same folks decided not to try and hold onto an architecture that just wasn’t compatible with new Web standards. Instead, they, and the Mozilla Group, started fresh, re-building the browser layout engine from the ground up.

Because Netscape 6.x is built from the ground up, and based on current and upcoming standards, you’re going to find that many of the features supported in Navigator 4.x are no longer supported. This includes the use of layers as well as JavaScript styles (JSS). Instead, Netscape 6.x embraces CSS (both CSS1 and CSS2), as well as XML, and the DOM Levels 0 and 1 (and partially DOM 2 from what I can see) releases from the W3C.

As you can imagine, this is going to have an impact of your Navigator-only or cross-browser DHTML effects. How much so could surprise you.

Changes…

The implementations for DHTML for the new DOM-compliant browsers (Mozilla/Netscape 6.x) is the same as that for IE 4.x and up — for most of the functionality. This includes hiding and showing an element using the visibility CSS attribute, as well as moving an element and changing the element’s width, using the respective CSS2 attributes. In fact, Netscape 6.x is going to be closer in functionality to IE than it will be to Navigator 4.x. Read more on shared functionality in the sections “Movement and Visibility”, “Element Height and Width”, and “Layering and Z-Order”, found at the bottom of this page.

One nice surprise is that event handling with Mozilla/Navigator 6.x is quite easy to incorporate into your DHTML effects, thanks to the new Event-based objects in the DOM Level 2. Very little code had to change in my DHTML applications based on event handling, though each DHTML page did have to change (event handling is not part of the X-Object implementation — See the article section titled “Events”).

We’ll explore the changes between Navigator 4.x and 6.x, as well as the new DOM functionality, as we convert my existing cross-browser objects to the new, improved X-Objects.

Categories
Just Shelley

51,000+ DotCom Layoffs…

Recovered from the Wayback Machine.

Being one of those that are part of a steeply growing curve, a layed off dot comer, I found the article Silicon Valley Workers Head Home in the Australian IT to be very interesting.

According to a source quoted in the article, there have been over 51,564 people laid off from DotComs…to date.

I bucked the trend and actually moved to San Francisco from Boston — and I found a new contract within a couple of weeks. Note, though, that I do have a number of years of experience, and with some fairly significant technologies. Still, before we attend a wake for the Internet, time for a reality check folks. The Internet and technology businesses are down, but they ain’t dead.

Categories
Just Shelley

New City, New Servers

The Burning Bird Corporation is now open for business in beautiful San Francisco. What can I say folks, but I love this city!

In addition, I’ve aggregated all of my web sites on to one server. Hopefully the move will go smoothly, but if you find pages missing or out of synch, most likely they didn’t survive the move. Please send me an email with the missing reference.

The Burning Bird (formerly known as YASD) is being joined by other web sites on the Burning Bird Network. Among the new Webs to be posted will sites focusing on travel and art. Man does not live by technology alone…and neither does Woman.

If you’re in the San Francisco area, drop me an email, say Hi, let me know the good restaurants, walks, etc. I would be appreciative.