Constructors
Download
1 / 12

Constructors - PowerPoint PPT Presentation


  • 117 Views
  • Uploaded on

Constructors. Constructors. You got plenty of experience using constructors in the Marching Band program. A constructor is the subroutine which creates objects from a class. Every class has a default constructor which takes no parameters– New().

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Constructors' - margie


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Constructors1
Constructors

  • You got plenty of experience using constructors in the Marching Band program.

  • A constructor is the subroutine which creates objects from a class.

  • Every class has a default constructor which takes no parameters– New().

  • This default constructor is there even if you don’t see it.


Threedpoint
ThreeDPoint

  • In the class definition, there is no Sub New.

  • However, I can still call the default constructor like this:


Creating a constructor
Creating a Constructor

  • However, we can create our own constructor.

  • There are two main reasons for creating your own constructor:

    • To initialize the value of variables using parameters; and

    • To create any objects that the current object will need; that is, calling constructors of other classes. Here’s the Rank class’s constructor from Marching Band:

  • Notice that it

    • Initializes the mStepSize variable;

    • Creates the 10 members of the rank by calling BandMember’s constructor.


Creating a constructor1
Creating a Constructor

  • Here’s a simple constructor for the ThreeDPoint class. It simply initializes the variables.


Constructors in action
Constructors in Action

  • To understand how to declare variables and create objects using constructors,

  • View the ConstructorsInAction video.


Overloads
Overloads

  • VB allows functions and subs (including constructors) to be overloaded.

  • This means that you can have two or more procedures with the same name.

  • They just have to differ in the number and/or type of parameters.

  • Overloads are used widely in the built-in VB.NET classes (you’ll see many in the graphics routines), but you can create your own overloads as well.



Tostring
ToString

  • Older versions of VB, including Visual Basic for Applications in Excel, do not have a “ToString” function.

  • VBA is a loosely-typed language that does all sorts of datatype conversions in the background for you (sometimes well, sometimes not).

  • VB.NET, the descendant of VBA (VB 6), is a strongly typed language, which means that functions, subs and properties can require a particular parameter type.

  • If a String parameter type is required you can put ToString on the end of a variable or expression to convert it to a string.


Tostring1
ToString

  • For example: Notice that when I try to put a numeric value into a Text property, I get an error:

  • ToString is the cure:

  • (Note: I added the extra blank lines so the error message wouldn’t cover up the line above. This doesn’t mean that I now approve of lots of blank lines!)


Tostring2
ToString

  • The standard datatypes (even String) have built-in ToString methods.

  • ToString can even be applies to expressions:

  • However, the classes that you create will have a default ToString method that’s pretty useless: It just displays the name of the class.

  • And this can be annoying when you start adding objects of your own classes to ListBoxes and ComboBoxes!


Using tostring to format numbers
Using ToString to Format Numbers

  • ToString can be used to format numbers, like this:


ad