Search this blog

You can search for all the topics in our blog using our google custom search now.

Search the blog

Loading

Wednesday, June 30, 2010

Learning regular expression step by step tutorials: Step 3

Free Microsoft MCTS 70-536 Examination Training and Preparation


If you haven't watched the previous post, then check out the related topics

Character Classes or Sets

If you want to tell the regex engine to match with one of the several characters, then you can form Character Classes/Sets. To form a character set, we just need to place the characters in square brackets like this : [a-z], this will include all the characters from a to z
or like this : [ghi], this will include characters specified in the square bracket(g,h and i)

Negated Character Classes

If you want to tell the regex  not to match with any of the given range/set of characters, then you can use negated character classes. To form a Negated Character Class you need to add ^(A Caret sign) after opening the square bracket. Some of the examples are:

[^ghi] character class will match with any character other than g,h and i.
[^a-z] character class will match with any characters other than between a to z.

Related Topics

Step 1 Learning regular expressions,


Step 2 Learning regular expressions

Short hand Character Classes


In previous post we have seen that when you use "backslash" with literals other than special characters because "backslash" in combination with other literals creates a regex token which has a special meaning in itself.
For example:
when "d " is used in combination with "/", it creates a regex token "/d" which matches all digits from 0-9.



The above regex token is an example of Short-hand Character Class. A short hand character class has been developed to include character classes which are used more often. Other Examples are:

"\w" stands for word character which can be defined as [a-zA-Z0-9_]
"\s" stands for white space character which can be defined as [ \t\r\n](space, tab, line break)


Repeating Character Classes 

You can repeat a character class using ?,* or + operators. If you use "+" operator with a character class like this: [a-c]+ 

The above expressions tells the regex engine to match with any characters between a,b,c one or more times. Which means [a-c]+ matches with jukibyha since input(jukibyha) has "b" and "a".

However most of the times  you want to check if there are any combinations of "aaa","aba","bbc","baa","cca" etc patterns in the given input. To do that we need to use backreferences to check for repeated matched characters. If you didn't understand what i mean by repeated matched characters, then here is the following explanation:

If my regular expression is [0-3] and the input is 568935413, then  our regex engine finds the first match "3" and then stops even though "1" is also a valid match. So now "3" is a Matched character by the regex engine.
If you want regex engine to keep searching for matched character "3" then you use backreferences to tell regex engine to save the matched characters in its memory.If you use back references, regex engine will find the first match "3", save it in the memory and it will keep searching for another "3" until end of the given input.

So if you want to look for repeated matched characters, you can create a regular expression like this
([0-3])\1+.This will tell the regex engine to look for repeated matched characters for one or more time.

 


 
Check for next tutorial on regular expressions
Regards
Sameer Shaik
Free Microsoft MCTS 70-536 Examination Training and Preparation

Wednesday, June 23, 2010

Learning regular expression step by step tutorials: Step 2

If you haven't watched the previous post, then below is the link:
Step 1 Learning regular expressions

Now we will going little further with regular expressions. In previous tutorial, we have seen that matches were done for very simple expression. In practicality we will be looking for more complex expressions to get the exact match. To provide us with these capabilities, Special Characters/Meta Characters are available.

There are 11 characters with special meanings:
1. The opening square bracket [
2. The backslash \
3. The caret ^
4. The dollar sign $
5. The period or dot .
6. The vertical bar or pipe symbol |
7. The question mark ?
8. The asterisk or star *
9. The plus sign +
10. The opening round bracket (
11. The closing round bracket ).

Since the above characters have a special meaning, we cannot find a match  for "2+2" in a string "2+2 equals 4". When you use these special characters for ex: plus sign in  "2+2" it will match with 22,222,2222 but not just 2+2,2,22+2 etc. '+' matches the preceding character one or more times.

If you would like special characters to be treated like literals in the previous example. That is you want "2+2" regex to match with "2+2 equals 4". Then you need to escape these special characters with "backslash(\)" like this: 2\+2 will ignore the "+" sign as literal which will now match with the given string.

Point to remember:  When you use "backslash" with literals other than special characters because "backslash" in combination with other literals creates a regex token which has a special meaning in itself.
For example:
when "d " is used in combination with "/", it creates a regex token "/d" which matches all digits from 0-9.

The following example is simple demonstration of what we've learnt,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace RegexStep2
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "2+2 equals 4";
            string regex = @"2+2";
            if (Regex.IsMatch(s, regex))
            {
                Console.WriteLine("Given string " + s + " matches with regular expression " + regex);
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("Given string " + s + " does not match with regular expression " + regex);
                Console.ReadKey();

            }

        }
    }
}

