1 / 59

Custom Classes

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

etoile
Download Presentation

Custom Classes

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. D101 Software Development Custom Classes

  2. Content • Review • Last weeks stuff (FileIO, Methods) • Custom Classes • Assignment Details

  3. 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

  4. Review • Week 8 Practical

  5. Review • Set Box button • Calls the ChangeBox method • Passes in three strings as parameters • Height, Width, and Colour cbxColour.Text also works

  6. Review • ChangeBox Method

  7. Review

  8. Review • Week 8 Practical

  9. Review • Save Button • Calls the SaveSetting method • Passes in three strings as parameters • Height, Width, Colour

  10. Review • SaveSetting Method

  11. Review

  12. Review • Week 8 Practical

  13. Review • Load Presets Button • Clear the list box (gets rid of old presets) • Calls the LoadPresets method • There are zero parameters

  14. 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

  15. Review • Use Preset Button

  16. 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]

  17. Review

  18. Custom Classes Making our own objects

  19. 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

  20. 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

  21. 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

  22. Custom Classes • How to create a custom class • Click the ‘Add New Item’ button

  23. Custom Classes • Select ‘Class’

  24. Custom Classes • Give the class a Name then click Add

  25. Custom Classes • Visual Studio will generate an empty class

  26. Custom Classes • Now in our Form1.cs code we can see our PresetBox class in the intellisense

  27. 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

  28. Custom Classes • Declaring class variables

  29. 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

  30. Custom Classes • The Constructor

  31. Custom Classes • Now in our Form1.cs code we can declare and initialise PresetBox objects

  32. Custom Classes • We can create as many PresetBox objects as we want

  33. 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

  34. 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

  35. Custom Classes • Creating Properties

  36. Custom Classes • Creating Properties (click ok)

  37. Custom Classes • Creating Properties (Click Apply)

  38. Custom Classes • Creating Properties

  39. Custom Classes • Creating Properties (to be tidy shift the property underneath the constructor)

  40. Custom Classes • Now we can access the BoxHeight property in the Form1.cs code (it’s a public property)

  41. Custom Classes • We can change the value of the BoxHeight using simple assignment

  42. Custom Classes • Or we can get the value from a text box

  43. 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

  44. Custom Classes • With name variableand Property

  45. Custom Classes

  46. Custom Classes • Now we can access all of the Properties in the Form1.cs code

  47. Custom Classes • All Properties Set

  48. 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

  49. Custom Classes • NewSaveSettings method

  50. Custom Classes • Finally we can call the NewSaveSettings method and pass in our PresetBox object

More Related