Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp - Your Visual Blueprint For Building .NET Applications (2002) [eng].pdf
Скачиваний:
36
Добавлен:
16.08.2013
Размер:
9.71 Mб
Скачать

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

Соседние файлы в предмете Программирование на C++