Presentation is loading. Please wait.

Presentation is loading. Please wait.

These materials where developed by Martin Schray

Similar presentations


Presentation on theme: "These materials where developed by Martin Schray"— Presentation transcript:

1 Martin Schray http://www.martinschray.com martin@martinschray.com
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would like to suggest corrections or enhancements please drop me a note at Martin Schray

2 VB .NET - Using Objects & basics about classes
Copyright © Martin Schray

3 VB .NET Classes Uses of a class
they can be used to define abstract data types Data is protected or hidden inside a class Class can be used to derive another class New classes derived from existing class will inherit the data and methods already defined public class MyClass // class members End Class

4 Declaring a class [attributes] [Class Modifiers] class ClassName
[inherits SuperClass] [implements Interface] //list of variables and methods declarations End Class

5 Class Access Modifiers (page x)
Public Optional. Public access. There are no restrictions on the use of public entities. Private Optional. Private access. A private entity is accessible only within its declaration context, including any nested entities. Protected (only applies to class members) Optional. Protected access. They are accessible only from within their own class or from a derived class. Protected access can be specified only on members of classes (including inner classes). It is not a superset of friend access. Friend Optional. Friend access. Only within the program that contains the entity declaration. Classes that do not specify an access modifier are declared as Friend by default. Protected Friend Optional. Entities declared with the Protected Friend modifiers have the union of protected and friend accessibility.

6 Other Class Modifiers Shadows (only applies to class members)
Optional. Indicates that this class shadows an identically named programming element in a base class. You can shadow any kind of declared element with any other kind. A shadowed element is unavailable in the derived class that shadows it MustInherit Optional. Indicates that non-shared members of the Class can only be accessed via derived classes. Instances of must-inherit classes cannot be created. NotInheritable Optional. Indicates that the Class is a class from which no further inheritance is allowed.

7 Class Modifiers Only one modifier from each of the following groups can be used in a method declaration public, private, friend, protected friend <empty – gives you the default of friend> mustInherit, notIheritable

8 Class example public class point private x,y as integer
public sub move(dx as Integer, dy as integer) x += dx; y += dy; End Sub end class

9 Class Terms All variables and functions of a class are called members of the class Member functions of a class are also called methods of the class or simply methods A object is not created until it is instantiated with the new keyword

10 Initializing Instance Variables
Instance variables are initialized automatically Instance variables can also be assigned a default value

11 New Subroutine (Constructors)
New Methods are member functions which are automatically executed when a class is instantiated The method has the name new ?New are invoked after initial default members are initialize ?No argument constructor is provided when no other constructor are provided

12 New Subroutine (Constructors)
There can multiple constructors (as long a signature differs)

13 Functions (return a value)
A class’s method typically contains the code that understands and manipulates an object’s state. Syntax for method declaration follows [ <attrlist> ] [{ Overloads | Overrides | Overridable | NotOverridable | MustOverride | Shadows | Shared }] [{ Public | Protected | Friend | Protected Friend | Private }] Function name[(arglist)] [ As type ] [ Implements interface.definedname ] [ statements ] [ Exit Function ] [ statements ] End Function

14 Function Declaration Method declaration perform three main tasks:
determine who may call the method (public, private…) determine what the method may receive as arguments (parameters) determine what and if the method returns VB. Net does not allow a variable number of arguments like c++

15 Subroutine (no return value)
Like a function, but with NO return value [ <attrlist> ] [{ Overloads | Overrides | Overridable | NotOverridable | MustOverride | Shadows | Shared }] [{ Public | Protected | Friend | Protected Friend | Private }] Sub name [(arglist)] [ Implements interface.definedname ] [ statements ] [ Exit Sub ] [ statements ] End Sub

16 Sub Declaration Method declaration perform three main tasks:
determine who may call the method (public, private…) determine what the method may receive as arguments (parameters) NO RETURN TYPE VB. Net does not allow a variable number of arguments like c++

17 Function and Sub Modifiers
Public Optional. Public access. There are no restrictions on the accessibility of public procedures. Protected Optional. Protected access. They are accessible only from within their own class or from a derived class. Protected access can be specified only on members of classes. It is not a superset of friend access. Friend Optional. Procedures declared with the Friend keyword have friend access. They are accessible from within the program that contains their declaration and from anywhere else in the same assembly. Protected Friend Optional. Protected Friend keywords have the union of protected and friend access. They can be used by code in the same assembly, as well as by code in derived classes. Protected friend access can be specified only on members of classes. Private Optional. Private access. They are accessible only from within their declaration context, including from members of any nested types such as procedures.

18 Function & sub Modifiers
Overloads Optional. This Sub procedure overloads one or more procedures defined with the same name in a base class. The argument list in this declaration must be different from the argument list of every overloaded procedure. Overrides Optional. Indicates that this Sub procedure overrides an identically named procedure in a base class.

19 Function and Sub Modifiers
Overridable Optional. Indicates that this Sub procedure can be overridden by an identically named procedure in a derived class. NotOverridable Optional. Indicates that this Sub procedure cannot be overridden in a derived class. MustOverride Optional. Indicates that this Sub procedure is not implemented in this class and must be implemented in a derived class for that class to be creatable. Shadows Optional. A shadowed element is unavailable in the derived class that shadows it.

20 Method Modifiers Only one modifier from each of the following groups can be used in a method declaration public, protected, private, friend, protected friend<empty> Overloads or shadows Overrideable, noteoverridable, Mustoverride

21 Hello Class public class Hello ‘ class level variables go here
private shared count as interger = 0 private shared const msg as String = “I have been called this many times ”; public sub new() // constuctor count++; // increment count by one End Sub public sub Message() System.Console.writeln(msg & count); end sub End Class

22 Testing Classes Module Module1 Sub Main() Dim c As New FirstClass()
FirstClass.SharedTest() c.test() End Sub End Module Public Class FirstClass Public Sub test() System.Console.WriteLine("test") Public Shared Sub SharedTest() System.Console.WriteLine("Shared test") End Class


Download ppt "These materials where developed by Martin Schray"

Similar presentations


Ads by Google