Microsoft Visual C Sharp: Language Building Blocks

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Microsoft Visual C# Building Blocks: 50 Questions and 50 Answers

1. Why shouldn’t all numbers be declared as the larger data types instead of the smaller data types?
Although it might seem logical to use the larger data types, this would not be efficient. You should not use any more system resources (memory) than you need.

2. What happens if you assign a negative number to an unsigned variable?
The compiler returns an error saying that you can’t assign a negative number to an unsigned variable if you do this with a literal. If you do a calculation that causes an unsigned variable to go below zero, you get erroneous data.

3. A decimal value is more precise than a float or a double value. What happens with rounding when you convert from these different data types?
When converting from a float, double or decimal to one of the whole number variable types, the value is rounded. If a number is too big to fit into the variable, an error occurs. When a double is converted to a float that is too big or too small, the value is represented as infinity or 0, respectively. When a value is converted from a float or a double to a decimal, the value is rounded. This rounding occurs after 28 decimal places and occurs only if necessary. If the value being converted is too small to be represented as a decimal, the new value is set to 0. If the value is too large to store in the decimal, an error occurs. For conversions from decimal to float or double, the value is rounded to the nearest value that the float or double can hold. Remember, a decimal has better precision than a float or a double. This precision is lost in the conversion.

4. What other languages besides C# adhere to the Common Type System (CTS) in the Common Language Runtime (CLR)?
Microsoft Visual Basic .NET (Version 7) and Microsoft Visual C++ .NET (Version 7) both support the CTS. Additionally, versions of a number of other languages are ported to the CTS as well.

5. How important is it to understand operators and operator procedures?
You will use operators in almost every application you create. Operator precedence is critical to understand. If you don’t understand operator precedence, you might end up with results that are different from the ones you are trying to achieve.

6. Is it important to master the binary number system?
Although it is not critical to understand binary, it is important. With computers today, information is stored in a binary format. Whether it is a positive versus negative charge, a bump versus an impression, or some other representation, all data is ultimately stored in binary. Knowing how the binary system works will make it easier for you to understand actual storage values. In addition to binary, many computer programmers work with octal and hexadecimal. Octal is a base 8 number system and hexadecimal is a base 16 number system.

The Decimal Number System
The decimal number system is the base 10 system that you use every day. A number in this system, for example 342, is expressed as a power of 10. So, in the decimal number system, this number breaks down to 300 plus 40 plus 2 to give you 342. The base 10 system requires ten different digits 0 through 9. Also, a number is represented as powers of the system’s base and the system of base n requires n different digits.

The Binary Number System
The binary number system is base 2 and therefore requires only two digits, 0 and 1. The binary system is useful for computer programmers, because it can be used to represent the digital on/off method in which computer chips and memory work. Here is an example of a binary number and its representation in the decimal notation you’re more familiar with, writing 1011 vertically:

1 represents eight;
0 represents zero (but would be four if was 1);
1 represents two;
1 represents one; so 8 plus zero plus two plus one equals eleven.

Binary is a great system however with one limitation: it is cumbersome for representing large numbers.

The Hexadecimal Number System
This number system is base 16. Therefore, it requires 16 digits. The digits 0 through 9 are used, along with the letters A through F, which represents the decimal values 10 through 15. Here is an example of a hexadecimal number 2DA and its decimal equivalent:

2 represents 2 times 256, which is 256
D represents 13 times 16, which is 208
A represents 10 times 1, which is 10, equaling 730.

The hexadecimal system is really useful in computing because it is based on powers of 2. Each digit in the hex system is equivalent to a four-digit binary number and each two-digit hex number is equivalent to an eight-digit binary number.

Hexadecimal Decimal Equivalent Binary Equivalent

0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111
10 16 00010000
F0 240 11110000
FF 255 11111111

7. What are the essential constructs that are part of the C# language?
The most essential construct is the “if else” construct. Then, there is the switch statement (a type of selection statement). Next, there are the while, do, and for statements. Finally, there is the “goto” statement and this can be used with case, default, or labels statements.

8. Are there other types of control statements?
Yes, there are the throw, try, catch, and finally statements as well.

9. Can you use a text string with a switch statement?
Yes. A string is a “governing type” for switch statements. This means that you can use a variable that holds a string in the switch and then use string values in the case statements. Remember, a string is simply text in quotation marks.

10. Why is a “goto” statement considered unfavorable?
The goto statement has gotten a bad rap. If used cautiously and in a structured and organized manner, the goto statement can help solve a number of programming problems. The statements “goto case” and “goto default” are prime examples of good uses of goto. The statement goto has a bad rap because the goto statement is often not used cleanly; programmers use it to get from one piece of code to another quickly and in an unstructured manner. In an object oriented language like C#, the more structure you can keep in your programs, the better. They will also be more maintainable by other programmers, should that be the case.

11. What are the four pillars of object-oriented programming?
The four pillars of object-oriented programming are

1. Encapsulation
2. Polymorphism
3. Inheritance
4. Reuse

12. What is encapsulation?
Encapsulation is the concept of making “packages” that contain everything you need. In object-oriented programming, this means that you can create a class that stores all the variables that you need and all of the routines to commonly manipulate this data. For example, you can create a class called “Circle” that stores all information about circles. This class can include routines, for example, that include getting a circle’s area, radius, circumference, center point, etc. By encapsulating a circle, you allow the user to be oblivious to how the circle works. He or she only need to know how to interact with the circle.

13. What is polymorphism?
Polymorphism is the ability to assume many forms which means that programs can work with the information you send to them. For example, you could have a routine that gives the area of a shape. Because the area of a triangle is calculated differently than that of other shapes, the routine to calculate the area would need to adapt based on what is sent. Regardless of whether a triangle, circle, or another shape is sent, the routine would be capable of treating them all as shapes and calculating, for example, the area.

