Friday, 27 September 2013

Delphi Assert Function - A Debugging Tool in Delphi

Delphi Assert Function - A Debugging Tool in Delphi

Assert function in Delphi is used as a debugging tool. Assert Function is used to make sure that certain conditions which are assumed to be true are never violated. Assert is a Symbol not a Keyword in Delphi. You can set Assertion ON/OFF in RAD Studio.

Syntax of Assert Function:

function Assert(expr : Boolean [; const msg: string]);

Assert function tests whether a boolean expression expr is true. If not, Assert raises an EAssertionFailed exception. If a message string was passed to Assert, the exception object is created with that string (Exception.CreateFmt).

Assert provides an opportunity to intercept an unexpected condition and halt a program rather than allow execution to continue under unanticipated conditions.

Code Snippet for Assert Function

procedure TestAssert;
var
  i : integer;
begin
   i:=5;
   assert(i<2,'Assert Message');
end;

Now when you run the above Delphi code with Assertions enabled, you will get message 'Assert Message' because 'i<2' is false. If i = 1, the program will continue as nothing happened.

ASSERT is not a KEYWORD in DELPHI

Assert is not a keyword in Delphi. Assert is the symbol that is not reserved by the compiler. Assert exists as symbol within the "System" unit namespace. You can create your own Assert procedure or function and even though it's not recommended, it will compile.

Assertion Settings in Delphi

We can put settings in the RAD Studio to tell the compiler whether to compile with assertions in a debug mode or not. Assertion are always "ON" in debug mode and "OFF" in release mode by default. Compiler directives are provided to disable the generation of assertion code: $ASSERTIONS ON/OFF(long form) 

Firebird Basic Interview Questions and Answers

Firebird Basic Interview Questions and Answers

If you are going to appear in a firebird technical interview, you must go through following basic firebird interview questions and answers. These are very basic questions on firebird database like general introduction to firebird, features of firebird database, similarities and differences between firebird and interbase, IBExpert tool for Firebird, firebird database connectivity etc. Following are the firebird interview questions and answers.

1. What do you know about Firebird database?

You should know general things about the Firebird database like: 

A) Firebird is open source database
B) It runs on Windows, Linux and Unix
C) Some Facts, Figures and Features of Firebird database

Read more for further details...

2. What are the various features of Firebird database?

You should be aware of the features and functionalities Firebird database provides like:

1. Firebird supports multiple platforms like Windows, Linux and Unix
2. Multi-generation architecutre of Firebird database
3. Powerful and developer-friendly SQL language
4. Logging and monitoring features of Firebird database
5. Security and Performance of Firebird and much more.

Read more for further details...

3. What is the difference and similarities between Firebird and Interbase databases?

You must prepare this interview questions because Firebird and Interbase databases are closely related. There are a lot of difference and similarities between Firebird and Interbase databases. I have written a complete article on this. You can access it here.

4. What is IBExpert?

IBExpert is a professional Integrated Development Environment (IDE) for the development and administration of InterBase and Firebird databases. IBExpert includes many coding tools and features: visual editors for all Database Objects, an SQL Editor and Script Executive, a Debugger for Stored Procedures and Triggers, a Query Builder, a powerful Database Designer and much, much more...

5. How to connect with Firebird database in Delphi using TSQLConnection?

This question is related to Delphi developers. TSQLConnection component is used to connect with firebird in Delphi. Below is code snippet for making firebird database connection in Delphi.

begin
  SQLConnection1.ConnectionName := 'Devart InterBase';
  SQLConnection1.DriverName := 'DevartInterBase';
  SQLConnection1.GetDriverFunc := 'getSQLDriverInterBase';
  SQLConnection1.Params.Values['LibraryName'] := 'dbexpida40.dll';
  SQLConnection1.Params.Values['VendorLib'] := 'fbclient.dll';
  SQLConnection1.Params.Values['HostName'] := 'hostname';
  SQLConnection1.Params.Values['Database'] := 'databasename';
  SQLConnection1.Params.Values['User_Name'] := 'username';
  SQLConnection1.Params.Values['Password'] := 'password';
  SQLConnection1.LoginPrompt := False;
  SQLConnection1.Open;
end;

6. Have you ever found the compiler error "Unsupported on-disk structure for file xxx.fdb" while working with Firebird database?

This is the common error occurs in most of the cases when you are using both firebird and interbase. Sometimes mismatch happens between gds32.dll (interbase driver dll) and fbclient.dll (firebird driver dll) which causes this error. There is detailed solution of this error discussed here.

Beside all the above firebird interview questions and answers, you should prepare general database concepts like joins, index, query optimization, stored procedures and functions, triggers, cursors etc. 

