
- •maranGraphics
- •CREDITS
- •ACKNOWLEDGMENTS
- •ABOUT THE AUTHORS
- •AUTHORS’ ACKNOWLEDGMENTS
- •TABLE OF CONTENTS
- •HOW TO USE THIS BOOK
- •INTRODUCTION TO C#
- •START VISUAL STUDIO .NET
- •OPEN A NEW C# PROJECT
- •OPEN A C# WEB PROJECT
- •SET JSCRIPT .NET AS THE DEFAULT SCRIPT LANGUAGE
- •EXPLORE THE CLASS VIEW WINDOW
- •VIEW THE CONTENTS WINDOW
- •GET HELP USING THE INDEX WINDOW
- •SEARCH FOR HELP
- •ADD COMPONENTS FROM THE TOOLBOX
- •ADD A TASK TO THE TASK LIST
- •CHANGE FORM PROPERTIES IN THE PROPERTIES WINDOW
- •ADD A CUSTOM TOOLBAR
- •DELETE A TOOLBAR
- •CHANGE THE VISUAL STUDIO ENVIRONMENT
- •MANAGE OPEN WINDOWS
- •OPEN A PROJECT
- •VIEW THE MAIN METHOD
- •COMBINE PROGRAM TYPES
- •ADD REFERENCE TYPES
- •ADD OPERATORS
- •INSERT ATTRIBUTES
- •ENTER CLASSES
- •ADD COMMENTS TO CODE
- •WRITE YOUR FIRST PROGRAM
- •ENTER XML DOCUMENTATION
- •ACCESS DOCUMENTATION
- •LOG A BUG REPORT
- •VIEW INFORMATION ABOUT C# BUILDING BLOCKS
- •PROGRAM CLASSES
- •ADD A CLASS
- •EMPLOY CLASS INHERITANCE
- •PROGRAM INSTANCE CONSTRUCTORS
- •INSERT DESTRUCTORS
- •PROGRAM STRUCTS
- •DISPLAY HEAP AND STACK INFORMATION
- •FIND TYPE INFORMATION
- •PROGRAM CONSTANT EXPRESSIONS
- •SPECIFY VALUE TYPES
- •PROGRAM NUMERIC TYPES
- •PROGRAM THE BOOLEAN TYPE
- •DECLARE REFERENCE TYPES
- •ENTER REFERENCE TYPE DECLARATIONS
- •CONVERT VALUE TYPES TO REFERENCE TYPES
- •PROGRAM POINTER TYPES
- •INSERT THE VOID TYPE
- •ADD INTERFACE PROPERTIES
- •ADD AN INTERFACE INDEX
- •VIEW INFORMATION ABOUT METHODS
- •ADD A METHOD
- •ADD STATIC METHODS
- •INCLUDE NON-STATIC METHODS
- •ENTER DELEGATES
- •PROGRAM EVENTS
- •ADD AN EVENT-HANDLING METHOD
- •VIEW INFORMATION ABOUT ARRAYS
- •ENTER SINGLE-DIMENSIONAL ARRAYS
- •ADD MULTIDIMENSIONAL ARRAYS
- •PROGRAM ARRAY-OF-ARRAYS
- •ITERATE THROUGH ARRAY ELEMENTS
- •SORT ARRAYS
- •SEARCH ARRAYS
- •IMPLEMENT A COLLECTIONS CLASS
- •PROGRAM STRUCTS
- •ADD AN INDEXER
- •INCLUDE ENUMERATIONS
- •CREATE STRING LITERALS AND VARIABLES
- •ASSIGN VALUES TO STRINGS
- •CONCATENATE STRINGS
- •COMPARE STRINGS
- •SEARCH FOR SUBSTRINGS
- •REPLACE CHARACTERS
- •EXTRACT SUBSTRINGS
- •CHANGE THE CHARACTER CASE
- •TRIM SPACES
- •REMOVE CHARACTERS
- •SPLIT A STRING
- •JOIN STRINGS
- •PAD STRINGS
- •VIEW INFORMATION ABOUT PROPERTIES
- •COMPARE PROPERTIES AND INDEXERS
- •PROGRAM PROPERTY ACCESSORS
- •DECLARE ABSTRACT PROPERTIES
- •INCLUDE PROPERTIES ON INTERFACES
- •VIEW INFORMATION ABOUT WINDOWS FORMS
- •ADD A WINDOWS FORM IN THE WINDOWS FORM DESIGNER
- •SET THE FORM TYPE
- •CHOOSE THE STARTUP WINDOWS FORM
- •CREATE A MODAL FORM
- •LAYOUT A FORM
- •SET A FORM LOCATION
- •CHANGE FORM PROPERTIES
- •CREATE A TRANSPARENT FORM
- •AN INTRODUCTION TO WEB FORMS AND CONTROLS
- •CREATE AN ASP.NET WEB SITE
- •CREATE A WEB FORM
- •ADD SERVER CONTROLS TO A WEB FORM
- •READ AND CHANGE PROPERTIES FROM OBJECTS ON A WEB FORM
- •USING SERVER-SIDE COMPONENTS ON WEB FORMS
- •INTRODUCING DATA ACCESS WITH ADO.NET
- •DISPLAY DATA WITH THE DATAGRID CONTROL
- •CONFIGURE THE DATAGRID CONTROL
- •INSERT DATA INTO A SQL DATABASE
- •UPDATE DATA FROM A SQL DATABASE
- •DELETE DATA FROM A SQL DATABASE
- •EXECUTE A STORED PROCEDURE IN A SQL DATABASE
- •READ XML FROM A FILE
- •SAVE XML TO A FILE
- •QUERY XML WITH XPATH
- •APPLY XSL TO XML
- •INTRODUCTION TO DISTRIBUTED APPLICATIONS
- •CREATE AN APPLICATION WITH PRIVATE ASSEMBLIES
- •CREATE AN APPLICATION WITH SHARED ASSEMBLIES
- •VERSION A SHARED ASSEMBLY
- •CONFIGURE A CLIENT FOR A VERSIONED ASSEMBLY
- •CREATE A WEB SERVICE
- •USING A WEB SERVICE
- •INTRODUCTION TO EXCEPTION HANDLING
- •THROWING AN EXCEPTION
- •HANDLING EXCEPTIONS WITH THE CATCH BLOCK
- •USING THE FINALLY BLOCK
- •WRITE ERRORS TO THE APPLICATION LOG
- •BASIC EXAMPLES
- •WHAT’S ON THE CD-ROM
- •USING THE E-VERSION OF THIS BOOK
- •INDEX
- •Symbols & Numbers