14. What is inheritance?
In many object-oriented programming books, an animal analogy is used to illustrate inheritance. The analogy starts with the concept of an animal as a living being. Now consider reptiles, which are everything that an animal is, plus they are cold-blooded. A reptile contains all of the features of an animal, but it also adds its own unique features. Now consider a snake. A snake is a reptile that is long and skinny and that has no legs. It has all the characteristics of a reptile, yet has its own unique characteristics. A snake can be said to inherit the characteristics of a reptile. A reptile can be said to inherit the characteristics of an animal.

15. What is reuse?
When you reuse a class, you can reuse it to create lots of objects. By using inheritance and some of the features described previously, you can create routines that can be used repeatedly in many programs and in many ways. By encapsulating functionality, you can create routines that have been tested and are proven to work.

16. Why are classes defined?
Ultimately, classes are defined so that you can then create objects. A class by itself does not have the capacity to hold information or actually perform routines. Instead, a class is used to declare objects. The objects within a class can be used to hold the data and perform routines defined within the class.

17. What is instantiation?
Instantiation is the declaring of an object, so this means that an object is an “instance” of a class.

18. Would you ever use a class with just data member?
Generally, you would not use a class with just data members. The value of a class and of object-oriented programming is the capability to encapsulate both functionality and data into a single package.

19. Should all data members always be declared public so people can get to them?
Absolutely not. Although many data members many be declared as public, sometimes you don’t want others to get to your data. One reason is to allow for the capability to change the way that the data is stored.

20. What classes already exist for C#?
Microsoft has provided a bunch of classes called .NET base classes, and also has provided documentation on what each of these classes can do. The classes are organized by namespace.

21. What are other names for routines in C#?
Two popular “other names” for routines in C# are functions and methods. Most developers today refer to routines as methods, but there are some developers who refer to them as functions. A method (e.g. function) is a named piece of independent code that is placed in a reusable format and can operate without interface from other parts of an application. If created correctly, it should perform a specific task that is indicated by its name. Methods can both return a value as well as have information passed along to them.

22. What does a method header tell us about a method?
There are four things that we can know about a method from its header:
a. The access that programs have to the method.
b. The return data type of the method.
c. Any values that are being sent to the method.
d. The name of the method.

23. How does a method header end?
A method header ends without a semicolon. If you place a semicolon at the end of the method header, you will get an error.

24. How should you name your methods?
You should name methods appropriately by giving them a meaningful name. For example, if a method calculates and returns the area, the name “getArea” makes sense, as would names like “CalculateArea” and “CalcArea”. Names like “routine1” make little or no sense.

25. How do you use a method?
You must call a method for it to be used. A method is called the same way that a data member is called. You enter the object name followed by a period and then the method name. The difference between calling a method and calling data members is that you must also include parentheses and any parameters that are needed.

26. How do you pass values to methods?
You must specify the header along with appropriate parameters. The format of a method header with parameters is as follows:

Modifiers ReturnType Name (Parameters)

The parameters are passed within the parentheses of the method. Parameters are optional so if no parameters are sent, the parentheses are empty. The basic format for each parameter that is used is as follows:

{Attribute} Type Name

Type is the data type of the value being passed and Name is the name of the variable being passed. Optionally, you can have an attribute as well.

27. What are the three types of “access” attributes for parameters?
These “access” attributes for parameters are (1) value, (2) reference, and (3) out. Value access on a parameter refers to when a copy is made of the data being sent to the method. The method then uses a copy of the data being sent. The original values sent to the method are not impacted. In the case that you want to modify the data stored in the original variable being sent to a method, you can pass a reference to the variable instead of the variable’s value. So, a reference is a variable that has access to the original variable. If you change the reference, then you change the original variable’s value. Finally, you can add parameters to your method header specifically for returning values by adding the “out” keyword. This keyword signifies that a value is being returned out of the method but is not coming in. When you call a method that has an out parameter, you must be sure to include a variable to hold the value being returned.

28. Are there special types of methods?
Yes – special types of methods include (1) property accessor methods, (2) constructors, and (3) destructors/finalizers. Examples of property accessor methods include “set” and “get”. These methods enable you to keep data members private. Constructors are used when you want some setup to occur when the object is first created. This method is used specifically for this purpose (e.g. initial set-up or construction). There are two types of constructors: (1) instance and (2) static. Instance constructors are used when each instance or object is created and static constructors are used before any objects are created for a class. Finally, you can perform some operations when an object is destroyed. These are accomplished in the destructor. From the technical side of things, a destructor is generally called by the C# runtime after an object of a class is no longer in use. The C# runtime normally calls destructors just before checking to see whether any available memory can be freed or released (a concept called garbage collection). If the C# runtime does not do any of this memory checking between the time the object is no longer used and the time the program ends, the destructor will never happen. It is possible to force garbage collection to happen. However, it makes more sense to just limit your use of destructors. A destructor is defined by using a tilde (~) followed by the class name and empty parentheses.
//———————–
//sample destructor
//———————–
~xyz()
{
//destructor class
}
//————————

29. What is the difference between a parameter and an argument?
A parameter is the definition of what will be sent to a method. A parameter occurs with the definition of a method in the method head. An argument, on the other hand, is a value that is passed to a method. You pass arguments to a method. The method matches the arguments to the parameters that were set in the method’s definition.

30. Can you create a method outside of a class?
No. Because Microsoft Visual C# is object-oriented, all code must be within the framework of a class or classes.

31. If I cannot depend upon destructors, how can I do cleanup code?
It is recommended that you create your own methods that perform this task. For example, if you have a class that creates a file object, you will want to close the file when you are done with it. Because a destructor might not be called, you should create your own closing method. You don’t want to leave this file sitting open longer than you need to.

