Home Classroom C# .NET C# .NET Part 2

C# .NET Part 2

952

First Code of the Series.

During our last session we got an idea about the .NET Framework and how to install it. During this session we’ll start off with our 1st coding example. This would show how easy it is to code with C#.NET. And I hope you’ll enjoy it a lot.

1. When you select “Windows Form” from “New Project” section, the following interface would appear.

 

Figure 1: Windows Application Form

 

On Right hand side a side screen could be seen, it has a Window called “Solutions Explorer” it includes all the current files related to the project we created a few minutes ago. Let’s called that project as “Digit”. Go to “Programs.cs” in that list and remove the code in it. We’ll get back to those removed code lines later. And enter the following…

 

Figure 2: First code of the Series

 

2. Now Run the Program. Now you’ll see a Message Box displayed.

Figure 3: Output Message

 

In the example given above,

“System” is a large Library containing pieces of code, as the developer you don’t have to type this code; you can include them just by importing the “System” package. These codes are built into the C#.

“Namespace” can be considered as two things. The first one is it acts as a collection of Libraries. And on the other hand it helps to omit naming problems in classes. It is impossible to have two classes with the same name, but they could exist in two different namespaces.

“Main” method is a very important aspect in a C# program. Execution of the program starts from there.

public – meaning that we can access the main method from everywhere in the current program.

static- meaning that it doesn’t manipulate any data.

void- meaning that the Main method is not expected to return a value.

This is a very Simple program and includes the basic features in a C# program. Feel free to move around the IDE and try out various options included. That’s the best option to get a very good idea about .NET IDE as well as C#.

Now let us concentrate on some C# programming techniques. Let me tell you that C# is an Object Oriented Language. Object Oriented Programming is a programming technique where it considers all the building blocks in programs as objects.

 

Understanding Objects and Properties

Let’s dive into the world of Object Oriented Programming. It is based on Objects. In more technical terms an object is a programming structure that encapsulates data and functionality as a single unit and for which the only public access is through the programming structure’s interfaces (properties, methods, and events). We’ll discuss about these terms in details later on.

In C# components like Buttons, Textboxes, etc. are considered as objects. Just like ordinary objects that we find out in the real world. Let’s consider about something real. Let’s consider about pets. Dogs and cats are definitely different entities (objects), but they both fit into the category of Pet objects. Similarly, text boxes and buttons are each unique types of objects, but they’re both considered control objects.

Objects have properties and behaviors. Properties, in another word attributes describe the nature of the object. All the objects from the same object category have the same list properties. But the property value could change from one to another. For an example dogs could have a same property, sex property, etc.

Here it is in more descriptive manner.

 

Figure 4: Properties of a Dog Object

 

In programming these property values are hidden. But they could be retrieved by using Getter and Setter matters. Here is how we can set property values.

{Object name}. {Property name}

This could be equal to the value that we set. Ex: The Text property of a Textbox (txtName) can be changed by this. txtName.Text = “Address”;

But fortunately in C#.NET we don’t have to code these values to each and every object. Values of a certain object could be changed using the Properties Window

Figure 5: Properties Window for a Windows Form

 

Understanding Methods

In addition to Properties Objects have Methods. Unlike properties ,methods (Behaviors) can perform certain tasks. Let’s discuss about this using more simpler example.

Figure 6: Methods of a Dog Object

 

Methods too could be triggered using the same way which is used to call Properties.

{Object Name}. {Methods Name}

Example: Eat method of a Dog can be called like this way. For a dog called ‘rovy’

rovy.eat ();

Let’s look at a sample code to see this in a more clear view. This would create a small and the data which is entered would be displayed in a Message box.

1. Design Data Entry form using C#.NET. The output you get is seeing below. By double clicking on the button or Pressing the Events option from ‘Solutions Explorer’, you can get the click event of the button.

  

Figure 7: Sample Data Entry Form and Output

The code related to above example is here.

2. The code related to above example is here.

In this the import statements (System namespaces) are not included; they’ll be added by the IDE, while you are typing the code. This code section is to understand the Methods and Properties. Furthermore some concepts here are not yet covered during this series. But later on we will put our attention into them as well. But for now let’s concentrate on these areas.

This code segment starts with the class statement. Class is a very important section in any programming language. Class is the blueprint of objects, we’ll be discussing about classes in detail later on. On the code next comes the constructor, it has the same name as the class, but with the exception of certain things. This too needs to be discussed in detail.

Next it is the method invoked for clicking the button. The method name is ‘btnView_Click’. This method consists all the action which needs to be triggered when the button is clicked. As you can see when the button is clicked two arguments are sent to the method. Method is a piece of code segment which could be used to call another set code in the program.

Methods are much like events, but rather than being executed by a user interacting with a form or control, methods are executed when called by a code statement. Two types of code procedures are used in Visual C#:

  • Methods that return a value
  • Methods that do not return a value

This method has ‘void’ as its return type, which says that this method doesn’t return a data type. Apart from this a method could contain any variable type as its return type. The content in side a method are surrounded by two curly brackets (‘{}’). When there is a return type keyword ‘return’ must be used with the variable name that you going to return. This should be added at the end of the method. If not the compiler will display as error message.

Method calls in Visual C# must always have parentheses. Sometimes they’ll be empty, but at other times they’ll contain data to pass to the method. Methods are generally used to perform an action using an object, such as saving or deleting a record in a database.

Variables are temporary memory locations which are used to store values. Variables have lot of types, these could be used depending on the type of data that you need to store. In here we have used ‘string’ as the data type and we have used three string variables for this example. What we have done is that the value we got from the (get method) from the textbox property is sent to a string variable and then it is stored there for future reference, in our case to be displayed in the Message box.

‘name = txtName.Text;’ In here ‘Text’ property of the textbox, the value which was entered by the user is assigned to the variable name. Same thing is done with the other textbox prroperties as well. This is jut a small demonstration, you can can go trought eh ‘properties Window’ of a Textbox and findout about all the available properties. Properties, on the other hand, are used to get and set attributes of the object. One way to tell in code whether a statement is a property reference or method call is that a method call will have a set of parentheses after it.

Same could be done with other GUI objects like Buttons, Labels, Combo boxes,etc.. Next those values which were stored in variable would be displayed in a Message Box. The escape Characters (‘\n’) which were used here are used to indicate that the next variable value should be displayed in a new line. When the user runs the program, enters the values and presses the ‘View’ button the details which were entered in the textboxes would be displayed in Message box. This example was used to clarify the use of properties of objects in this case Textbox objects and methods of objects (the click method of the button is evoked).

This is a very simple example, but these are the basic components and concepts used in almost all the complex systems. So it is vital to have a pretty good understanding about objects their properties and methods that they contain. In our next session we’ll start to concentrate on some really important aspects in C#. Those are variables. Till then have a nice time.

 

Comments

comments

Lakpriya Kottahachchi is an Undergraduate at the Faculty of Information Technology, University of Moratuwa. He enjoys programming with Java and C#.

NO COMMENTS

Leave a Reply