Wednesday, 25 September 2013

List of Errors and Compiler Warnings while code migration from Delphi 7 to Delphi XE4 - Part 1

List of Errors and Compiler Warnings while code migration from Delphi 7 to Delphi XE4 - Part 1

I had to migrate a Delphi 7 application to the Delphi XE4. While migrating my Delphi 7 application to Delphi XE4, I encountered a lot of compiler errors, warnings and hints. I made a list of all the compiler errors and warning with their solutions which I got during migration from Delphi 7 to Delphi XE4. I think I should share my list and experience with all you Delphi guys. 

Basically, you will get all these compiler errors and warnings when you are migrating the code from your Delphi application which is earlier than Delphi 2009 to Delphi 2009 or the higher versions of Delphi. Most of the errors are related to unicode strings. Delphi 7 does not support unicode because unicode support in Delphi was introduced in Delphi 2009.

Here goes my list of errors and compiler warnings which I got and resolved during Delphi 7 to Delphi XE4 code migration.

1. Mismatched datatypes like AnsiChar / AnsiPChar / AnsiString 

In my Delphi 7 code, a lot of AnsiChar, AnsiPChar and AnsiString keywords were used which were causing problems. So I converted them to WideChar (Char), Unicode String (String) and WidePChar (PChar) for unicoding.

2. Index 0 is not accessible in string

In Delphi 7, there is 0 based string indexing while in Delphi XE4, there is 1 based string indexing. So wherever 0 index of string was used, I had to use 1 index.

3. NoMetadata property does not exist

In Delphi 7, TSQLDataset has property NoMetadata which if set to true, no metadata is loaded. Similar property in Delphi XE4 is GetMetadata which if set to false, no metadata is loaded.

So, I replaced all NoMetaData = True lines to GetMetadata = False and the error got resolved.

4. DBXpress component was not found

DBXpress is deprecated and its functionality is included in SqlExpr. So I removed DBExpress from uses.

5. TMsgDlgBtn enum was causing compilation errors

TMsgDlgBtn enum (Message Dialog) has 12 elements in Delphi XE4 while in Delphi 7, there are only 11 elements. New element is mbClose. So added mbClose in the enums.

6. Shortstring (string[n]) datatypes were throwing errors

Shortstring (string[n]) datatype is deprecated in Delphi XE4 but is there for backward compatibility. So you can still use shortstring in Delphi XE4 but its ANSI not unicode.

I was using firebird 2.5.2 database. I also got some error related to that:

7. %1 is not a valid Win32 application

I had installed 32-bit Delphi XE4 and 64-bit Firebird 2.5. Because of this, I was getting the mentioned error while connecting to the database through Delphi XE4. I uninstalled 64-bit Firebird, downloaded and installed 32-bit Firebird and the problem was resolved. 

8. Following errors were coming in while executing firebird database queries from Delphi XE4

invalid Token
invalid request BLR at offset 163 
function F_ANYFUNCTION is not defined     
module name or entrypoint could not be found

Earlier I was using Firebird 2.5.1. Now I had migrated to Firebird 2.5.2. I looked in the Firebird 2.5.1 configuration file and made similar changes in firebird 2.5.2 configuration file.

ExternalFileAccess = Restrict (path of database file)

Also added udf.dll in UDF folder of Firebird 2.5.2

9. Unknown Driver - DevartInterbase

My Delphi 7 was using DevartInterbase driver in one of the TSQLConnection component. But now when I migrated to Delphi XE4, I was getting the above error. To solve this I downloaded the latest version of devart interbase for Delphi XE4 and the problem was resolved.

10. Unsupported on-disk structure for file mydatabase.fdb

When we installed latest version of devart interbase, by default it was using gds32.dll which is for interbase database. I just replaced VendorLib property of TSQLConnection from gds32.dll with fbclient.dll and the problem was resolved.

11. 'SQLDataSet: Type mismatch for field 'NAME', expecting: String actual:WideString'

When I installed latest devart interbase driver to support Delphi XE4 unicode version, I got this error in TSQLConnection component. 

I found 2 solutions for this error:

A) Set UseUnicode property of TSQLConnection to False.
B) Replace all TStringField with TWideStringField

12. interbase is not licensed

As I downloaded the developer free edition of devart interbase, it was giving this error. To resolve this error, Firebird and Interbase databases have to be restarted every 24 hour. Afterwards I got enterprise edition of devart.

13. Cannot perform operation – DB is not open

While connecting to database, firebird library name and path (fbclient.dll) was missing. Added fbclient.dll path and problem was resolved.