Non Printable Characters:
We can also use special characters to put non printable characters in our regular expressions.Some examples of non printable characters are as follows:
1. "\t" to match a TAB character
2. "\n" to match a Line Feed.
3. "\r" to match a Carriage Return.
etc etc
 




Regards
Sameer Shaik

Learning regular expression step by step tutorials: Step 1

What is a regular expression?
It is a set of characters that can be compared to a string to determine whether the string meets specified format requirements.

The most basic form of regular expression is using Literal Characters. For ex: "t" will match any first occurrence in "mcts tutorials blog", which is simply "t" in "mcts", if you notice there is a second match too, but we haven't developed regular expression to evaluate any occurrence after the first.

Create a new console application and double click on program.cs file. Add the namespace System.Text.RegularExpressions.Regex.IsMatch() is the method which is going to evaluate your regular expression i.e. It is the going to compare the given string with the regular expression. If the match is successful then it is going to return true or mismatch results in false.






Your code should look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RegexStep1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "Literal Characters";
            string regex = "tr";
            if (Regex.IsMatch(s, regex))
            {
                Console.WriteLine("Given string " + s + " matches with regular expression " + regex);
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("Given string " + s + " does not match with regular expression " + regex);
                Console.ReadKey();

            }
          
        }
    }
}


Please check out the video for detailed explanation on this example.

Sunday, June 20, 2010

Retrieving data from threads C# example




To understand how to retrieve data from threads, you need to have complete understanding of how delegates work. If you are unsure about how delegates works, you can check out my previous post on delegates on the link below:
Delegates tutorial
The above post provides you video tutorial on the same topic, so do check it out.
Step-1:

Now, to retrieve data from a thread, you need to create a method that accepts return results as a parameter and to that we need to create a delegate for the method.
For example, Imagine a thread is passing salary of an employee to a method called displaySalary. The method definition of displaySalary is as follows:

public static void displaySalary(int salary)
        {
            Console.WriteLine("Renewed salary of the employee is : "+salary );
            Console.WriteLine("Press any key to clear the screen: ");
            Console.ReadKey();
            Console.Clear();
            flag = 0;
           
        }
As i said, to the above method a thread is going to pass value "salary". But for a thread to pass value to this method, we need to create a delegate for the above method.
This is how you create a delegate with exact same signature as the method displaySalary.

public delegate void Mydelegate(int value);

Remember the above statement doesn't link your delegate to the specified method. To link your method to delegate, you must create an obect of the delegate as follows:

Mydelegate PassValDelegate = new Mydelegate(displaySalary);

Great, now you have a delegate object linking to your method. Now for the next step of passing value from a thread.




Step-2:

Imagine i have a method called setSalary, whose definition is as follows:

 public void setSalary()
        {
            Console.Clear();
            Console.WriteLine("Please provide the hike in salary and press enter:");
            int hike = Convert.ToInt32(Console.ReadLine().ToString());
            int Reviewedsalary = Convert.ToInt32(salary) + hike;
          

        }

Create a thread to execute above method in background as follows:

Thread t1 = new Thread(new ThreadStart(setSalary));
  t1.Start();

If i want to pass Reviewedsalary value to the method displaySalary, then i need to pass it using the delegate object created above. So now our method setSalary would look like this:

 public void setSalary()
        {
            Console.Clear();
            Console.WriteLine("Please provide the hike in salary and press enter:");
            int hike = Convert.ToInt32(Console.ReadLine().ToString());
            int Reviewedsalary = Convert.ToInt32(salary) + hike;
           //you can pass value to displaySalary using the delegate object as follows
           PassValDelegate(Reviewedsalary);


        }

This is little complex to understand.However check out my video, i will try to explain few things which can't be explained in writing.

Now for the working example of the above tutorial. Please check out the delegates examples by following the link i have provided you in the beginning of the post so that understanding this example will be easier.

Please start  a console project and name it as PassValBetThreads. To this project add a class and name it as employee. My class employee has 4 methods which display information about employee and 2 methods which set employee information. Go ahead and write the methods as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PassValBetThreads
{
    public delegate void Mydelegate(int value);
    public class Employee
    {
        public string name;
        public string designation;
        public string department;
        public string salary;
        public Mydelegate PassValDelegate;

       
        public  void setEmployee(string _setname, string _setdesignation, string _setdepartment, string _setsalary)
        {
            name = _setname;
            designation = _setdesignation;
            department = _setdepartment;
            salary = _setsalary;
        }

        public void setSalary()
        {
            Console.Clear();
            Console.WriteLine("Please provide the hike in salary and press enter:");
            int hike = Convert.ToInt32(Console.ReadLine().ToString());
            int Reviewedsalary = Convert.ToInt32(salary) + hike;
            PassValDelegate(Reviewedsalary);


        }

        public void displayname()
        {
            Console.WriteLine("Name of the employee is : "+ name);
        }
        public void displaydesignation()
        {
            Console.WriteLine("Designation of the employee is : " + designation);
        }

        public void displaydepartment()
        {
            Console.WriteLine("Department of the employee is : " + department);
        }

        public void displaysalary()
        {
            Console.WriteLine("Salary of the employee is : " + salary + " Dollars");
        }
    }
}