32. What are a few alternative data storage methods?
There are three such methods: structures, enumerators, and arrays.

33. What is the difference between a structure and a class?
The primary difference between a structure and a class is centered on how a structure is stored and accessed. A structure is a value data type, while a class is a reference data type. A value data type stores the actual values at the location that points to where the information is stored. Because the overhead of reference is not included, a structure is preferred when dealing with small amounts of data or small amounts of data values. When dealing with large amounts of data, however, a class is preferred. This is especially true when passing the data to a method. So, if you need to decide between using a structure or class, determine first if the total size of data (members) is 16 bytes or less. If so, a structure is recommended. Otherwise, you should use a class.

34. Is declaring members in a structure the same as a class?
Yes – however the struct keyword is used instead of class.
Example:
//———————–
//sample structure
//———————–
struct Point
{
public int x;
public int y:
}
//————————

35. What is a nested structure?
Like classes, structures can contain any other data type, which includes other structures (e.g. nested structures).

36. What is a structure method?
Like classes, structures also contain methods and properties. Methods and properties are declared exactly the same as classes. This includes using the modifiers and attributes used with classes. You can (1) overload these methods, (2) pass values, and/or (3) return values.

37. What is unique about a structure constructor?
Unlike classes, if you decide to declare a constructor, you must include declarations with parameters. You cannot declare a constructor for a structure that has no parameters.

38. Can destructors be used in structures?
Whereas classes can have destructors, structures cannot. If you try to add one to a structure, the compiler gives you an error.

39. What is an enumerator?
Enumerators are another type that can be used in C#. Enumerators enable you to create variables that contain a limited number of values. For example, there are only seven days in the week. Instead of referring to the days of a week as 1, 2, 3, etc. it is much clearer to refer to them as Day.Monday, Day.Tuesday, Day.Wednesday, etc. Enumerators are declared with the “enum” keyword.

The format for an enumerator is as follows:
//————————–
//sample enumerator
//————————–
modifiers enum enumName
{
enumMember1;
enumMember2;
}
//—————————-

40. In the above example, what does “modifiers” represent?
The term “modifiers” is either the new keyword or the access modifier (public, private, protected, or internal). Note that by default, when an enumerator variable is initially declared, it is set to the value of 0.

41. How can I change the default value of an enumerator variable?
The default value assigned to an enumerator variable is 0, however, you can change this default value. First, you can put a filler value in the first position of the enumerator. This is an easy option if you want that value to start at 1. The second option is to explicitly set the value of your enumerator members. These can be set with literal values, the value of other enumerator members, or calculated values.

42. How do I change the underlying type of an enumerator?
If you do not specify the exact type, the default for enumerators is “int”. However, other types can be selected, such as byte, sbyte, uint, short, ushort, long, and ulong.

To change the default type, use the following format:

modifiers enum enumName : typeName {members(s)}

43. When should I use an array to store data?
If you need to keep track of items that are of the same data type, the best solution is to use an array. An array is a single data variable that can store multiple pieces of data that are each of the same data type. Each of these elements is stored sequentially in the computer’s memory, thus making it easy to manipulate them and navigate among them. To declare an array, you must use the square brackets [] after the data type when you declare the variable.

The basic format is as follows:

datatype[] name;

44. How does an array work?
After you have declared an initialized an array, you can begin to use it. Each item in an array is called an element. Each element within the array can be accessed by using an index. An index is a number that identifies the offset, and thus, the element within the array.

Here is a sample array:

decimal[] balance = new decimal[] {1000.00m, 2000.00m, 3000.00m};

45. Where can arrays be used?
An array is just another type that can be used to create variables. Arrays can be placed and created anywhere other data types can be used. This means that arrays can be used in structures, classes, and other data types.

46. How is the “foreach” keyword to be used?
The “foreach” keyword can be used to simplify working with arrays, especially when you want to loop through an entire array. Additionally, instead of using the array name with a subscript, you can use a simple variable to work with the array. The downside of the “foreach” statement is that the simple variable that you get to use is read-only, so you cannot do assignments to it.

Here is the “foreach” format:

foreach (datatype varname in arrayName)
{
statements;
}

47. What are they types of arrays I can use?
You can use (1) one-dimensional arrays, (2) rectangular arrays where subarrays have the same size array, or (3) arrays of different sizes (e.g. jagged arrays).

48. Is the enumerator a value type or reference type?
When a variable is declared as an enumerator, it is a value type. The value is actually stored in the enumerator variable.

49. What is the foundation for C# applications?
This includes storing basic data, controlling the flow of the program, repeating pieces of code, and creating classes that can store both data and methods.

50. So, what have we learned so far?
These concepts are “single” concepts only, and have not yet been integrated with other concepts. They have been explained in isolated scenarios for clarity.

Note: Information Source: Teach Yourself C# in 21 Days, by Bradley Jones

Five Business Lessons from Bill Gates

Bill Gates enjoyed and also was challenged by his thirty-plus year tenure with Microsoft Corporation as its leading spokesperson, technologist, chairman, and advocate for innovative change. As he leaves Microsoft Corporation to spend more time with his philanthropic activities of the Gates Foundation, which is also co-managed by his wife Melinda, Bill Gates has left behind some profound lessons from which we can learn. This blog post specifically notes five lessons that Bill Gates has taught me, that apply especially to the world of business and possibilities.

Lesson One: It is possible to translate an idea into a physical reality. In the late 1970s, Bill Gates succeeded in getting IBM to enlist his services to develop and implement an operating system that could be used for its computers. So, Bill Gates translated the idea of the Disk Operating System into MS-DOS, which became a prominent Microsoft product for many years before Windows was finally released.

