C# Tip



How do I write code to invoke a method dynamically?

Ans: One of the uses of Reflections is that we can invoke a method dynamically. Following code snippet invokes a method fun( ) of sample class stored in 'emitassembly.dll'.

Assembly a = Assembly.LoadFrom ( "emitassembly.dll" ) ;
Type t = a.GetType ( "sample" ) ;
Object o = Activator.CreateInstance ( t ) ;
t.InvokeMember ( "fun", BindingFlags.InvokeMethod, null, o, null ) ;

CreateInstance( ) creates an instance of the specified type and InvokeMember( ) method invokes the method specified as the first parameter.

Top


VB.NET Tip



How do I display an icon of an application in system tray and display context menu for it?

Ans: Create a Windows application and place NotifyIcon control on the form. Name it as trayicon. Add an icon to your application and name it as myicon. Set the Icon property of trayicon as myicon. Place a ContextMenu control on the form and name it as trayconmenu. Add one item to the menu having text 'Exit' and name as ex. Set the ContextMenu property of trayicon as myconmenu.

Add handler for click event for ex menu item and write following code in it.

Private Sub ex_Click ( ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles ex.Click

Application.Exit( )

End Sub

Top


Device Driver Tip



I/O Address Space...

The location numbers in the I/O address space are known as ports or port addresses. For example, a printer device connected to a D type 25 pin female connector. The female connector is further connected to a parallel port device. The data status and control registers of the parallel port device are mapped to locations 378h, 379h and 37Ah. 

Thus 378h, 379h and 37Ah are port addresses of 3 device registers of the parallel port device respectively. The processor provides two separate instructions IN and OUT for reading and writing from the location in the I/O space.

Top


VC++ Tip



How do I check programmatically, whether 'View As Web Page' option is enabled?

Ans: Use shell API function SHGetSettings( ) as shown below:

void CMyDlg::OnGetSettings( ) 
{

SHELLFLAGSTATE sh ;
SHGetSettings ( &sh, SSF_DESKTOPHTML ) ;

int status = sh.fDesktopHTML ;

if ( status != 0 )
AfxMessageBox ( "View As Web Page option is enabled" ) ;

}

Here, to the SHGetSettings( ) function we have passed address of variable of SHELLFLAGSTATE structure and a parameter called SSF_DESKTOPHTML to indicate that the information about fDesktopHTML member of SHELLFLAGSTATE structure is required. We can also use this function to know whether options like 'Show All Files', 'Show Hidden Files' are enabled or not.

Top


C++ Tip



When can I use the istrstream class?

Ans: When we want to retrieve the streams of bytes from memory we can use istrestream. The following example shows the use of istrstream class.

#include <strstream.h>

void main( )
{

int age ;
float salary ;
char name[50] ;
char str[] = "22 12004.50 K. Vishwanatth" ;

istrstream s ( str ) ;
s >> age >> salary >> name ;

cout << age << endl << salary << endl << name ;
cout << endl << s.rdbuf( ) ;

}

Here, s is the object of the class istrstream. When we are creating the object s, the constructor of istrstream gets called that receives a pointer to the zero terminated character array str. The statement s >> age >> salary >> name ; extracts the age, salary and the name from the istrstream object s. However, while extracting the name, only the first word of name gets extracted. The balance is extracted using rdbuf( ).

Top


C Tip



What is environment and how do I get environment for a specific entry?

Ans: While working in DOS, it stores information in a memory region called environment. In this region we can place configuration settings such as command path, system prompt, etc. Sometimes in a program we need to access the information contained in environment. The function getenv( ) can be used when we want to access environment for a specific entry. Following program demonstrates the use of this function.

#include <stdio.h>
#include <stdlib.h>

main( )
{

char *path = NULL ;

path = getenv ( "PATH" ) ;
if ( *path != NULL )

printf ( "\nPath: %s", path ) ;

else

printf ( "\nPath is not set" ) ;

}

Top


Article: C#- The Regular Expressions



Almost every application has search facility, where we type a string in a dialog box and search it in the given text. We can replace the string with specified string too. We can provide this kind of facility in our program using methods of the System.String class. But if we wish to search how many times a string is repeated or the strings starting and ending with a given character or the strings representing dates in a given range or strings having repeated characters in it, etc. then searching becomes difficult. For this kind of situation regular expression language is of great use. In regular expressions language we can write search expressions. Using these search expressions we can extract, delete or replace a substring. We can also split a string into substrings. 

One of the most common usages of regular expressions is to validate the user input such as e-mail address, credit card number, ZIP code, etc. Another common use of regular expression is ‘screen scraping’. Suppose a web site extracts real time information from a database and displays it on an HTML page. Information can be a weather report, stock exchange prices, air ticket reservation and so on. If we want to use this information in our application we would access the HTML page. What we would get is the HTML script. We would need to parse the script to obtain the required information. Regular expressions can be effectively used in this task. 

The regular expressions language consists of literals and metacharacters (sometimes called escape sequences). A metacharacter acts as a command to the regular expression parser. A parser is the engine responsible to understand and interpret the regular expression. The System.Text.RegularExpressions namespace provides classes to create and use the regular expressions.

Let us take an example. The metacharacter '\b' is used as a word boundary indicating either beginning or end of a word. If we wish to search a word that starts with character ‘a’ then the search expression will be @“\ba”. To search for a word that ends with characters ‘ing’ the expression will be @“ing\b”. If we have to write an expression that searches strings starting with @“\ba” and ending with @“ing\b”, what about the characters in the middle? For this, we must use the '\S' escape sequence. '\S' stands for any character except a whitespace. So, our expression would be @“\ba\S*ing\b”. * here means zero or more number of characters. So the expression “\S*” means zero or more number of characters except the whitespace

Let us use this search expression in a program.

using System ;
using System.Text.RegularExpressions ;
static void Main ( string [ ] args )
{

String instr = @“It's amazing that the amount of news that happens in the world everyday always just 
exactly fits the newspaper." ;

String pattern = @"\ba\S*ing\b" ;
Match m = Regex.Match ( instr, pattern, 
RegexOptions.IgnoreCase ) ;
while ( m.Success )
{

Console.WriteLine ( m.Value ) ;
m = m.NextMatch( ) ;

}

}

This program would output ‘amazing’. The Regex class contains several methods that we can use to perform operations using the specified regular expression. Here, the string pattern contains our regular expression. The static method Regex.Match( ) finds the string matching to the specified pattern in the input string instr. Matching is done case-insensitively since we have mentioned the enumerated value RegexOptions.IgnoreCase. The Match( ) method returns a reference to an object of the Match class that we have collected in m (don’t get confused between the Match( ) method and Match class). If the Match( ) method succeeds, the Value property of the Match class contains the resultant substring and the Success property contains true. Another property of Match class called Index contains the index of the first character of the resultant substring. To locate and display all the matching substrings, we have called the NextMatch( ) method of the Match class. The NextMatch( ) method returns the Match reference with the next matching substring. If the NextMatch( ) method is called after the last match is found, it would fail and the Success property would contain false. Instead of m.Value we can directly pass m to WriteLine( ) method because it calls the ToString( ) method overridden in Match class. The ToString( ) method of Match class returns the same substring that the Value property holds. 

The following table displays the metacharacters we can use in regular expression.

 Expression

 Meaning

 . 

 Matches any character except \n

 [character]

 Matches a single character in the list

 [^characters]

 Matches a single character not in the list

 [charX-charY]

 Matches a single character in the specified range

 \w

 Matches a word character (word character is any alphanumeric character and underscore)

 \W

 Matches a non-word character

 \s

 Matches a whitespace character

 \S

 Matches a non-whitespace character

 \d

 Matches a decimal digit

 \D

 Matches a non-digit character

 ^

 Beginning of the line

 $

 End of the line

 \b

 On a word boundary

 \B

 Not on a word boundary

 *

 Zero or more matches

 +

 One or more matches

 ?

 Zero or one matches

 {n}

 Exactly n matches

 {n,}

 At least n matches

 {n,m}

 At least n but no more than m matches

 ( )

 Capture matched substring

 (?<name>)

 Capture matched substring into group name

 |

 Logical OR

We would now write a program that splits a given input string using the specified delimiter. Suppose we have collected e-mail addresses separated by semicolon ( ; ). Then we can split the string into single address as shown in the following code.

String instr = "rucha_200@hotmail.com;malviya@yahoo.com;rdeshpande@hotmail.com" ;
Regex ex = new Regex ( ";" ) ;
string [ ] str = ex.Split ( instr ) ;
foreach ( string s in str )

Console.WriteLine ( s ) ;

We have created an object of the Regex class passing to its constructor the delimiter string i.e ‘;’. The Split( ) method of the Regex class splits the string passed to it. It uses the pattern passed to the Regex’s constructor. The Split( ) method splits the string into substrings and returns an array of the substrings. We have collected the same and displayed. We can use multiple delimiters to split a string. The multiple delimiters must be separated by a pipe ( | ) character.

We saw how to find a pattern in a string using the Match( ) and Matches( ) methods. We can use these methods to find multiple patterns in a string too. For example,

String instr = @"The brain is a wonderful organ, it does not stop until you get into the office." ;
Regex ex = new Regex ( "(is)|(in)|(it)" ) ;
MatchCollection mc = ex.Matches ( instr ) ;
foreach ( Match m in mc )

Console.WriteLine ( "{0} found at position {1}", m, m.Index ) ;

We have specified the multiple patterns “is”, “in” and “it” separated by ‘|’. This will display multiple instances of the given patterns along with their positions. The character ‘i’ is common in all the patterns, so alternatively, we can use a regular expression as "i(s|n|t)".

Regular expressions language can be effectively used for creating applications like HTML processing, HTTP header parsing, etc. Here is an example that checks the HTML script for the ‘href’ tag used for specifying links. 

String instr = @"<HTML>

<A href = ""freevb.htm"">Free VB.NET source code</A> 
<A href = ""freec.htm"">Free VC++ source code</A> 
</HTML>" ;

string pattern = @"href\s*=\s*(?:""(?<1>[^""]*)""|(?<1>\S+))" ;
MatchCollection mc = Regex.Matches ( instr, pattern,RegexOptions.IgnoreCase ) ; 
foreach ( Match m in mc ) 

Console.WriteLine ( m ) ;

Interpreting the expression used to find hrefs is now your job. 

We can use the brackets [ ] to find a set or range of characters in a string. For example, the expression “[aeiou]” can be used to match any vowel in the string. On the other hand, if we use ^ as the first character in [ ] then it finds characters other than those specified. For example, “[^aeiou]” will find all the consonants. To find characters that fall in a range, we can use dash (–) to separate the two characters. For example, the expression “\d[0-25-9]” will find any digit (\d is used to match a digit) in the string that is in the range 0 to 2 or 5 to 9.

Groups And Captures

One of the features of regular expressions is that we can group characters together. For example, suppose a training institute has maintained a list of machine names and their IP addresses in a string. We can create two different groups. One group will contain machine names from all the strings and second will contain IP addresses. Following program illustrates how to create and use groups.

using System ;
using System.Text.RegularExpressions ;
static void Main ( string [ ] args )
{

string instr = "User1 192.168.90.45\n" + "User2 192.168.90.46\n" + "User3 192.168.90.47" ;
Regex ex = new Regex ( @"(?<name>\S+)\s"+@"(?<ip>(\d|\.)+)" ) ;
MatchCollection mc = ex.Matches ( instr ) ;

foreach ( Match m in mc )
{

Console.WriteLine ( "\nEntire string: {0}", m ) ;
Console.WriteLine ( "Name: {0}", m.Groups [ "name" ] ) ;
Console.WriteLine ( "IP: {0,5}", m.Groups [ "ip" ] ) ;

}

}

The groups are always created in pair of parenthesis. Our two groups namely name and ip are noticeable by the two pairs of parenthesis.

Top


Joke



Two psychiatrists were at a convention. "What was your most difficult case?" one asked the other.

"Once I had a patient who lived in a pure fantasy world," replied his colleague. "He believed that a wildly rich uncle in South Africa was going to leave him a fortune. All day long he waited for a make-believe letter to arrive from a fictitious lawyer. He never went out or did anything. He just sat around and waited.

"What was the result?" "It was an eight-year struggle, but I finally cured him. And then that stupid letter arrived…"

Top


Different Strokes





An Illusion!

Top