Monday, January 1, 2018

SOLID Principles in C#


SOLID principles are group of 5 principles, which helps to create a good software system. One can notice that all design patterns are following these principles.

While designing a software system, several things are taken into considerations that may includes design pattern, coding, database schemas, robustness, adapting to new requirements, etc.



SOLID principles helps us to minimize the software desing problems while creating the software. It helps in creating a strong software architecture that can also be reused with the other requirements of the users.

SOLID stands for following 5 important object oriented principles:

·         S: Single Responsibility Principle (SRP)
·         O: Open closed Principle (OSP)
·         L: Liskov substitution Principle (LSP)
·         I: Interface Segregation Principle (ISP)
·         D: Dependency Inversion Principle (DIP)


The SOLID principles help in making the software design more understandable, flexible, robust, extensible and maintainable.

Sunday, December 31, 2017

Calculate Distance Between Two Lat Long in SQL Server


SQL SERVER provides the geometry data type which stores the spatial data type. To calculate the distance between two lat longs in sql server, the point should be stored in "geometry" data type.

Syntax:
  
     
        SELECT GEOMETRY.STDistance(GEOMETRY)
           

STDistance returns the shortest line distance between two points in a geometry.


Example 1:
STDistance() on numeric column.

@sourceLAT VARCHAR(10),
@sourceLONG VARCHAR(10),
@destinationGEO GEOGRAPHY,
@destinationLAT VARCHAR(10),
@destinationLONG VARCHAR(10)

SET @sourceLAT = '28.412676'
SET @sourceLONG = '77.314813'
SET @sourceGEO = geography::Point(@sourceLAT, @sourceLONG, 4326)

SET @destinationLAT = '28.413686'
SET @destinationLONG = '77.357664'
SET @destinationGEO = geography::Point(@destinationLAT, @destinationLONG, 4326)

SELECT
          ROUND(@sourceGEO.STDistance(@destinationGEO), 2) AS [DISTANCE (Meter)],
          ROUND((@sourceGEO.STDistance(@destinationGEO)) / 1000, 2) AS [DISTANCE (KM)]



Saturday, December 30, 2017

What are escape sequences in C# ?


Escape sequences are special characters which are created by combination of characters consisting of backslash (\) followed by a letter of a combination of digits.

An escape sequence is considered as a single character since there is no letter or character that textually represents the escaped character. Backslash (\) in a string indicates that character following it is a special character or should be interpreted literally.


List of C# escape sequences:

·         \'   -        Single Quotation mark
·         \"   -        Double Quotation mark
·         \\  -        Backslash for file path
·         \?  -        Question Mark
·         \a  -        Alert (bell character)
·         \b  -        Backspace
·         \f   -        Form feed
·         \n  -        New line
·         \r  -        Carriage return
·         \t  -        Horizontal tab
·         \v  -        Vertical tab
·         \e  -        Escape
·         \0  -        Null
·         \u  -        ASCII character hex value escape sequence
·         \x  -        ASCII character hexadecimal escape sequence
·         \o  -        ASCII character octal notation


Friday, December 29, 2017

Gratuity Calculator


TekGrabs Gratuity Calculator
Basic Pay:


Calculated Gratuity
Dearness Allowance (D.A):


Number of years of Service:







Formula:
Gratuity Calculation= [ (Basic Pay + D.A) x 15 days x No. of years of service ] / 26

Where,
D.A = Dearness Allowance.

End of Service Benefits Eligibility:
1. Any person employed on wages/salary.
2. At the time of retirement or resignation or on superannuation, an employee should have rendered continuous service of not less than five years.
3. Payable without completion of five years only when death and disablement.

Example: Gratuity amount for a given Basic Pay = 17500, DA = 1500 and Years of Service = 25

Solution: = ( ( 10000 + 1000 ) * 15 * 5 ) / 26 = 31730.77

Therefore, gratuity amount is Rs. 31730.77


Thursday, December 28, 2017

Declaring and Initializing Variables in C#

Declaring and Initializing Variables in C#

You can declare and initialize variables by using the following syntax:

< data_type > < variable_name > = < value >;

In the above syntax, the represents the kind of data type that will be stored in a variable and specifies the value that needs to be stored in the variable.

Consider the following statement that declares a variable:

int marks = 79;

The preceding statement declares a variable named ‘marks’ of the int data type. In addition, the statement initializes the variable with the value, 79. The int data type is used to store numeric data (integers).


Consider another following statement:

char isSuccess = ’y’;


The preceding statement declares the variable ‘isSuccess’ of the char data type and initializes the variable with the value, y.


Rules for naming variables in C#

Rules for naming variables in C#

In C#, the following rules are used for naming variables:

· A variable name must begin with a letter (a-z or A-Z) or an underscore (‘_’), which may be followed by a sequence of letters, digits (0-9), or underscores.

· The first character of a variable name cannot be a digit i.e. variable cannot starts with 0 to 9.

· A variable name must not contain any embedded spaces or symbols i.e. ? ! @ # + - % ^ & * ( ) [ ] { } . , ; : ″ ′ / and \.

· Instead of giving space to make a meaning, an underscore can be used wherever a space is required, like Highest_Marks.

