RSS

Tag Archives: c#

Regular Expressions Patterns


RegExp

  • Integer (positive and negative)
    • ^[+-]?\d+$
    • ^[+-]?[0-9]+$
    • Matches: any signed integer
  • Integer (positive)
    • ^[0-9]+$
    • Matches: any positive signed integer
  • Integer (negative)
    • ^[-][0-9]+$
    • Matches: any negative signed integer
  • Decimal (positive and negative)
    • ^[-+]?\d+(\.\d+)?$
    • ^[+-]?[0-9]*(?:\.[0-9]*)?$
    • Matches: 123 | -123.45 | +123.56
  • Decimal (positive)
    • ^[+]?[0-9]*(?:\.[0-9]*)?$
    • Matches: 123 | 123.45 | +123.56
  • Decimal (negative)
    • ^[-][0-9]*(?:\.[0-9]*)?$
    • Matches: -123 | -123.45 | -123.56
  • Number (positive and negative)
    • ^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$
    • Matches: 23 | -17.e23 | +.23e+2  
  • Natural Number
    • ^[0-9]*[1-9]+$|^[1-9]+[0-9]*$
  • Alphabetical
    • [^a-zA-Z]
    • Matches: ABC | Test | xyz
  • Alphanumeric
    • [^a-zA-Z0-9]
    • ^[a-zA-Z0-9_]*$
    • Matches: ABC | Test123
  • E-Mail
    • ^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
    • Matches: AhmedNegm@WindowsLive.com
  • Date
    • ^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$
    • Matches: this expression validates a date field in dd/mm/yyyy  and mm/dd/yyyy format
  • Temperature
    • ^[-+]?\d*(\.\d+)?( )?(Celsius|C|c|CELSIUS|Fahrenheit|F|f|FAHRENHEIT|Kelvin|K|k|KELVIN)?$
    • Matches: 34F | 56 Celsius | 22C
  • Egyptian National ID
    • (2|3)[0-9][1-9][0-1][1-9][0-3][1-9](01|02|03|04|11|12|13|14|15|16|17|18|19|21|22|23|24|25|26|27|28|29|31|32|33|34|35|88)\d\d\d\d\d
  • Egyptian Mobile Number
    • (201)[0-9]{9}
  • Hexadecimal String
    • ^[0-9A-Fa-f]+$
    • Matches: 062706440644064700200623064306280631
 
 

Tags: , , , , , , , , , , , , , , , , , , ,

Bulk Insert into SQL Server using C#


multiple-files

I was recently tasked with a project at my work to update SQL Server database with large amounts of data each hour. Every hour, I’ve got an text file contains more than 500,000 records and there were several that needed processing daily. 

Read the rest of this entry »

 
Leave a comment

Posted by on July 17, 2013 in ASP .NET, Visual C# . NET

 

Tags: , , , , , , , , , , , , ,

Strong Name in .NET !


This content copied from MSDN !

Strong Name consists of the assembly’s identity—its simple text name, version number, and culture information (if provided)—plus a public key and a digital signature. It is generated from an assembly file (the file that contains the assembly manifest, which in turn contains the names and hashes of all the files that make up the assembly), using the corresponding private key. Visual Studio can assign strong names to an assembly. Assemblies with the same strong name are expected to be identical.

Strong Name (further referred to as “SN”) is a technology introduced with the .NET platform and it brings many possibilities into .NET applications. But many .NET developers still see Strong Names as security enablers (which is very wrong!) and not as a technology uniquely identifying assemblies. Strong Name is a technology based on cryptographic principles, primary digital signatures.

Read the rest of this entry »

 
1 Comment

Posted by on April 20, 2013 in Visual C# . NET

 

Tags: , , , , , , , , , , , , ,

Visual Studio 2010 Code Snippets !!


code1

Code snippets are small blocks of reusable code that can be inserted in a code file using a context menu command or a combination of hot-keys. They typically contain commonly-used code blocks such as try-finally or if-else blocks, but they can be used to insert entire classes or methods.

Read the rest of this entry »

 
Leave a comment

Posted by on January 15, 2013 in Visual Basic .NET, Visual C# . NET

 

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Dynamic keyword in C# 4.0 !!


image9_207

You have to read or know more about DLR (Dynamic Language Run-time) from here (DLR)

The introduction of static type checking represented an  important milestone in the history of programming languages. In the 1970s, languages such as Pascal and C started enforcing static types and strong type checking. With static type checking, the compiler will produce an error for any call that fails to pass a method argument of the appropriate type. Likewise, you should expect a compiler error if you attempt to call a missing method on a type instance.

