590 likes | 823 Views
D101 Software Development. Custom Classes. Content. Review Last weeks stuff ( FileIO , Methods) Custom Classes Assignment Details. Learning Outcomes. Describe the five (5) parts of a custom class: Name Variables Constructor Properties Methods Implement a custom class
E N D
D101 Software Development Custom Classes
Content • Review • Last weeks stuff (FileIO, Methods) • Custom Classes • Assignment Details
Learning Outcomes • Describe the five (5) parts of a custom class: • Name • Variables • Constructor • Properties • Methods • Implement a custom class • Describe the difference between public and private access modifiers • Explain the use of a custom class
Review • Week 8 Practical
Review • Set Box button • Calls the ChangeBox method • Passes in three strings as parameters • Height, Width, and Colour cbxColour.Text also works
Review • ChangeBox Method
Review • Week 8 Practical
Review • Save Button • Calls the SaveSetting method • Passes in three strings as parameters • Height, Width, Colour
Review • SaveSetting Method
Review • Week 8 Practical
Review • Load Presets Button • Clear the list box (gets rid of old presets) • Calls the LoadPresets method • There are zero parameters
Review • Load Presets Method Declare and Initialise a StreamReader object While we’re not at the end of the file Read a line from the file Then add it to the list box
Review • Use Preset Button
Review • Declare a string called preset, set it to equal the selected item from the list box • E.g. 10,50,Orange • Split the string preset at every comma and put the resulting strings into an array called parameters • E.g. { “10” “50” “Orange” } • Call the ChangeBox method and pass in the parameters array elements • E.g. parameters[0], parameters[1], parameters[2]
Custom Classes Making our own objects
First Consider This • What if our box presets had more than three bits of information? • How would we display this information in the list box? • What if there were 13 bits of information? • Would we want a SaveSetting method with a 13 parameter signature? Ideally, we would want to encapsulate all that information into a single object
Custom Classes • So far we’ve used • Buttons • Labels • Text Boxes • List Boxes • Combo Boxes • Picture Boxes • Group Boxes • Check Boxes • Strings • Ints • Booleans • StreamReaders • StreamWriters • Colors • … These are all predefined classes that are built in to C#i.e. someone else made them up
Custom Classes • C# also allows us to make up our own classes • E.g. • Instead of passing around individual height, width and colour values for a preset box, we could write a PresetBox class which will let us keep those values in a single object • myPresetBox.Height • myPresetBox.Width • myPresetBox.Colour
Custom Classes • How to create a custom class • Click the ‘Add New Item’ button
Custom Classes • Select ‘Class’
Custom Classes • Give the class a Name then click Add
Custom Classes • Visual Studio will generate an empty class
Custom Classes • Now in our Form1.cs code we can see our PresetBox class in the intellisense
Custom Classes • The next step is to declare the variables that our PresetBox objects will hold • E.g. • The Box Height • The Box Width • The Box Colour
Custom Classes • Declaring class variables
Custom Classes • The next step is to write the class constructor • The constructor is called (like a method) and it’s initialises the class variables with default values • The constructor looks like a method except it does not have a return type and it’s name is always the name of the class
Custom Classes • The Constructor
Custom Classes • Now in our Form1.cs code we can declare and initialise PresetBox objects
Custom Classes • We can create as many PresetBox objects as we want
Custom Classes • Although we have declared variables for our PresetBox objects, the variables are private so we can’t see them in the Form1.cs code
Custom Classes • The next step is to create publicly accessible Properties for each of the private class variables • This is so we can set and get the boxHeight, boxWidth and boxColour values in the code • A quick way to do this is: Right click on a private variable > Refactor > Encapsulate Field
Custom Classes • Creating Properties
Custom Classes • Creating Properties (click ok)
Custom Classes • Creating Properties (Click Apply)
Custom Classes • Creating Properties
Custom Classes • Creating Properties (to be tidy shift the property underneath the constructor)
Custom Classes • Now we can access the BoxHeight property in the Form1.cs code (it’s a public property)
Custom Classes • We can change the value of the BoxHeight using simple assignment
Custom Classes • Or we can get the value from a text box
Custom Classes • We can repeat the steps for the BoxWidth variable & the BoxColour variable (and any other variables we may want to add) • Maybe we want to adda name variable too
Custom Classes • With name variableand Property
Custom Classes • Now we can access all of the Properties in the Form1.cs code
Custom Classes • All Properties Set
Custom Classes • Now we can pass the single PresetBox object into a method as a single object • E.g. make a new SaveSettings method with a single PresetBox object as a parameter
Custom Classes • NewSaveSettings method
Custom Classes • Finally we can call the NewSaveSettings method and pass in our PresetBox object