Lesson Two: It is possible to unite like-minded people in an entrepreneurial setting and turn this gathering into a long-lasting and profitable business. It is indeed ironic how the 1970s pictures of the Microsoft staff look like a bunch of flower children from the 1960s who possibly lived alternate lifestyles and entertained alternate philosophies of life. Indeed, this group laid the foundation for the incredible infrastructure of Microsoft which eventually hired more and more talented personnel in many aspects of the software business.

Lesson Three: It is possible to adapt to a changing market and adapt products to fill new and changing needs. Well, Microsoft did it. They dominated the desktop software platform for more than two decades, and then when the Internet became popular, Microsoft produced its Internet Explorer along with many other products to take on the World Wide Web. Today, if you check out Microsoft’s online documentation for its Developer Network, as an example, you will see only the very best in documentation for languages like Visual Basic Dot Net, C# Dot Net, among others.

Lesson Four: It is possible to improve products based upon user feedback. I worked for a short time for a company in Boston that emulated Microsoft in terms of working with customers and trying to meet all customer needs, and then some. Microsoft has indeed worked hard for more than three decades to ensure that users are able to accomplish their tasks and say positive things about their software.

Lesson Five: It is possible to bring an unknown product or idea to the market and eventually turn it into a publicly traded company where average people like you and me can buy the stock and enjoy dividends as well as capital appreciation. It took Microsoft more than a decade, but it eventually went public and many people have enjoyed immense profits because of Microsoft’s commitment to its stockholders and also products.

Microsoft Word 2007 Inside Out

As a Technical Writer, my favorite software tool is, without a question, Microsoft Word 2007. It is a phenomenal and incredible resource. It has evolved tremendously since its inception in the mid 1980s, and I remember my first experience with Microsoft Word when I had my MacIntosh Plus computer and I was typing my college assignments. Microsoft Word 2007 is so much more than a word processor. It is a means to producing high-quality documents that can be used for print media, the web, presentations, proposals, blogging, and all sorts of diverse text and graphical output. As I mentioned in yesterday’s post, I might seem a little bold on Microsoft right now, but products like Microsoft Word 2007 are what are contributing toward my professional success and unfoldment. In short, there is nothing more gratifying than having truly great resources at your disposal that enhance and uplift your work.

If you are a high school or college student or professional, I highly recommend you go to your local computer store or go online and order at least a legitimate copy of Microsoft Word 2007 for yourself. You will not be disappointed. Your teachers and/or boss will be truly impressed with the caliber of documents that you are producing. Here is a list, for your convenience, of the chapters that make up the Microsoft Press book Word 2007 Inside Out, which is the perfect means for you to master this software program.

Chapter One: Room to Create

Chapter Two: Document Creation

Chapter Three: Mastering Page Setup

Chapter Four: Formatting Documents

Chapter Five: Applying Themes

Chapter Six: Mastering Document Fundamentals

Chapter Seven: Honing Document Navigation Skills

Chapter Eight: Working with Text Tools

Chapter Nine: Refining Research Services and Reference Tools

Chapter Ten: Clarity and Structure

Chapter Eleven: Organizing Concepts in Tables

Chapter Twelve: Showcasing Data

Chapter Thirteen: Adding Visual Impact

Chapter Fourteen: Aligning Information and Formatting Paragraphs

Chapter Fifteen: Using Styles to Increase Formatting Power

Chapter Sixteen: Formatting Layouts

Chapter Seventeen: Borders and Shading

Chapter Eighteen: Sharing Documents & Collaboration

Chapter Nineteen: Using Markup Tools

Chapter Twenty: Addressing Document Protection

Chapter Twenty-One: Formatting Columns and Sections

Chapter Twenty-Two: Master Documents

Chapter Twenty-Three: Configuring References

Chapter Twenty-Four: Tables of Contents

Chapter Twenty-Five: Effective Indexes

Chapter Twenty-Six: Printing with Precision

Chapter Twenty-Seven: Blogging with Word 2007

Chapter Twenty-Eight: Mail Merges

Chapter Twenty-Nine: Content Controls

Chapter Thirty: Macros and VBA

10 Great Microsoft Programs

As much as I respect the open-source movement and applaud the many great open-source programs on the web, there is still my respect for companies which continue to produce ever-increasing quality through their products. Microsoft Corporation is no exception. The following are ten (10) Microsoft programs that you will definitely enjoy when you start working with them. Each product addresses unique user needs. Microsoft Word, Excel, PowerPoint, Access and Visio are for general office needs. Microsoft Accounting Express will help you to balance your checkbook, among other things. Microsoft Visual Studio Express will let you practice important programming skills. Microsoft Encarta will help you to revive your history and general academia skills. And, finally, Microsoft Internet Explorer will help you discover the World Wide Web (WWW).

Microsoft Windows Vista Operating System

Microsoft Word 2007

Microsoft Excel 2007

Microsoft PowerPoint 2007

Microsoft Access 2007

Microsoft Visio 2008

Microsoft Accounting Express

Microsoft Visual Studio Express Editions

Microsoft Encarta

Microsoft Internet Explorer 7

Book Review: Code by Charles Petzold

Today’s post is a short review of a classic book about computer programming written by the foremost software engineer who has ever worked at Microsoft Corporation: Charles Petzold. Back in 1994, I read Charles Petzold’s book Programming Windows, which contained alot of great source code examples for applications that ran in Windows 95. Back then, I was working with Microsoft C and SDK Version 7 and the first version of Visual C had not yet been produced by Microsoft. I had to actually build my programs in C in a DOS editor, compile all related files in DOS and then run the Windows application from the DOS command prompt. Ah, the old days of programming! That book was a great read and it was fascinating to see how Microsoft Windows 95 really worked.