C#
PAD STRINGS
You may sometimes need to have your strings appear a certain way on your screen. For example, you may want a string of numbers right-aligned so that the
numbers can be read more easily. The String Class enables you to leftand right-align strings by using the
String.PadRight and String.PadLeft methods, respectively.
You can left-align strings by using the String.PadRight method. The String.PadRight method adds spaces on the right for a specific total length of your string. When you use the String.PadRight method, you specify how long the string needs to be with a totalWidth argument. This width value minus the number of characters in your string
determines how many white spaces the method should add to the end of the string. If you do not want white spaces, you can replace the white space by providing a width and a padding character.
The String.PadLeft method is a mirror image of the String.PadRight method. In the case of the String.PadLeft method, you can enter the width of the string and the method will add the number of white spaces by subtracting the string width from the length of the string. You can also pad your flush-right string with characters instead of white spaces by adding the char argument and the padding character.
PAD STRINGS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
‹ Rename the class name to ˇ Add the Main function. |
Á Create a string variable |
|||||||
|
|
|
|
|
|||||||||
|
|
|
|
the |
Pad. |
that is initialized with a |
|||||||
|
|
|
|
|
|
› Save the file. |
greeting. |
||||||
|
|
|
|
namespace to |
|
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|

WORKING WITH STRINGS 8
You can pad with other characters besides white space.
TYPE THIS:
using System;
namespace StringSample
{
///<summary>
///Take the same Greeting and pad with a '-' instead of
///a space. Do this with taking the string length plus
///padding amount.
///</summary>
class Pad {
static void Main()
{string sGreeting =
"Welcome to 'My Personal Photo Album'"; string sGreetingPadded;
sGreetingPadded = sGreeting.PadLeft ((sGreeting.Length + 5),char.Parse("-"));
sGreetingPadded = sGreetingPadded.PadRight ((sGreetingPadded.Length + 5),char.Parse("-"));
Console.WriteLine(sGreetingPadded);
}
}
}
RESULT:
C:\>csc PadStrings_ai.cs
C:\> PadStrings_ai.exe
--- Welcome to 'My Personal Photo Album'---
C:\>
‡ Use the PadLeft function to indent the greeting 41 spaces.
° Use the PadLeft function to indent the greeting five spaces.
· Set a debug stop.
‚ Press F5 to save, build, and run the console application.
■ A greeting appears that is adjusted from the left.
■ A greeting appears that is adjusted from the left.
183

C#
ABOUT PROPERTIES
a struct or name of a structs,
statements to example, if temperatures Fahrenheit that
user so your
The declaration structure of a property is very similar to other types of declarations you have already learned about. The structure includes any optional attributes and modifiers, one of the four types of access modifiers (public, private, protected, or internal), and the property name. After you enter the property name, you enter the accessor information and any interface type, both of which are described later in this chapter.
A property is very similar to a field except that C# does not classify the property as a variable as it does with a field. The property instead reads and writes variables to objects that accesses your class, struct, or interface.
VIEW INFORMATION ABOUT PROPERTIES
Solution Explo... |
ution Explo... |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
⁄ Click Start Programs |
|
|
|
|
¤ Click Help. |
|
|
|
|
■ The Start page appears. |
‹ Click Index. |
||||||||
|
|||||||||
Microsoft Visual Studio .NET |
|
|
|
|
|
|
|
|
|
7.0 Microsoft Visual Studio |
|
|
|
|
|
|
|
|
|
.NET 7.0. |
|
|
|
|
|
|
|
|
184

ACCESSING PROPERTIES 9
You should remain cognizant of accessibility issues with properties. That is, the property must be as accessible as another property or another type. For example, if you have two properties A and B, property A must be as accessible in all areas of your program where property B is accessible.
To take this example further, suppose that property A does not have an accessibility modifier (thus the default accessibility is protected) and property B has the public accessibility modifier. Property B appears within subclass B, and subclass B inherits from parent class A. Parent class A contains property A.
Because property B and property A do not have the same accessibility types, the MDE window will catch this error and you will not be able to compile your program until you change one or both of the properties so they have the same accessibility type.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
■ The Index window |
|
|
|
|
|
||
|
|
|
|
|
|||
|
|
|
|
|
|||
|
|
|
|
|
|||
Á Click adding in C# under |
|||||||
appears. |
properties in the topics list. |
||||||
|
› Type properties in the |
|
|
|
|
|
|
|
|
|
|
|
|
||
Look for field. |
|
|
|
|
|
||
|
|
ˇ Click to select Visual |
|
|
|
|
|
|
|
|
|
|
|
|
|
C# in the Filtered by drop- |
|
|
|
|
|
||
down list. |
|
|
|
|
|
■ The C# Property Wizard page appears so you can load sample array files and see how they work.
185