14. “EurekaLog Error” - project post processing has failed

Delphi 7 was using EurekaLog 6. But now I was using EurekaLog 7. So to handle the error, I went in Project –> Eurekalog options –> activated eurekalog and changed project type to old eurekalog application.

Rebuilt and compiled the application and error was gone.

15. When opened Delphi 7 application in Delphi XE4 RAD Studio, I noticed that all the dfm files had changed. The changes which I noticed were like:

Width = 443 --changed in Delphi XE4
Height = 277 --changed in Delphi XE4
ExplicitWidth = 443 --added automatically in Delphi XE4
ExplicitHeight = 277 --added automatically in Delphi XE4

These are the changes which RAD Studio introduces itself in the dfm files when you open it first time. You can ignore these changes or accept it for the first time.

I will come up with more compiler errors and warnings which I got during Delphi 7 to Delphi XE4 code migration in second part of this post.

Purpose and Usage of CharInSet function in Delphi XE4

Purpose and Usage of CharInSet function in Delphi XE4

CharInSet function is found in SysUtils unit. When I was migrating my Delphi 7 code to Delphi XE4, I found the usage and purpose of CharInSet function. While migrating my code from Delphi 7 to Delphi XE4, I got following compiler warning:

[DCC Warning] MyUnit.pas(80): W1050 WideChar reduced to byte char in set expressions. Consider using 'CharInSet' function in 'SysUtils' unit.

I had following procedure in my MyUnit.pas file which was throwing this compiler warning:

procedure MyProcedure;
var
  C: Char;
begin
  C := 'k';
  if C in ['a'..'z', 'A'..'Z'] then
  begin
   ShowMessage("Show any message");
  end;
end;

Cause of the compiler warning: In Delphi 7, a character was one byte, so holding characters in a set was no problem. But now in Delphi XE4, Char is declared as a WideChar, and thus cannot be held in a set any longer. 

Solution of the above compiler warning: If you don't bother about this compiler warning, you can ignore this. But if you actually want to get rid of this compiler warning, you have to use CharInSet function to remove this compiler warning like following:

  if CharInSet(C, ['a'..'z', 'A'..'Z']) then
  begin
   ShowMessage("Show any message");
  end;

The CharInSet function will return a Boolean value, and compile without the compiler warning.

Tuesday, 24 September 2013

Development vs Maintenance/Support Projects: Learning and Growth Opportunities

Development vs Maintenance/Support Projects: Learning and Growth Opportunities

In an IT industry, as a software developer, you are supposed to do work on different kind of projects. I have categorized all the projects as development projects and maintenance /support projects. Mostly there are maintenance/support projects as compared to development projects in a software company because development will be done only once but maintenance will continue life long until the end of world.  

I just wanted to share my experience and thoughts on development and maintenance / support projects through this article. I might be wrong at many places because these thoughts are my personal, sheer based on my experience in my 4 years of IT career. I have experienced that developers who are in development projects are treated better as compared to the developers in maintenance/support projects. At the beginning of my career I also used to think so. I also wanted to be in development project but I was thrown in a maintenance / support project. After working in that project, I realized that I was wrong. You have a lot to do and learn in a maintenance / support project also. 

I have tried to compare development projects and maintenance / support projects in my ways:

Learning Curve: 

You learn a lot in a development project because you have to code each and every feature and functionality from scratch in development project. In this way you come to learn the technology as well as application functionality in depth. After the completion of your project, you can consider yourself expert in that technology (Java, .NET, PHP.....etc) and the domain of the application (Banking, Retail, Manufacturing, Security, Shipping/Transportation, Online Shopping....etc.) if you have sincerely worked in that project because I think a complete real world project will teach you everything and will give you the priceless experience.

If I look at Maintenance/Support project, learning curve is also very good. In these kind of projects, you learn from other people's code, efforts and experience. Everything is pre-prepared in front of you. You have access to each piece of code which is written by expert developers and also the functionality. Whenever you have to resolve any issue, enhance any functionality, add a new feature, remove any existing feature, you will have to scan and understand the whole code and functionality before making any changes to the existing code. You will have to keep good care of impact analysis before making any single line change in the existing code and have to retest all the existing functionality after making the changes. This kind of practice will enhance your debugging capabilities and you will start gaining confidence and get a good hold on the application functionality and technology. This kind of learning experience is going to serve you a lot in your future.

Growth Opportunity: 

Again its common misconception that if you are in development project, you will get good hike as compared to your colleague who is working on maintenance/support project. Its not true. It sheer depends upon your capabilities, your efforts which you have put in your project to benefit the customer, to benefit your company. If you are doing good in the project, you will get good appraisal. Politics do play a good role in appraisals, but that's not the topic of discussion today.

