Search VB 2010

Tuesday, April 12, 2011

OOP VB .Net Toturials and Examples - دروس فى ال أو أو بى

We discussed OOP concepts and started a project called ''Email Validation'' using the built-in-function IndexOf

In addition to the previous project, we will add New Form and we want now to repeat the previous SUB to Validate the Email on the new Form, to do so, we will need to copy the whole code and paste it to the new Form, now what if we wanted to add 20 New Form that require Email Validations for each Form of the 20 ... What would we do then ?! The answer is simple :
We shall use the OOP concepts to work this problem out ....

Creating SUB

We will create SUN [Validate_Email()] so we can validate Email in whole project without the need to copy and paste the same procedure 20 times for each Form.

Sub ValidateEmail ()

صورة رقم (3)
يحتوى هذا الاجراء على نفس الكود المراد تكراره , أى اننا سنقوم بعمل Cut للكود من مكانه و لصقه فالاجراء الجديد و التعويض عن الاكواد باسم الاجراء فقط كما بالصورة رقم 3
قم بتجربة البرنامج ستجد أن نفس النتيجه ظهرت .
الان سنقوم ببعض التغييرات على الـ Sub حيث سنقوم ببناء الـ Sub على متغير و يكون بهذا الشكل .
صوره رقم (4)
 Sub ValidateEmail (ByVal Txt As TextBox)
 الجمله التى بين الاقواس (ByVal Txt as TextBox) تسمى Parameter و سنقوم بتعديل الكود الموجود بالـ Sub حيث سنستبدل الــ Controls من نوع TextBox بالمتغير Txt و ايضا فى الحدث المسؤل عن تنفيذ الــ Sub  سنلاحظ وجود خط أحمر معناه أن الاجراء اصح ناقص (غير صحيح) حيث ينقصه المتغير المبنى عليه Txt و بما أننا نقوم بعمل Validate للــ TextBox2 فاننا سنضع بين الاقواس فى الــ Sub كلمة TextBox2 كما موضح بالصورة رقم 4.
قم بتجربة البرنامج سيعطيك نفس النتيجه .
الان سنقوم بالتعديل مره ثالثه فى الاكواد حيث سنستبدل المتغير Txt من نوع TextBox بالمتغير Txt1 من نوع String بهذا الشكل.
Sub ValidateEmail (ByVal Txt1 As String)
صورة رقم (5)
أى اننا نريد التعويض عن المتغير النصى هذه المره , أى اننا سنعوض عن TextBox2.Text بالكامل فى الاجراء بدلا من TextBox2 و سنقوم بتغيير الكود حيث سنستبدل Txt.Text بالكامل بــ Txt فقط .... كما بالصورة رقم 5 .
مثال أخر لتوضيح عمل الاجراءات Sub
الفرق بين الاجراءات المبنية على متغيرات اجبارية و المبنية على متغيرات اختياريه .
المتغيرات الاجبارية هى التى يجب ان تذكر عند كتابة الاجراء .
المتغيرات الاختياريه يمكن كتابتها و يمكن التغاضى عنها .
سنقوم الان بعمل فورم ثالث Form3 فى نفس المشروع المفتوح EmailValidation و سنقوم بعمل اجراء Test بهذا الشكل
Sub Test (ByVal X As Integer, Optional ByVal Y As Integer = 10)
الاجراء الموضح أمام حضرتك عباره عن عدد (2) Arguements الاول (X) و الثانى (Y) و نوعهم (Integer) أى متغير رقمى .

صورة رقم (6)
المتغير الثانى يحتوى على كلمة Optional و ذلك معناه أنه عندما التعويض عن الاجراء Test فانه من الممكن تجاهل قيمة (Y) حيث أنها اختياريه و لكن ستكون قيمتها دائما 10 اذا تم تجاهلها.
قم بكتابة المثال الموجود بالصورة رقم (6) حيث Form3 بها فقط Button1 و الكود سينفذ من خلال الحدث Button1_Click
ستلاحظ ان الكود يتم تنفيذه على الرغم من عدم كتابة المتغير الاختيارى و لكن لا يعمل بدون الاجبارى X ..
ملحوظة : المتغيرات الاختيارية تكتب فى منطقة الــ Parameters فى النهايه دائما (لا تنسى) مثال :
إجراء صيحيح
Sub Test(ByVal X As Integer , ByVal Y As String , ByVal L As Boolean , Optional ByVal N as Integer)
اجراء خاطىء
Sub Test(ByVal X As Integer , ByVal Y As String , Optional ByVal L As Boolean , ByVal N as Integer)
اجراء خاطىء
Sub Test(Optional ByVal X As Integer , ByVal Y As String , ByVal L As Boolean , ByVal N as Integer)
لاحظ ايضا لم نستخدم Public أو Private .... و ذلك لأننا كنا نعمل على مستوى الفورم فقط و بالتالى استخدامهم غير ضرورى.

Visual Basic Online Course - OOP

Visual Basic Online Course - OOP

Visual Basic 2010 OOP Course
Visual Basic 2010 OOP
OOP Introduction
The original versions of Microsoft® Visual Basic® provided a mechanism for defining data structures in a user-defined type (UDT). A UDT encapsulates the data, but not the processing associated with that data. Processing was defined in global standard modules, often called BAS modules because of their .bas extension.

The release of Visual Basic 4 dawned a new age for Visual Basic developers. Visual Basic took its first steps toward becoming an object-oriented programming (OOP) language by providing object-oriented features such as class modules. 

