You are on page 1of 5

Muruch’s C#

CHAPTER 3 - How to code [pg 58 - 90]

 An introduction to coding
o the process of creating objects from classes is called
instantiation.
o Members of an object include properties, methods, and
events that defined by classes and are used by objects.
 Properties – refers to the object’s characteristics and
data associated with the object.
 Methods – these determine the operations that can be
performed by the object.
 Events – represents signals sent by the object to your
application that something has happened that can be
responded to. For example, a “Button control” object
generates an event called “Click” if the user clicks the
button. Then the application can respond by running a
“Method” to handle the “Click” event.


o Classes and object concepts
 An object is a self-contained unit that combines code
and data. In other words, an object can also be referred
to as an instantiation of a class. For example, objects
may include forms and controls (Button, Label).
 A class is the code that defines the characteristics of an
object. In other words, a class can be called a template
for an object.
o When you use a form designer, VS automatically generates
C# code that creates a new class based on the Form class.
Then, when you run the project, a form object is instantiated
from the new class.
o Similarly, when you add a control to a form, VS automatically
generates C# code in the class for the form that instantiates
a control object.

o you often will need to refer to the properties, methods, and


events of the form’s objects, but to do that you type the
name of the object, a period and the name of the member.
 For example, syntax for referring to a member of a
class or object:
 ClassName.MemberName and
objectName.MemberName
 Statements that refer to properties
 txtTotal.Text = “10”;
o this assigns a string holding the number 10
to the “Text property” of the text box named
txtTotal.
 txtTotal.ReadOnly = true;
o assigns the true value to the ReadOnly
property of the text box named txtTotal.
 Statements that refer to methods
 txtMonthlyInvestment.Focus();
o uses the focus method to move the focus to
the text box named txtMonthlyInvestment.
 this.Close();
o uses the close method to close the form that
contains the statement. In this example, this
is a keyword that is used to refer to the
current instance of the class.
 Statements that refer to an event
 btnExit.Click
o refers to the click event of a button named
btnExit.
o Additionally, you also can refer to some of the properties and
methods of a class directly from that class.
o A static member is a property or method that you can refer
to directly from a class.
o The “this” keyword is used instead of the name of the form.

o Windows forms applications work by responding to events


that occur on objects.
o To indicate how an application should respond to an event,
you code an event handler, which is a special type of method
that handles the event.
o Event wiring refers to the process where to connect the event
handler to the event, VS automatically generates a
statement that wires the event to the event handler.


 How to add code to a form
o Coding rules
 Use exact capitalization for all keywords, class names,
object names, variable names, etc.
 End each statement with semicolon.
 Each block of code must be enclosed in braces {}.
 Use blank lines before and after groups of related
statements.
o A method written in a readable style:

 How to refactor code


o Refactoring in code refers to the situation where if you want
to change code in one place, you need to make further
changes in throughout the whole code.
o In other words, the process of revising and restructuring
existing code is known as refactoring, and this includes inline
renaming.

CHAPTER 4 – How to work with numeric and string data


o \

You might also like