Pages

Wednesday, February 29, 2012

Nested IF conditions vs Single line IF with AND operation

In C#, compiler will generate the same MSIL code for following code chunks.

Single line IF

if ( A && B ){
  // Do something;
}

Nested IF

if ( A ){
  if ( B ) {
  // Do something;
  }
}

Monday, February 27, 2012

How to fix TypeLoadException was unhandled Error?

Problem: I got the following runtime error while debugging my application.
TypeLoadException was unhandled
















"Evaluate" is an interface method defined in a different project (Assembly A) and implemented in another project. (Assembly B).

Then I saw following warning message in the Error list:
Warning: Found conflicts between different versions of the same dependent assembly.

The given assembly name (Assembly C) for the above warmning is referenced by both Assembly A and Assembly B. It is a third party dll. Also, Assembly A is referenced by Assembly B.


Once I double clicked on the warning, it prompted me saying "One or more dependent assemblies have version conflicts. Do you want to fix these conflicts by adding binding redirect records in the app.config file?

Then, once I accepted that, the code chunk given below was automatically added to my App.config file.







Consequently, that has fixed the issue!

But I suspected that there can be some unused dlls as the above App.config entry is redirecting all assembly binding references for versions 0.0.0.0 to 3.1.3.42154 (the latest) to version 3.1.3.42154 dll. This should happen as you can't load multiple versions of the same assembly in the same app domain. So, without binding redirect, an attempt to load a different version of an already loaded assembly will fail.

Since the above code chunk sounds like a workaround to overcome the issue, I revisited the code to find the actual reason for the exception.

There, I could find that different versions of the same dll is used in two dependent projects.















So, when I refer the third party dll (Assembly C) from the same folder, same version, and remove the code chunk in the App.config, I did not get the "TypeLoadException" issue.

After doing some research on this I found that this may occur if you have an interface in one assembly and it's implementation in another, and the implementation assembly was build against a different version of the interface.

Also, this issue can occur if you load assembly dynamically using LoadFrom(string) method. I used LoadFrom method to invoke Assembly B.

Also, the project dependency given above is called as "diamond shaped" dependency which may cause this issue.

However, the error message is little bit mis-leading, but we can get some clue out of that. Exception can occur even the method is not executed during the runtime.

Sunday, February 26, 2012

How to survive as a Software Engineer? It's not all about CODING!

Being a software engineer for few years and also being involved in some other fields such as quality assurance and database administration, made me to think twice on "What are the essential qualities you need to develop to survive as a software engineer?".

These may be some of the SPECIFIC qualities you need in order to ENJOY your work, (in the sense) you can still keep on working as a software engineer and gain some income without having them.
Without boasting further about it, I will get in to the topic.

1.  Being Curious

2. Love Problems (This is hilarious though!)

3. Have faith that problems will not last forever, might as well call this "Optimism"

4. See problems and challenges as means of improving - as opposed to get "Suicidal feelings"

5. Celebrate "fixing a bug"

6. Hate 8 - 5

This is it up to this time. I'll keep on adding when ever I found some more. Add anything if missing. I will elaborate on each of the above later.

Thursday, February 23, 2012