However, there is another book by Charles Petzold that surpasses Programming Windows, and that book is Code: The Hidden Language of Computer Hardware and Software. This book (published in 2000) is a must read for anyone who really wants to understand the fundamentals of binary code and how essential elements of the computer go to work for us, producing amazing interfaces and also behind the scenes capabilities for data processing. So, whether you are a computer geek or a simple guy trying to compete in the highly-competitive computer industry, I encourage you to get at least a used copy of this book and study it well. Charles will reveal all of the inner workings of computers and the thought processes that are at the heart of all modern computing. This book has twenty-five (25) chapters, and each one will take you toward another transformative level of computing insight:

Chapter 1: Best Friends
Chapter 2: Codes and Combinations
Chapter 3: Braille and Binary Code
Chapter 4: Anatomy and a Flashlight
Chapter 5: Seeing Around Corners
Chapter 6: Telegraphs and Relays
Chapter 7: Our Ten Digits
Chapter 8: Alternatives to Ten
Chapter 9: Bit by Bit by Bit
Chapter 10: Logic and Switches
Chapter 11: Gates (not Bill)
Chapter 12: A Binary Adding Machine
Chapter 13: But What About Subtraction?
Chapter 14: Feedback and Flip-Flops
Chapter 15: Bytes and Hex
Chapter 16: An Assemblage of Memory
Chapter 17: Automation
Chapter 18: From Abaci to Chips
Chapter 19: Two Classic Microprocessors
Chapter 20: ASCII and a Cast of Characters
Chapter 21: Get on the Bus
Chapter 22: The Operating System
Chapter 23: Fixed Point, Floating Point
Chapter 24: Languages High and Low
Chapter 25: The Graphical Revolution

Thank you, Charles Petzold, for a truly incredible book!

Microsoft Visual C Sharp: Working with Strings

Working with Strings

Strings are special data types that can hold textual information. In the Microsoft Visual C# language, string is a keyword. It is simply a different name from the String class in the System namespace. Therefore, string has all the properties and methods of the String class. A value in a string cannot be modified.

String Methods

When string methods are called or when you make changes to a string, a new string is actually created. If you try to change a character in a string, you get an error. The following sample program tries to replace the 4th character of the str1 string ABCDEF with X, which cannot be done. Compiling this program should give you an error message about this.

//———————————————————-
using System;
class Str_Err
{
public static void Main ()
{
string str1 = “ABCDEF”;
str1[3] = ‘X’;
Console.WriteLine(str1);
}
}
//———————————————————

So, if strings cannot be modified, then is their usefulness limited? No, because string methods allow you to make string comparisons, which come in handy as you develop your program. A number of extremely useful methods can be used with string comparisons. The following list shows some of the more prominent string methods, along with descriptions of their use.

Static Methods

Compare - compares the values of two strings.

CompareOrdinal - compares the values of two strings without compensating for language or other internationalization issues.

Concat - concatenates (joins) two or more strings into one string.

Copy - creates a new string from an existing string.

Equals - compares two strings to determine whether they contain the same value. Returns true if the two values are equal. Otherwise, returns false.

Format - replaces format specifiers with their corresponding string values. Specifiers were covered earlier today.

Join - Concatenates two or more strings. A specified “separator string” is placed between each of the original strings.

Methods and Properties of Each Instance

Char – returns the character at a given location.

Clone – returns a copy of the stored string.

CompareTo – compares the value of the string with the value of another string. This method returns a negative number if this string is less than the compared string, 0 if equal, and a positive number if the value of this string is greater.

CopyTo – copies a portion of or all of a string to a new string or character array.

EndsWith – determines whether the end of the value stored in the string is equal to a string value. If they are equal, true is returned; otherwise, false is returned.

Equals – compares two strings to determine whether they contain the same value. Returns true if the two values are equal. Otherwise, false is returned.

IndexOf – returns the index (location) of the first match for a character or string. Returns -1 if the value is not found.

Insert – inserts a value into a string. This is done by returning a new string.

LastIndexOf – returns the index (location) of the last match for a character or a string. Returns -1 if the value is not found.

Length – returns the length of the value stored in the string. The length is equal to the length number of characters contained.

PadLeft – right-justifies the value of a string and then pads any remaining spaces with a specified character (or space).

PadRight – left-justifies the value of a string and then pads any remaining spaces with a specified character (or space).

Remove – deletes a specified number of characters from a specified location within a string.

Split – the opposite of join. Breaks a string into substrings based on a specified value. The specified value is used as a breaking point.

StartsWith – determines whether the value stored in a string starts with a specified character or set of characters. Returns true if there is a match and false if not. If specified character is null, true is also returned.

Substring – returns a substring from the original string starting at a specified location. The number of characters for the substring might also be specified but is not required.

ToCharArray – copies the characteristics in the current string value to a char array.

ToLower – returns a copy of the current value in all lowercase letters.

ToUpper – returns a copy of the current value in all uppercase letters.

Trim – removes copies of a specified string from the beginning and end of the current string.

TrimEnd – removes copies of a specified string from the end of the current string.

TrimStart – removes copies of a specified string from the beginning of the current string.

Building Strings

The StringBuilder class is provided in the System.Text namespace to create an object that can hold a string value that can be changed. An object created with the StringBuilder class operates similarly to a string. The difference is that methods of a StringBuilder can directly manipulate the value stored. The methods and properties for a StringBuilder object are displayed in the following list:

Append – appends an object to the end of the current StringBuilder.

AppendFormat – inserts objects into a string based on formatting specifiers.

Capacity – sets or gets the number of characters that can be held. Capacity can be increased up to the value of MaxCapacity.

Chars – sets or gets the character at a given index position using indexer notation.

EnsureCapacity – ensures that the capacity of StringBuilder is at least as big as a provided value. If the value is passed to EnsureCapacity, the value of the Capacity property is set to this new value. If MaxCapacity is less than the value passed, then an exception is thrown.