Other languages that push forward the opposite approach—dynamic type checking—have come along over the years. Dynamic type checking contradicts the idea that the type of a variable has to be statically determined at compile time and can never change while the variable is in scope. Note, however, that dynamic type checking doesn’t confer wholesale freedom to mix types, pretending they’re the same. For example, even with dynamic type checking, you still can’t add a Boolean value to an integer. The difference with dynamic type checking is that the check occurs when the program executes rather than when it compiles.

Read the rest of this entry »

 
Leave a comment

Posted by on October 10, 2012 in Visual Basic .NET, Visual C# . NET

 

Tags: , , , , , , , ,

Passing & Streaming images to CrystalReport at run-time !!


Sometimes, you want to make something abnormal in your application. One of these abnormal actions that you want to pass an image to your report (CrystalReport) at run-time stage of your application.

There are many articles and resources have been written in this topic, but all are complicated and take a hard code to do with incomplete way. You can follow the pictures to see how this ??

Read the rest of this entry »

 

Tags: , , , , , , , , , , , , , , , , , ,

How to stop “Ding” sound when pressing “Enter” in TextBox


Very simple way to remove the annoying beep sound from pressing enter in a single-line text box, for example in a console, or login form. Very simple, no multiline, and the sounds gone!

private void txtValue_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        e.SuppressKeyPress = true;
    }
}
//////////////////////////
private void txtValue_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Enter)
    {
        e.Handled = true;
    }
}
 
Leave a comment

Posted by on August 4, 2012 in Visual C# . NET

 

Tags: , , , , , , ,

OOP Concepts – Polymorphism !!


 

Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance. Polymorphism is a Greek word that means “many-shaped” and it has two distinct aspects:

  1. At run time, objects of a derived class may be treated as objects of a base class in places such as method parameters and collections or arrays. When this occurs, the object’s declared type is no longer identical to its run-time type.
  2. Base classes may define and implement virtual methods, and derived classes can override them, which means they provide their own definition and implementation. At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code you can call a method on a base class, and cause a derived class’s version of the method to be executed.

Virtual methods enable you to work with groups of related objects in a uniform way. For example, suppose you have a drawing application that enables a user to create various kinds of shapes on a drawing surface. You do not know at compile time which specific types of shapes the user will create. However, the application has to keep track of all the various types of shapes that are created, and it has to update them in response to user mouse actions. You can use polymorphism to solve this problem in two basic steps:

  1. Create a class hierarchy in which each specific shape class derives from a common base class.
  2. Use a virtual method to invoke the appropriate method on any derived class through a single call to the base class method.

First, create a base class called Shape, and derived classes such as Rectangle, Circle, and Triangle. Give the Shape class a virtual method called Draw, and override it in each derived class to draw the particular shape that the class represents. Create a List<Shape> object and add a Circle, Triangle and Rectangle to it. To update the drawing surface, use a foreach loop to iterate through the list and call the Draw method on each Shape object in the list. Even though each object in the list has a declared type of Shape, it is the run-time type (the overridden version of the method in each derived class) that will be invoked.

 
Leave a comment

Posted by on May 9, 2012 in Visual C# . NET

 

Tags: , , , , ,

OOP Concepts – Inheritance !!


What Is Inheritance?

Introduction:

Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics (or pillars) of object-oriented programming. Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. A derived class can have only one direct base class. However, inheritance is transitive. If ClassC is derived from ClassB, and ClassB is derived from ClassA, ClassC inherits the members declared in ClassB and ClassA.

Conceptually, a derived class is a specialization of the base class. For example, if you have a base class Animal, you might have one derived class that is named Mammal and another derived class that is named Reptile. A Mammal is an Animal, and a Reptile is an Animal, but each derived class represents different specializations of the base class.

Read the rest of this entry »

 
Leave a comment

Posted by on May 9, 2012 in Visual C# . NET

 

Tags: , , , , , , , , ,

Functionality of the “new” keyword in C#


When you declare a class variable, it is initially unassigned. To use a class variable, you must create an instance of the corresponding class and assign it to the class variable. To create an instance of a class, you use the new operator.

The new operator does two things:

  • It causes the CLR to allocate memory for your object
  • It then invokes a constructor to initialize the fields in that object.

The version of the constructor that runs depends on the parameters that you specify for the new operator. The following code example shows how to create and use instances of the House class

House MyHouse = new House();
House YourHouse = new House();

If you call new and do not specify any parameters, the default constructor runs. Remember that if you define one or more constructors for a class, the C# compiler does not create a default constructor for you automatically.

 
Leave a comment

Posted by on May 2, 2012 in Visual C# . NET

 

Tags: , , , , , , , ,