· A variable name must be unique. For example, to store two different values, two unique variable names must be declared.

· A variable name can have any number of characters i.e. a variable name can be of any length.

· Keywords or reserved words cannot be used as variable names. For example, you cannot declare a variable named class as it is a keyword.

· C# is a case-sensitive language. It means that the Highest_Marks variable is not the same as the highest_marks variable. In other words, Uppercase letters are considered distinct from lowercase letters.

Examples of valid variable names are:

v  Error_Message

v  ResponseMessage

v  This_variable_name_is_very_long

v  _SerialNumber


Examples of invalid variable names are:

v  #count

v  2index

Monday, December 25, 2017

Introduction to C#

Introduction to C#

Computer languages have come a long way since the 1940s. During that period, scientists punched instructions into large, room-sized computer systems. These instructions were given in machine language, which consisted of a long series of zeroes and ones. These machine language instructions were executed directly by the CPU. The machine language is called the First Generation of computer languages.

The 1950s saw the emergence of the Second Generation of computer languages, the assembly language. Assembly language is easier to write than machine language but still extremely complicated for a common man. However, the computer could still understand only machine language. Therefore, the Assembler software was developed to translate the code written in assembly language into machine language.

In 1967, Martin Richard developed a language called Basic Combined Programming Language (BPCL) for writing operating systems. An operating system is a set of programs that manages the resources of a computer and its interactions with users. The era of the Third Generation of computer languages had arrived. In 1970, Ken Thompson modified BPCL to create a new language called B. While working for Bell Laboratories, Thompson teamed up with Dennis Ritchie and wrote an initial version of the Unix operating system for a DEC PDP-7 computer.

Dennis Ritchie was working on a project to further develop the Unix operating system. He wanted a low-level language, like the assembly language, that could control hardware efficiently. At the same time, he wanted the language to provide the features of a high-level language, that is, it should be able to run on different types of hardware.

“B” had performance drawbacks, so in 1972 he rewrote B and called it C. Therefore, C is categorized as both a Second and a Third Generation language. Thompson and Ritchie rewrote the Unix operating system in C. In the years that followed, C was widely accepted and used over different hardware platforms. This led to many variations of C. In 1989, the

In the early 1980s, Bjarne Stroustrup of Bell Labs developed the C++ language. In his own words, “C++ was designed primarily so that my friends and I would not have to program in assembly, C, or various modern high-level languages. Its main purpose was to make writing good programs easier and more pleasant for the individual programmer.”

C++ was originally known as ‘C with classes’ because two languages contributed to its design: C, which provided low-level features, and Simula67, which provided the class concept. C++ is an object-oriented language. Other object-oriented languages are Java, Smalltalk, and C# (pronounced as C sharp).


C# is a programming language introduced by Microsoft. C# is intended to be a simple, modern, general-purpose, object-oriented programming language. The name C# is inspired by the musical scale. On a musical scale, sharp (#) written against a note means that the note should be made half step higher in pitch. This is similar to the name C++, where ++ appended to C indicates an enhanced version of the C language.


Friday, December 22, 2017

bool.TryParse in C#


bool.TryParse method is used to convert given string to Boolean value same like bool.Parse but the main difference is that it does not throw exception even if the input is not in correct format or is null.

Syntax:
bool.TryParse(string str, out returnValue);

str is Input string.
returnValue: Boolean value.

If the conversion is successful, true is returned otherwise false is returned to the out parameter.




This function returns true for successful conversion and false if conversion is failed. Every time there will be one of two results either false if failed or true if successful. There will not be any type of exception thrown by this function even if the input string is:

a)   Null                                      :        NO ArgumentNullException

b)   Not is correct format            :        NO FormatException

double.TryParse in C#


double.TryParse method is used to convert given string to its double-precision floating point number same like double.Parse but the main difference is that it does not throw exception even if the input is not in correct format or is null.

Syntax:
double.TryParse(string str, out returnValue);

str is Input string.
returnValue: double-precision floating point number equivalent to “str”.

If the conversion is successful, the given string in converted into double-precision floating point number otherwise 0 is returned to the out parameter.



This function returns true for successful conversion and false if conversion is failed. Every time there will be one of two results either 0 if failed or value if successful. There will not be any type of exception thrown by this function even if the input string is:

a)   Null                                      :        NO ArgumentNullException
b)   Not is correct format            :        NO FormatException
c)    Out of range                         :        NO OverflowException


Thursday, December 21, 2017

decimal.TryParse in C#


decimal.TryParse method is used to convert given string to its decimal number same like decimal.Parse but the main difference is that it does not throw exception even if the input is not in correct format or is null.

Syntax:
decimal.TryParse(string str, out returnValue);

str is Input string.
returnValue: decimal number equivalent to “str”.

If the conversion is successful, the given string in converted into decimal number otherwise 0 is returned to the out parameter.



This function returns true for successful conversion and false if conversion is failed. Every time there will be one of two results either 0 if failed or value if successful. There will not be any type of exception thrown by this function even if the input string is:

a)   Null                                      :        NO ArgumentNullException
b)   Not is correct format            :        NO FormatException
c)    Out of range                         :        NO OverflowException