Questions? Drop me an e-mail at great.documents@gmail.com and I’ll do my very best to answer your question or concern. All The Best, Keith

Inspired Source: Learn C# in 21 Days by Bradley Jones

Microsoft Visual C#: Object Oriented Programming

There are four pillars or key-characteristics that make up an object-oriented language:

(1) Encapsulation

(2) Polymorphism

(3) Inheritance

(4) Reuse

Because Microsoft Visual C# is an object-oriented language, it is important to understand these concepts to truly harness the power and versatility of the language.

Encapsulation is the concept of making classes or “packages” that contain everything you need. In object-oriented programming, this means that you can create a class that stores all the variables that you need and all the routines to manipulate the data. You can create a “Triangle” class that stores information about triangles. This could include information about the three angles, the three sides, and more. By encapsulating a triangle, you allow the user to be oblivious to how the triangle works. The user needs to know only how to interact with the triangle. This provides a “shield” to the inner workings of the triangle. Any programs that you write that use the “Triangle” class should not need revisions of any kind.

Inheritance is the way in which individual instances of a class automatically carry with them all the attributes of the “umbrella” class over them. So, triangles, circles, squares, etc. all inherit characteristics of the “shapes” class which overshadows all of them.

Polymorphism means having the capability of assuming different forms. For example, different formulas for different shapes are needed to determine the areas of individual triangles, squares, or circles that are submitted to the program. Polymorphism is the way in which you can guarantee, for example, that all unique shapes will be treated as such and correctly united with the correct area formula (that is, per this example).

Reuse: when you create a class , you should be able to reuse it to create lots of objects. By using inheritance and some of the features in the above sections, you can create routines that can be used repeatedly in many programs and in many ways. By encapsulating functionality, you can create routines that have been tested and are proven to work. You won’t have to test the details of how the functionality works. Rather, you need only know that it works correctly.

Inspired Source: Learn C# in 21 Days by Bradley Jones

Microsoft Visual C Sharp: Lesson Two

The second topic that I would like to discuss is Program Flow. In Microsoft Visual C#, there are mechanisms by which you can successfully control your logic and the overall flow of the program. It is important to select the right method to control the direction of your program.

Method #1: If - Else Statements

Essentially, the else component of the if-else statement gives you the capability of having code that executes when the if statement’s conditions fail or are false. Also, only one of the two aspects of this statement actually execute. Both never execute, if set up correctly. So, this the beauty of this particular program flow is that if you have a simple fork in the logic, if-else allows the program to go via route A or route B quite easily.

Note: If-Else Statements can be increased to include else if statements as well. Nonetheless, when all listed paths for the program logic prove false, the final else is where the program continues on ahead.

Method #2: Switch Statements

Microsoft Visual C# provides yet another much easier way to modify program flow based on multiple values that are stored in a variable: with the switch statement. The format for this statement is as follows:

switch (value)
{
case result 1:
case result 2:
case result n:
default:
}

In this program flow, there is no condition. Rather, a value is used. This value can be the result of an expression or it can be a variable. This value is then compared to each of the values in each of the case statements until a match is found. If a match is not found, then the flow goes to the default case. If there is no default set-up, then the first statement following the switch statement is where the flow goes.

Method #3: Iteration Statements

When you want to repeat a segment of code in your application, Microsoft Visual C# provides a number of iteration (e.g. repetition) techniques. They are as follows: while, do, for, and foreach.

The basic logic of a while statement is as follows:

while (condition)
{
statement(s)
}

A while statement uses a conditional statement. If the condition is met (true), then the statement is executed. Otherwise, the statement(s) are not executed and the program flow goes to the next command following the while statement.

A do statement, however, first executes its statements. Then a while is presented with a condition. If the while condition is true, the program returns to the statements, otherwise, the program flow goes to the next line after the do….while. The syntax for a do statement is as follows:

Do
{
statement(s)
} while (condition);

The for statement, the third of the four iteration methods, consolidates steps into a much simpler format, as illustrated below:

for (initializer; condition; incrementor)
{
statement(s);
}

The initializer is executed when the for statement begins. Then, the condition statement is evaluated. Finally, the incrementor is generally used to increment a counter. After the incrementor is executed, the condition is again evaluated, and this happens as long as the incrementor is true and not maxed out. As long as the incrementor is not maxed out, the statement(s) in this program flow are read by the compiler and executed accordingly.

Lastly, the foreach statement iterates very much like the for statement. However, the foreach statement has a special purpose - it can loop through collections such as arrays.

Inspired Source: Learn C# in 21 Days by Bradley Jones

Microsoft Visual C Sharp: Lesson One

Even though I am a Technical Writer, from time to time I must learn new programming languages so that I can stay at the cutting edge of my profession. Currently, I work with a group of professionals that utilize Microsoft Visual C#. According to Microsoft Press, books about Visual C# were the leading sellers among programming books in 2006 and 2007. Also, if you investigate applications being built for both local desktops as well as the web, many of them have been built using Microsoft Visual C#. At some future point I also want to learn PHP, but right now, I believe there is a lot I can contribute to my professional experience by learning this emerging and dynamic object-oriented programming language developed by Microsoft. So, I hope you similarly benefit from these postings.

Today’s lesson is going to feature the very essentials of a Visual C# application: (1) Whitespace, (2) Visual C# keywords, (3) Literals, and (4) Identifiers.

Whitespace: Because C# compilers ignore whitespace, you should make liberal use of it to help format your code and make it readable to other programmers and managers besides yourself. Whitespace is the blank space put into a listing. Whitespace can consist of spaces, tabs, line feeds, and carriage returns.

Example: int radius = 10;

The same expression could be written as: int radius = 10 ;