Using ANTLR with Visual Studio 2008 (C# Target)

ANTLR can be used with different language targets such as C# and Java. This is how to use ANTLR with C# in VisualStudio IDE.
  1. Install Java Runtime Environment (JRE).
  2. Verify the above using "java -version" command.
  3. Set the Java CLASSPATH variable to point the ANTLR package version you want to use. Ex: antlr-3.1.3.jar
  4. Build the ANTLR project to verify that there are no issues using following command:
  5. java -cp "path to ANTLR package" org.antlr.Tool "grammer file name"
  6. Ex: java -cp "C:\antlr\antlr-3.1.3.jar" org.antlr.Tool tsqllexer.g
Some important options:

If you get "java.lang.OutofMemoryError: Java heap space" issue, use the following command to modify the JVM heap size.
Xmx750M

If you get "Multiple token rules can match input such as X, Tokens X.Y was disabled for that input" issue, use the following command to set NFA conversion timeout for each decision for a suitable value.
-Xconversiontimeout 10000
  1. Create new C# project.
  2. Add these dlls as references: antlr.runtime.dll, Antlr3.Runtime.dll, Antlr3.Utility, StringTemplate.dll
  3. Build the C# project.

How to fix "Exception in thread "main" java.lang.NoClassDefFoundError:org/antlr/Too"

The following error is an initial error for ANTLR developers, when they try to build their application for the first time using command prompt:







What you need to do?

You need to set the Java Classpath for ANTLR application as given below in cmd:
set CLASSPATH = D:\antlr\antlr-3.1.3.jar

Check if it is set correctly using the following command:
echo %CLASSPATH%

Above command should display the given path.

Then verify it using the following command:
java org.antlr.Tool -version

The result should be something like this based on your ANTLR version:
ANTLR Parser Generator Version 3.1.3 Mar.18 2009 10:09:25

Why you need to do that?

The Classpath is a parameter set either command line or through envronment variable that tells JVM (Java Virtual Machine) where to look for user defined classes and packages when running Java programs. Without this, JVM does not know where to fine code base related to org.antlr.Tool.

Wednesday, February 15, 2012

Multi-dimensional databases vs Relational databases

Just back from the SQL Server user group meeting and the first topic was about implementing multidimensional databases using OLAP.

Below video has a good introduction about this interesting concept!

Get data type of a table in database dynamically

You can get the data types of columns in a given table daynamically using system tables as given below: Assume 'CurrentStatus' is the table name.









The column name and the data type will be diplayed as result.

Tuesday, February 14, 2012

Monday, February 13, 2012

JOIN vs IN vs EXISTS in sql

Difference between IN and EXISTS
  • IN will ignore null values where as EXISTS will considers null values as the result.
  • JOIN also, will consider null values.
Difference between IN and JOIN
  • IN does not consider duplicate values where as JOIN consider duplicates. (To avoid duplicate values, use DISTINCT)
  • EXISTS also does not consider duplicate values.

Wednesday, February 8, 2012

Why/ Why not use "WITH RECOMPILE" option?

Once the sproc does not have any syntax errors, it will create entries in sysobjects, sysdepends and syscomments tables. But, it will not compile the query until you execute that.

At the time you execute the proc, SQL Server will create an execution plan and save that in procedure cache for future use.

When the proc executes again, it will re-use the same execution plan, unless otherwise statistics are being changed.

So, if you want to use a new execution plan for each individual execution, use WITH RECOMPILE option in your proc.

Why?
- Optimal use of indexes on columns in a case by case scenario

Why not?
- Improves performance

Add an alias to connect database server using SSMS in MSSQL

  1. All Programs > SQL Server Configuration Manager
  2. Expand SQL Native Client 10.0 Configuration
  3. Aliases
  4. New Alias



Wednesday, February 1, 2012

Linux Kernal Headers

Linux kernal headers provide APIs to interact between modules in kernal level as well as modules between user space and kernal space.

If you want to do any kernal level operations in Linux, you need to use above APIs with C language.

It is located at /usr/src/linux-headers-x.x. See below:



How to fix "The program 'gcc' can be found in following packages' issue

GCC is the compiler environment for Linux. Once you type "gcc" in cmd, you will get the following error if gcc is not installed.






To fix this issue, run following command:
sudo apt-get install build-essential

Create and Edit and Save a file in Linux

  1. pico test.c
  2. Type your content
  3. ctrl + x
  4. Select option 'Y'

Get process information in Linux

Use "top" command

How to get system information in Linux?

Use uname -m command to see whether your kernal is 64-bit or 32-bit. uname -ar will give you the other system and OS information such as machine name, Linux version etc.

get Linux system information