Conclusion: 

Whatever project you get in your company whether development or maintenance/support, take it by heart. Work hard and smart, you will definitely learn many things and will also get benefited. Just continue learning from whatever you are doing.

Friday, 20 September 2013

Must Prepare Delegates and Event Handling Questions for C# Interview

Must Prepare Delegates and Event Handling Questions for C# Interview 

If you are going for interview in C# / ASP.NET, I would suggest you to must prepare the topic of Delegates and Event Handlers in C#. This is the most basic as well as tricky C# interview question which you will be surely asked. I have listed down some of the C# interview questions on Delegates and Events.

1. What are Delegates? What are the types of Delegates? What is the difference between Delegate and Multicast Delegate?
2. What are Events and Event Handlers in C#? 
3. What is the basic difference between Delegates and Events?
4. Isn't an Event Handler a type of delegate in C#?
5. When to use Events and when to use Delegates?
6. All Delegates in ASP.NET are Multicast Delegates?
7. Write down the syntax of Delegate / Multicast Delegate.
8. How to subscribe and unsubscribe event handlers and delegates in C#?

There is a lot of material available on delegates and event handling concepts on Google and StackOverflow. You can get all these by just simple googling. You should also prepare some working example on the above questions so that you can better explain the above concepts on delegates and events. Also give a good look on the syntax of delegates and events. If you would be able to answer the above questions on delegates and events in the C# interview, half of the work will be done. It will give you the positive impact and upper hand in the interview because I have noticed that even many of the experienced C# developers are also not able to explain the delegates and event handling concepts.

I remember that when I was just a fresher in the IT world in 2009, I had given a project interview in my company for ASP.NET project. One of the senior team member took my technical interview and asked very basic C# and ASP.NET questions at the very beginning of interview.  I felt confident by answering those simple C# / ASP.NET questions on CLR, conditional statements, looping structures, session, cookies, data binding concepts etc. Then he jumped to delegates and event handling questions and after telling him 2-3 basic things on delegates, I was blank and mum. I did not know the depth of delegate concepts and how delegates and events are used in the real world projects. Because I was fresher at that time, so that person spared me on those questions, smiled and took me into his project. But today as a 4 years experience, if I am not able to explain these delegates and event handling concepts in the interview, I will make big blunder and this time nobody is going to spare me on these questions.

Wednesday, 18 September 2013

A Software Developer should be a good Debugger and Analyst

A Software Developer should be a good Debugger and Analyst

As a developer, you are not going to develop a project from scratch every time  You cannot expect sheer coding work at all the time. In IT industry, major effort of project life cycle goes into maintenance and support. 

I have 4 years of software development experience and currently I am working in my second company. In my this short experience, I have worked in 5 projects out of which 3 were maintenance projects and 2 were development projects which had to be developed from scratch. My first project was in Shipping domain which was maintenance project. Next two were development projects in Retail and Manufacturing domain which had to be developed in Delphi XE2 and WPF respectively, fourth one was health and insurance domain and my current project in security domain in which I have to migrate a Delphi 7 application to Delphi XE4.

In all the projects I did till now, I had done a lot of maintenance tasks like fixing existing bugs, doing some enhancements, adding / modifying new features in the existing big application, handling tickets and resolve the issues etc. But this is also quite interesting job in the life of a software developer. In fact these things are the real test of a software developer. To handle these kind of tasks you must have good debugging capabilities. You have to identify and use your skills and experience to know in which file, in which part of code, the issue is arising. You should use StackTrace, TraceInto features to know the real cause of the issue and fix it.

Impact Analysis

Only issue tracking and fixing is not only the solution. You should be aware of the impact which your change can cause to the application. So I suggest you to comment at every place where you make changes in the code with name, date and reason why the code was changed. Proper testing of application is required after making any change to ensure that all the existing functionality are not impacted by your change.

Documentation 

After adding/modifying the features/issues in the application, documentation related to the application like SSRS, Test Cases should be updated.

In short, maintenance and support projects are not boring. You can learn a lot from these projects. These kind of projects sharpen your debugging skills and make you good analyzer. These qualities are necessary to make you a complete and skilled software developer.

Learning and sharing: 

While analyzing the issues and debugging the code, you come to explore many secrets of the complex application. I usually use to note down all the new things, findings and solutions which I learn during this process and document it. I use to share them on the internal knowledge portal of my company so that other people if encounter same kind of error, may get benefited from my work. It also helps me because if I face same problem, I can refer my document.