The second line compiles exactly the same as the first line. So, Visual C# is able to intelligently interpret the white space and not see it as meaning anything extra. The only exception to this rule is that when you use double quotations, this is a fixed text string, so the compiler will not change this in any way. They way you enter the text is the way it appears in the compiled output.

Keywords: There are approximately seventy-six (76) keywords in the Microsoft Visual C# language. Keywords cannot be used outside their designated and functional purposes. The following list is a list of these seventy-six keywords with a brief definition:

abstract: a modifier that can be used to indicate that a class is to be used only as a base class to another class.

as: an operator used to perform conversations between compatible types. The value to the left of the operator is cast as the type on the right.

base: a keyword that enables values and types in a base class to be accessed.

bool: a logical data type that can be either true or false. Bool is equivalent to System.Boolean in the Microsoft .NET framework.

break: a program flow keyboard that enables programs control to exit a loop or a conditional block, such as “switch” or “if”.

byte: a data type that stores an unsigned integer in 1 byte. This is a value from 0 to 255. This keyword is equivalent to System.Byte in the Microsoft .NET framework.

case: a program flow keyword that defines a logical condition within a switch statement.

catch: part of the try-catch error-handling logic of a program. The catch blocks are used to specify exceptions to be handled and the code to be executed when such exceptions occur.

char: a data type that stores a single Unicode character in 2 bytes. This keyword is equivalent to System.Char in the Microsoft .NET framework.

checked: a program flow keyword that indicates that overflow-checking for integer-type arithmetic operations and conversions should occur.

class: a reference data type that can contain both data and method definitions. A class can contain constructors, constants, fields, methods, properties, indexers, operators, and nested types.

const: this is a modifier that is applied to a data member or variable. When used, the value of the data type is constant and, therefore, cannot be changed.

continue: this is a program flow keyword that enables program control to automatically go to the next iteration of a loop.

decimal: this is a data type that stores a floating-point number in 16 bytes. The precision of a decimal variable is better than that of the other floating point types. This makes it better for storing financial values. The suffixes m and M designate a decimal literal. This keyword is equivalent to System.Decimal in the Microsoft .NET framework.

default: this keyword is a label within a switch statement to which program flow goes when there is no matching case statement.

delegate: this keyword is a reference type that can receive a method based on a specified method signature. This signature of methods is based on the declaration of the delegate (similar to function pointers in languages such as C and C++).

do: this is a looping program flow construct that causes execution of a statement or block of statements until a condition at the end of the block evaluates to false.

double: this is a data type that stores a floating-point number in 8 bytes. The suffixes d and D designate a double literal. This keyword is equivalent to System.Double in the Microsoft .NET framework.

else: this is a conditional program flow statement that contains a statement or block of statements that is executed when a preceding if statement evaluates to false.

enum: this is a value data type that can store a number of predetermined constant values.

event: this is a keyword used to specify an event. The event keyword enables a delegate to be specified that can be called when an “event” occurs in a program.

explicit: this is a keyword used to declare an explicit conversion operator for a user-defined type.

extern: this is a modifier that indicates that a method is external and, thus, outside the C# code.

false: this is a boolean literal value. This keyword can also be used as an operator that can be overloaded.

finally: the finally block executes after the try block’s scope ends. It is generally used to clean up any resources allocated in the try block.

fixed: a keyword used within unmanaged code to lock a reference type in memory so that the garbage collector won’t move it.

float: this is a data type that stores a floating-point number in 4 bytes. The suffixes f and F designate a float literal. This keyword is equivalent to System.Single in the Microsoft .NET framework.

for: a program flow statement used for looping. This statement contains an initializer, a conditional, and an iterator. The statements within the for construct’s block execute until the conditional evaluates to false. The initializer is executed at the start of the for. The iterator is executed after each execution of the for statement’s statement block.

foreach: an iterative program flow construct that enables you to loop through a collection or array.

get: a special word used for creating an accessor that gets the value from a property. This is not a reserved word.

goto: this is a program flow constant that jumps program flow from the current location to a labeled location elsewhere in the program.

if: this is a program flow construct that executes a block of code when a condition evaluates to true.

implicit: this is a keyword used to declare a user-friendly defined type conversion operator that does not have to be specified (e.g. called implicitly).

in: this is a keyword used with the foreach keyword. This keyword identifies the collection or array that the foreach will loop through.

int: a data type that stores a signed integer in 4 bytes. The range of possible values is from -2,147,483,648 to 2,147,483,647. This keyword is equivalent to System.Int32 in the Microsoft .NET framework. Literal numbers with no suffix are of type int by default if the value fits within the given range for an int.

interface: this is a keyword used to declare a reference type that defines a set of members but does not declare them.

internal: this is an access modifier that enables a data type to be accessible only from within files in the same assembly.

is: this is an operator used to determine at runtime whether an object is a specified type.

lock: this is a keyword used to make a block of code critical. This section of code does not enable more than one thread to access it at a time.

long: this is a data type that stores a signed integer in 8 bytes. The range of possible values is from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. This keyword is equivalent to System.Int64 in the Microsoft .NET framework. The suffixes l and L designate a long literal.

namespace: this is a keyword that enables you to organize a number of types into a group. This is used to help preven name collisions and to make it easier to reference types.

new: this is an operator used to create an object. This is also used as a modifier to hide a member inherited from a base class.

null: this is a literal used to represent reference value points to nothing.

object: this is a type based on the System.Object. class in the .NET framework. All other types are derived from object.

operator: this is a keyword used to create or overload an operator’s functionality in a class or structure.

out: this is a parameter modifier that enables the parameter reference variable to be used to return a value from a method. The variable must be assigned a value in the method.

override: this is a keyword used to provide a new implementation of a method or property, which replaces a base class’s existing method or property with the same signature.