Now go to program.cs file of your console application and write the methods as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace PassValBetThreads
{
  
    class Program
    {
         public static int flag = 0;
        public delegate void MethodPointer();
      
        static void Main(string[] args)
        {
           
            int choice = 0;
            Employee Sam = new Employee();
            Sam.setEmployee("Sameer Shaik", "Programmer", "IT Development", "5000");
            while (choice != 6)
            {
                if (flag == 0)
                {
                    Console.WriteLine("Please enter 1 to view name of employee");
                    Console.WriteLine("Please enter 2 to view designation of employee");
                    Console.WriteLine("Please enter 3 to view department of employee");
                    Console.WriteLine("Please enter 4 to view salary of employee");
                    Console.WriteLine("Plese enter 5 to change the salary of the employee");
                    Console.WriteLine("Please enter 6 to exit");

                    choice = Convert.ToInt32(Console.ReadLine().ToString());
                    if (choice == 1)
                    {
                        MethodPointer PointerToNameDisplay = new MethodPointer(Sam.displayname);
                        displayinformation(PointerToNameDisplay);

                    }
                    if (choice == 2)
                    {
                        MethodPointer PointerToDesignationDisplay = new MethodPointer(Sam.displaydesignation);
                        displayinformation(PointerToDesignationDisplay);
                    }
                    if (choice == 3)
                    {
                        MethodPointer PointerToDepartmentDisplay = new MethodPointer(Sam.displaydepartment);
                        displayinformation(PointerToDepartmentDisplay);

                    }
                    if (choice == 4)
                    {
                        MethodPointer PointerToSalaryDisplay = new MethodPointer(Sam.displaysalary);
                        displayinformation(PointerToSalaryDisplay);
                    }
                    if (choice == 5)
                    {
                        flag = 1;
                        Sam.PassValDelegate = new Mydelegate(displaySalary);
                        Thread t1 = new Thread(new ThreadStart(Sam.setSalary));
                        t1.Start();


                    }
                }
            }
        }


        public static void displayinformation(MethodPointer M1)
        {
            Console.WriteLine("Displaying information you have chosen");
            M1();
            Console.WriteLine("Press any key to clear the screen: ");
            Console.ReadKey();
            Console.Clear();

           
        }

        public static void displaySalary(int salary)
        {
            Console.WriteLine("Renewed salary of the employee is : "+salary );
            Console.WriteLine("Press any key to clear the screen: ");
            Console.ReadKey();
            Console.Clear();
            flag = 0;
           
        }
      
       


       
       
    }
}



The above example must clear your understanding on passing and retrieving values from threads.

Passing data to threads C# example





Passing data to Threads:
To pass data to threads, you need to store the data to variables of class when you call constructor.First in your class create a constructor with parameters whose values will be needed by threads.
For example:
class employee
{
//Class Variables
        public string name;
        public string designation;
        public string department;
        public string salary;

//My employee constructor
public Employee()
        {
           name = "Sameer";
            designation = "Programmer";
            department = "IT";
            salary = "3000";

        }
}

Now the above class has 4 variables whose values can be passed to any thread like this:
Am going to run a thread on below method, please see the definition

 public void displayname()
        {
            Console.WriteLine("Name of the employee is : "+ name);
        }

Now am going to create the thread as follows:
//Create a thread on displayname method
Thread t1 = new Thread(new ThreadStart(displayname));
//Start the thread as follows
t1.Start();

If you check carefully, i haven't passed any parameters to method displayname. The only way to pass values to threads is by using public variables, the way its used in this example.

Please dont confuse yourself alot with this example. Start creating your own class and method. Passing values to thread should be piece of a cake.

Am going to post a detailed post on passing and retrieving values from threads in the next post. If you have any doubts on this, get back to me.


Regards
Sameer Shaik

Check out the video on this post:

Friday, June 18, 2010

WELCOME

Hello visitor,
Let me welcome you to our world of ever changing programming concepts.
I have started this blog to help you all understand the concepts you will be tested in microsoft certification.
It will be of great value if you can provide me information about which topics you will need detailed explanation and please mention the scale of difficulty you face with that concept.
All of this is for you.
Help me build this right for each one of you.
Thanks
Regards
Sameer Shaik

Skills you will be measured on in 70-536





This certification exam measures your knowledge of the fundamentals of the .NET Framework 2.0. Before taking the exam, you should be proficient in the job skills that are listed as following:

