1 / 18

Structure and Class Members

Structure and Class Members. (c) Allan C. Milne School of Computing & Creative Technologies University of Abertay dundee. Last updated 20 th October 2005. Agenda. Fields Properties Methods Static & instance members Events Delegates. Introduction.

stevie
Download Presentation

Structure and Class Members

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structureand ClassMembers (c) Allan C. Milne School of Computing & Creative Technologies University of Abertay dundee Last updated 20th October 2005

  2. Agenda • Fields • Properties • Methods • Static & instance members • Events • Delegates

  3. Introduction • This presentation introduces the different types of components that can be used as members of a class (or structure). • We will focus here on members of classes (the reference type) rather than of structures (the value type) although much of the content is applicable to both. • Members also have individual accessibility (public, private or protected) that defines their accessibility to other programming entities; this will only be covered here in passing.

  4. Members • A class or structure is constructed from its individual members. • Members are named and typed entities that define both • the public class behaviour (to users of the class), and • the private state and mechanisms that form the internal workings of the class. • Members can be fields, properties, methods or events.

  5. Fields (Attributes) … • are used to store values that normally represent the internal state of an object. • have a name and a type. • should be defined as private; • hidden from outside users of the class, and • only accessible by other class members. • often have properties associated with them to provide a public exposure.

  6. Properties … • are supported by the CLR through metadata annotations. • expose the state of objects of the class. • are usually defined as public; • accessible from outside the class, and • together with methods, provide the external behaviour of the class. • can be read-only (get), write-only (set) or read-write (get and set).

  7. A Field & Property Example class Staff { private String name; private int room; private String email; public String Name { get { return name; } } public int Room { get { return room; } set { room = value; } } public String Email { set { email = value; } } public Staff (String name) { this.name = name; } } // using the class Staff me; me = new Staff ("Allan"); String myName = me.Name; me.Room = 4519; int myRoom = me.Room; me.Email = "fred@here"; see Programs\Staff1.cs

  8. Properties also … • have a name, type with get and/or set accessor methods. • are accessed as if they were public fields for reading and writing. • represent logical fields of the class; • there is no requirement for any relationship between an actual field and a property, • get property values can be computed, and • set property operations may result in some general change of state.

  9. Public Properties Vs Puclic fields • Provide protection through control over the read-write operations available. • Assures data integrity through allowing appropriate validation to be performed on write operations. • Contributes to removing versioning issues through distinguishing between logical fields and physical implementation.

  10. Methods … • define the behaviour of the class. • form a contract between the class and users of the class. • have (i.e. the contract is defined by) • a name, • a parameter list (possibly empty), and • a return type. • are called by users of the class by supplying the method name and an actual parameter list.

  11. Method Accessibility • Most methods will be defined as public; • accessible outside the class • provide the external behaviour of the class. • Methods can also be defined as private; • hidden from outside users • only accessible by other class members • provide any internal structuring of functionality required.

  12. Static Members … • have only one copy at run-time. • are invoked on the class type itself. • can be called at any time (even if no class objects exist). class Eg1 { public static void Hi () { Console.WriteLine ("A static Hi"); } } … … … Eg1.Hi();

  13. Instance Members … • have a copy in each object of the class. • are invoked on an explicit object. • can only be called when an object of the class type has been created. class Eg2 { public void Hi () { Console.WriteLine ("An instance Hi"); } } … … … Eg2 obj = new Eg2(); obj.Hi();

  14. Events … • expose the processing of asynchronous changes in an object. • are supported directly in the CLR through generated methods and associated metadata. • have a name and a type. • types specify the method signature that users must supply for the event-handler method; this is a delegate type.

  15. Event-Handler Methods … • are user-defined methods that implement the handling of an event. • are also known as callback methods or listeners. • must match the method signature specified in the event type (the delegate type). • are the methods that will be called when the associated event is raised. • must be registered with an event through attaching an appropriate delegate.

  16. Delegates … • are an object-oriented implementation of a type-safe function pointer. • are supported by the CLR. • are used to specify callback methods for events. • are also used in many other non-event contexts.

  17. Outline of Event Usage • Define an appropriate delegate type for the event-handling (callback) method. • Define a structure or class that exposes a member of an event type. • Create a value of the class type. • Attach event-handling (callback or listener) methods for the event. • Raise the event.

  18. Event Example public delegate void Handler (int n); class EventEg1 { public event Handler MyEvent; public void DoSomething (int n) { MyEvent (n); } } class EventEg1App { public static void Main () { EventEg1 myEg = new EventEg1(); myEg.MyEvent += new Handler(Cat); myEg.DoSomething (3); } private static void Cat (int n) { Console.WriteLine ("{0} cats", n); } } seePrograms\EventEg1.cs

More Related