params: a parameter modifier that indicates that a variable number of values can be contained in the parameter. This modifier can be used only with the final parameter in a method’s parameter list.

partial: a potential future keyword used to indicate that the associated class is only partially defined in the current listing. This allows a single class to be broken across multiple source listings.

private: this is an access modifier that indicates that a method, property, or other member of a structure or class is accessible only within the same class or structure.

protected: this is an access modifier that indicates that a method, property, or other member of a class is accessible only within the same class or within classes that are derived from this class.

public: this is an access modifier that indicates that a method, property, or other member of a class or structure is accessible.

readonly: this is a data member modifier that indicates that after the initial assignment - either at the time of declaration or within the constructor - the value within the data member cannot be changed.

ref: this is a parameter modifier that indicates that changes to the parameter variable will also be reflected in the variable that was passed as the ref argument.

return: this is a keyword used to return a value from a method.

sbyte: this is a data type that stores a signed integer in 1 byte.

sealed: this is a modifier for classes that prevents you from deriving from the class.

set: this is a special word used for creating an accessor that sets the value in a property.

short: this is a data type that stores a signed integer in 2 bytes.

stackalloc: this is a keyword that is used to allocate a block of memory on the stack.

static: this is a modifier that is used to indicate that only a single value will be stored for the type.

string: this is a data type that stores Unicode characters. String is an alias for System.String in the Microsoft .NET framework.

struct: this is a value data type that can contain both data and method definitions. A structure can contain constructors, constants, fields, methods, properties, indexers, operators, and nested types.

switch: this is a program flow construct that changes program flow based on a value of a variable.

this: a keyword used within a non-static method that associates a variable with the current instance of a class or structure.

throw: a program flow statement that is used to throw an exception, which indicates that something abnormal has occurred. This is used with try and catch.

true: this is a boolean literal value. True can also be used as an operator.

try: this keyword is used for exception handling.

typeof: this is an operator that returns the data type of an object.

uint: this is a data type that stores an unsigned integer in 4 bytes.

ulong: this is a data type that stores an unsigned integer in 8 bytes.

unchecked: this is an operator or statement that can be used to indicate that overflow checking on integer data types should be ignored.

unsafe: a keyword used to identify code that is considered unsafe to execute in the managed environment. For example, unsafe should be used to wrap any code that uses pointers.

ushort: this is a data type that stores an unsigned integer in 2 bytes.

using: this is a keyword for creating an alias for a namespace. It can also be used to shortcut the need to use fully qualified names for types within a namespace.

value: the name of the variable being set by a set property accessor.

virtual: a modifier used on a method or property to indicate that the method or property can be overridden.

void: this is a keyword used in place of a type to indicate that no data type is used. In method declarations, void can be used to declare that no value is returned from the method.

where: this is a potential future keyword used to declare constraints on generics.

while: this is a looping program flow construct that causes execution of a statement or block of statements as long as a condition evaluates to true.

yield: this is a potential future keyword that is used within iterators to indicate a value that should be returned to a foreach statement. The yield keyword indicates where the foreach statement should continue on its next iteration.

Literals: these are straightforward hard-coded values. They “are what they are”. Simple as that.

Identifiers: is the name given to the class, and class-body is the code that makes up the class.

Here is a brief example:

class identifier
{
class-body;
}

Questions? Just shoot me an e-mail at great.documents@gmail.com.
I’ll try my very best to answer your question :) All The Best, Keith

(Inspired Source: Teach Yourself C# in 21 Days, by Bradley Jones)

The Software Development Cycle

The software development cycle is composed of six (6) major phases. As a Technical Writer, I usually end up writing about a software module after it has been tested, but that is not always the case. Many times architects or others in management will ask me to create a “dummy” document that will spell out the details of how the system is to look, at least from the GUI perspective. Also, architecture and management are interested in having quality specifications determined before programming actually starts. So, yes, Technical Writing and Software Documentation are usually the “caboose” of the software development cycle, however, there are always exceptions to the rule.

Phase One: Determining the problem to solve. All software systems have been designed to help the user solve a problem or accomplish a task. This is why we have today great programs like Microsoft Office and Microsoft Developer Studio, not to mention the many open-source alternatives that exist as well. Again, the most important step is this step because one needs to clearly define the reason behind developing a software module or system.

Phase Two: Architecture. Software architects design the specifications in which code must be written. Architects determine the platform, scope, size, and depth of programming and how all aspects of the system are to communicate. The program could be a simple desktop application or it could also be a sophisticated system with a front end GUI, intermediate code, and back-end database with tables and more.

Phase Three: Programming. Once architecture has established its parameters for the system, developers next begin to code the system. It is good for programmers to insert meaningful comments in their code so that different programmers can work on the same module. Also, system documentation can be generated from these comments to provide further support for the modules as they unfold in development.

Phase Four: Quality Assurance. After all programming is done, testing is the key to ensuring that the logic of the code is strong and that there are no bugs that will affect the user. Sometimes as a Technical Writer I have to help QA write testing scripts so that the integrity of the system is really determined accurately.

Phase Five: Documentation. As mentioned above, this is the usual time at which software is actually documented and presented in the written word. Today there are many great tools to create a nice user manual or on-line help manual for those using the software. There is nothing more frustrating than trying to use a software program and simply getting stuck. This is where the Tech. Writer comes in and saves the day.

Phase Six: Implementation and Customer Feedback. At this point, all bugs have been worked out and all documentation for the program is done. The application or system simply needs to be implemented and then the user can begin putting the program to use. After the user has really put the system to work, he or she can then contact the software vendor and give some meaningful feedback. As a Tech. Writer, I have also helped to create installation guides so that users successfully install the software on their computers, and in this way they can access all options available within the system successfully.