A class module defines data as properties and the processing associated with that data as methods. By defining a class for each business entity, encapsulating data in properties and processing in methods, Visual Basic developers had object-based development.

As Visual Basic evolved from version 4 to version 6, Visual Basic developers expanded their knowledge of OO 'Object-Oriented' to include component-based development (CBD) techniques. With CBD, Visual Basic developers could build complete three-tiered applications for Microsoft Windows® and the Web. This type of development was so common that Microsoft provided a design pattern known as the Microsoft DNA architecture.

Visual Basic .NET provides another leap in Visual Basic development capabilities and features and provides for true object-oriented programming, as detailed in this article.


For a programming language to be a true OOP language, the language must meet the following criteria:

    Abstraction—Abstraction manages the complexities of a business problem by allowing you to identify a set of objects involved with that business problem.

    Encapsulation—Encapsulation hides the internal implementation of an abstraction within the particular object.

    Polymorphism—Polymorphism provides for multiple implementations of the same method. For example, different objects can have a Save method, each of which perform different processing.

    Inheritance—The excitement of Visual Basic .NET lies in inheritance. Visual Basic 5 introduced the concept of interface inheritance, which allows you to reuse the interface of a class, but not its implementation. Visual Basic .NET provides for true implementation inheritance whereby you can reuse the implementation of a class.

Now let's look at doing object-oriented programming in .NET.

OOP in Visual Basic .NET

Visual Basic .NET is not Visual Basic 6 with inheritance tacked onto it. Rather, Visual Basic .NET has been entirely rewritten to be fully object-oriented. In fact, everything in Visual Basic .NET can be treated as an object. Yes, even your strings and integers can be accessed as objects in Visual Basic .NET ............... Source WikiPedia

1) What's OOP ?
- Objects Oriented Programming
- It's the logic of programming

2) What does it do ?
- It helps the programmer to understand codes "where the code line came from - where is it going?" and enables him to be creative and able to create objects and controls .
3) OOP has 3 major concepts
  1. Inheritance
  2. Polymorphism
  3. Encapsulation
OOP Lesson Index /
  1. Classes Intro .
  2. Classes Studying .
  3. Sub & Function Concepts .(1 - 2)
  4. Property Programming
  5. Overloading - Inheritance
  6. Inheritance & Access Modifiers
  7. Implementations of OOP Concepts
  8. Make Your Own TextBox
1) Classes Intro
Classes are sections of Code but superior to any other code in the Forms ... let's take an example.
- in our example we will try to make an application that makes sure that the TextBox writes only Email format (I.e. : UserName@EmailServerName.Extension), ain't that represents the most common Email format used? ... OK.

Let's start a new Windows Form Application Project from Visual Basic 2010. Our project will aim to notify the user whether an Email format was provided in the TextBox control placed on the form or not. We shall call this project Email-Format-Validation.
  • Project Design [GUI] 
Visual Basic 2010 OOP course
Visual Basic Online Course OOP - Classes
As you can see the Project design is so simple :
Name : Form1
Name : Label1
Text : Name
Name : Label2
Text : E-mail
Name : TxtName
Name : TxtEmail
Name : BtnValid
Text : Validate E-mail
Now, what does it mean (Make sure the user writes E-mail address format correctly or in Email format)??
- Means we want the application to Check the TextBox.Text contents and determines Weather the User wrote (@ and .) in it, because this is what the common Email Address format looks like ,right?!
OK, so we write this block of codes in the Click Event In the BtnValid Control

What did we write ?
We created two variables (X,Y) as Integers, because we shall refer to the place where the (@) is, by using X variable and the and the place where the (.) is, by using Y variable. OK?!

The line where X = TxtEmail.Text.IndexOf("@") means that X = the IndexOf of the text in TxtEmail where IndexOf is a function in VB 2010 comes with the Text Property Of the TextBox Control, and it locates a character's position (Zero based index) within the Text in a TextBox, and it's called a (Built-in Function), OK?
Again, the line of code means (Locate the position of the (@) Character within the property Text in the Control TxtEmail, Please) ...
The line where it says, If X >= 1 Then means, if our user typed (@), then X "The position" must be bigger than or equals to 1 , why not (0) ??? 

Because (Ahmed) = (0 1 2 3 4) [and that is called a Zero based index, Ahmed has 5 elements starting A=0 and ends with D=4] also Emails don't start with (@) so the position (X) must be in the second letter (1) or in later position, so (if the user wrote (@) then checked for the (.) symbol, Y also represents the position of the (.) symbol but (@) can not come after (.), so we must make sure of that, and this is why we repeated IndexOf function, we used another property of the IndexOf Function.
Y = TextBox2.Text.IndexOf(".", X)
Again ... the above line means : Locate the (.) symbol that comes after the (@) .
The line If Y < 1, Then means that if the user wrote the (.) at the first letter Zero position then the application will generate a message box, then End If to end the IF Statement that belongs to locating the @ symbol.
MsgBox ("Invalid E-mail Address")
End If
Else the (@) at the (0) position
MsgBox ("Invalid E-mail Address")
End If 
End checking for (@) Locations it is after the (0) position already .
End Sub
Visual Basic OOP [Classes Intro] Homework :
(1) Please Design this Project On your own and notice how Email forms must be exactly typed as conditioned, and why (-1) shows sometimes !!??

(2) Where to write a message box that tells the user the the Email format is correct ?
Next Lesson - OOP Classes further Look

Here are some online Visual Basic lessons and courses :

Visual Basic Courses - Share