Basics

Exlr ST Object-oriented languages have three import concepts for working with objects. To work with an object, you will need to know what properties, methods, and events are. This chapter covers these three concepts. Properties describe attributes of your object. For example, you could represent a pencil as an object. Some of the properties of this object would be its length, color, and thickness of lead.

Properties are a natural extension of fields. They are both named members with associated types, and the syntax for accessing fields and properties is the same. In general, you should expose properties instead of public fields. With properties you have better control over the storage and access of the information stored. You will want to use Get and Set accessors to control reading and writing to properties.

AccessProperties.cs - Notepad

File Edil Format Help using System;

public class AccessProperties public static void Main(StringD args) {

String strDateTimeStamp;

-IstrDateTimeStamp - DateTime.Now.ToStringQ

IConsole WriteLinef'The Date and Time is " + strDateTimeStamp),|

—H Access the Now property from the DateTime class that is part of the "System" Framework class.

Microsoft Windows 2000 [Uersion S.00.2195] <C> Copyright 1985-2000 Microsoft Corp.

C:\Documents and Settings\Administrator>cd C:\CSharp

C:\Csharp>csc AccessProperties.cs

Microsoft Uisual Ctt Compiler Version 7.00.9148 [CLR version 6151

Copyright <C) Microsoft Corp 2000. fill rights reserved.

0 Send this value to the console.

Microsoft Windows 2000 [Uersion S.00.2195] <C> Copyright 1985-2000 Microsoft Corp.

C:\Documents and Settings\Administrator>cd C:\CSharp

C:\Csharp>csc AccessProperties.cs

Microsoft Uisual Ctt Compiler Version 7.00.9148 [CLR version 6151

Copyright <C) Microsoft Corp 2000. fill rights reserved.

0 Send this value to the console.

Note: See page 35 for more information on saving a file.

~, Compile and run the program.

■ The program displays the message about the date and time.

M ST

Was this article helpful?

0 0

Post a comment