Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Ghalib Private University

Visual Programming

By: M.Dawood Saddiqi


Class Implementation
• Example Defining Class in C#
• Lets student class having only two-fields i.e r_no, and Name can be
created as;
• Once a class has been defined then we can create object as:
class_name identifier=new class_name();
Student s1=new Student();
Here s1 is a objects of type student.
Class Implementation
Project
• On Form write the following Code
• public int getwin()

Project
• {
• Count c = new Count();
• return win;
• public Form1()
• }
• {
• class Count • public void IncrementSpin()
• InitializeComponent();
• { • {
• }
• private int spin; • ++spin;
• private void button1_Click(object sender, EventArgs e)
• private int win; • }
• {
• public Count() • public void IncrementWin()
• Random rd = new Random();
• { • {
• label1.Text = rd.Next(10).ToString();
• spin = 0; • ++win;
• label2.Text = rd.Next(10).ToString();
• win = 0; • }
• label3.Text = rd.Next(10).ToString();
• } • public double Percent()
• c.IncrementSpin();
• public int getspin() • {
• if ((int.Parse(label1.Text)) == 7 || (int.Parse(label2.Text)) == 7 || (int.Parse(label3.Text)) == 7)
• { • double per=0.0;
• {
• return spin; • if(win!=0)
• c.IncrementWin();
• } • per=(double)spin/win;
• }
• • return per;
• label4.Text = c.Percent().ToString();
• }
• label5.Text = c.getspin().ToString();
• }
• label6.Text = c.getwin().ToString();
Partial Class
• A class can contain a number of methods, fields, and constructors, as
well as other items discussed in later chapters.
• A highly functional class can become quite large With C#, you can
split the source code for a class into separate files so that you can
organize the definition of a large class into smaller, easier to manage
pieces.
• Example.
Understanding static Methods and Data

• In C#, all methods must be declared inside a class However,


• if you declare a method or a field as static, you can call the method
or access the field by using the name of the class No instance is
required.
• When you define a static method, it does not have access to any
instance fields defined for the class; it can use only fields that are
marked as static
Creating a Shared Field
• you can also use the static keyword when defining a field With this feature,
you can create a single field that is shared among all objects created from a
single class (Nonstatic fields are local to each instance of an object ).
• public static int NumObjects = 0;
• Example Count the number of object.
• Note: Keep in mind that static methods are also called class methods However,
static fields aren’t usually called class fields; they’re just called static fields (or
sometimes static variables)
Static Classes
• Another feature of the C# language is the ability to declare a class as static .
• A static class can contain only static members (All objects that you create using
the class share a single copy of these members ).
• The purpose of a static class is purely to act as a holder of utility methods and
fields.
• A static class cannot contain any instance data or methods, and it does not
make sense to try to create an object from a static class by using the new
operator.
Understanding Values Type and
References Type
• When you declare a variable as a value type, the compiler
generates code that allocates a block of memory big enough
to hold a corresponding value.
• For example, declaring an int variable causes the compiler
to allocate 4 bytes of memory (32 bits) , A statement that
assigns a value (such as 42) to the int causes the value to be
copied into this block of memory.
Understanding Values Type and
References Type

• int, foat, double, and char are called value


types.
Reference Types

• Reference types hold references to blocks of memory.


• When you declare a Circle variable, the compiler does not generate code that
allocates a block of memory big enough to hold a Circle; all it does is allot a
small piece of memory that can potentially hold the address of (or a reference
to) another block of memory containing a Circle (An address specifies the
location of an item in memory ).
• The memory for the actual Circle object is allocated only when the new
keyword is used to create the object .
• A class is an example of a reference type
Understanding Null Values and Nullable Types

• When you declare a variable, it is always a good idea to


initialize it With value types, it is common to see code such
as this:

• Remember that to initialize a reference variable such as a


class, you can create a new instance of the class and assign
the reference variable to the new object, like this:
Conti…
• But what if you don’t actually want to create a new object.

• After assigning c to copy, what happens to the original Circle object


with a radius of 99 that you used to initialize copy? Nothing refers
to it anymore .
• In this situation, the runtime can reclaim the memory by performing
an operation known as garbage collection. But for now garbage
collection is a potentially time consuming operation.
Conti…
• In C#, you can assign the null value to any reference
variable The null value simply means that the variable
does not refer to an object in memory You can use it like
this:
Using Nullable Types
• The null value is useful for initializing reference types, but null is itself a
reference, and you cannot assign it to a value type.

• C# defines a modifier that you can use to declare that a variable is a nullable
value type A nullable value type behaves in a similar manner to the original
value type, but you can assign the null value to it.
• use the question mark (?) to indicate that a value type is nullable, like this
• Now you can test in the same way as a reference type whether a nullable
variable contains null:
Using Nullable Types
• You can assign an expression of the appropriate
value type directly to a nullable variable.

• You should note that the converse is not true You


cannot assign a nullable value to an ordinary value
type variable.
Using ref and out Parameters
• You can assign an expression of the appropriate
value type directly to a nullable variable.

• You should note that the converse is not true You


cannot assign a nullable value to an ordinary value
type variable.

You might also like