The C# Language





Developing applications that use system types and collections

  1. Manage data in a .NET Framework application by using the .NET Framework 2.0 system types. (Refer System namespace)





    Related Sample Chapter(s) or Article(s):
  2. Manage a group of associated data in a .NET Framework application by using collections. (Refer System.Collections namespace)






  3. Improve type safety and application performance in a .NET Framework application by using generic collections. (Refer System.Collections.Generic namespace)






  4. Manage data in a .NET Framework application by using specialized collections. (Refer System.Collections.Specialized namespace)






  5. Implement .NET Framework interfaces to cause components to comply with standard contracts. (Refer System namespace)






  6. Control interactions between .NET Framework application components by using events and delegates. (Refer System namespace)







Implementing service processes, threading, and application domains in a .NET Framework application

  1. Implement, install, and control a service. (Refer System.ServiceProcess namespace)






  2. Develop multithreaded .NET Framework applications. (Refer System.Threading namespace)






  3. Create a unit of isolation for common language runtime in a .NET Framework application by using application domains. (Refer System namespace)





Embedding configuration, diagnostic, management, and installation features into a .NET Framework application

  1. Embed configuration management functionality into a .NET Framework application. (Refer System.Configuration namespace)






  2. Create a custom Microsoft Windows Installer for the .NET Framework components by using the System.Configuration.Install namespace, and configure the .NET Framework applications by using configuration files, environment variables, and the .NET Framework Configuration tool (Mscorcfg.msc).






  3. Manage an event log by using the System.Diagnostics namespace.






  4. Manage system processes and monitor the performance of a .NET Framework application by using the diagnostics functionality of the .NET Framework 2.0. (Refer System.Diagnostics namespace)






  5. Debug and trace a .NET Framework application by using the System.Diagnostics namespace.






  6. Embed management information and events into a .NET Framework application. (Refer System.Management namespace)





Implementing serialization and input/output functionality in a .NET Framework application

  1. Serialize or deserialize an object or an object graph by using runtime serialization techniques. (Refer System.Runtime.Serialization namespace)






  2. Control the serialization of an object into XML format by using the System.Xml.Serialization namespace.






  3. Implement custom serialization formatting by using the Serialization Formatter classes.






  4. Access files and folders by using the File System classes. (Refer System.IO namespace)






  5. Manage byte streams by using Stream classes. (Refer System.IO namespace)






  6. Manage the .NET Framework application data by using Reader and Writer classes. (Refer System.IO namespace)






  7. Compress or decompress stream information in a .NET Framework application (refer System.IO.Compression namespace), and improve the security of application data by using isolated storage. (Refer System.IO.IsolatedStorage namespace)





Improving the security of the .NET Framework applications by using the .NET Framework 2.0 security features

  1. Implement code access security to improve the security of a .NET Framework application. (Refer System.Security namespace)






  2. Implement access control by using the System.Security.AccessControl classes.






  3. Implement a custom authentication scheme by using the System.Security.Authentication classes. (Refer System.Security.Authentication namespace)






  4. Encrypt, decrypt, and hash data by using the System.Security.Cryptography classes. (Refer System.Security.Cryptography namespace)






  5. Control permissions for resources by using the System.Security.Permissions classes. (Refer System.Security.Permissions namespace)






  6. Control code privileges by using System.Security.Policy classes. (Refer System.Security.Policy namespace)






  7. Access and modify identity information by using the System.Security.Principal classes. (Refer System.Security.Principal namespace)



Implementing interoperability, reflection, and mailing functionality in a .NET Framework application

  1. Expose COM components to the .NET Framework and the .NET Framework components to COM. (Refer System.Runtime.InteropServices namespace)






  2. Call unmanaged DLL functions in a .NET Framework application, and control the marshaling of data in a .NET Framework application. (Refer System.Runtime.InteropServices namespace)






  3. Implement reflection functionality in a .NET Framework application (refer System.Reflection namespace), and create metadata, Microsoft intermediate language (MSIL), and a PE file by using the System.Reflection.Emit namespace.






  4. Send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery from a .NET Framework application. (Refer System.Net.Mail namespace)





Implementing globalization, drawing, and text manipulation functionality in a .NET Framework application

  1. Format data based on culture information. (Refer System.Globalization namespace)






  2. Enhance the user interface of a .NET Framework application by using the System.Drawing namespace.






  3. Enhance the text handling capabilities of a .NET Framework application (refer System.Text namespace), and search, modify, and control text in a .NET Framework application by using regular expressions. (Refer System.Text.RegularExpressions namespace)






    Information available on this post has been obtained from microsoft website.If you have any updated information, do let me know.
    Thanks
    Regards
    Sameer Shaik