Office Night-Out

Welcome to...

Read More
C#: My experience with const keyword
Nov30

C#: My experience with const keyword

There are numerous post on internet about ‘what is const keyword in .net’ or ‘what is the difference between const keyword and read only’ and I must say many might have already mugged it up like me I can bet, many of us might not have tried some exercise to learn the real implementation or did not get caught in a situation where the learning would have come on the doorsteps by itself. I was lucky as the learning came in a disguise to me but unlucky because we have to bang our head for sometime as a crucial deliverable deadline was approaching. Lets begin with the scene of a ODC where team is working during the UAT week. As per the changes received, DDLs containing helper classes needs to be changed and updated in the environment. Our main website project contains only the reference to the helper classes so there was no need to build the website project. It’s a normal procedure and we have carried it out several times so we were sure everything will work. But, we were about to learn something and there it was, all the changes that were asked were available on the website but one const field which was changed in the DLL is showing the old value only. We checked the helper class and find that it is updated correctly then why our website is showing old value. On banging our head for quite sometime, we build our website project also and to our surprise, we got the updated value. Great! We realized that website will certainly keep the references to DLLs and update its IL-intermediate language code, but if we look at the IL for a const field then we will find the actual value and not the const variable name like : For const a=5, IL will not contain variable name,it will only contain the actual value 5. In our case, our website IL contained the actual value so on building the DLLs, references got updated but not the actual value. To update it, Website was also required to be build. Real time experiences can teach us insightful lessons and provide meaning to our learning. Please share...

Read More
NetBiscuits: How fascinating can it be to create a mobile website?
Nov30

NetBiscuits: How fascinating can it be to create a mobile website?

My very first impression was like why do I need it, who wants to see a site on a mobile? How would a website be able to fit into my mobile without destroying the look and feel of a desktop site? Not so long ago, I remember behavior of a site varied in content display on the mobile device depending on the device type and OS used. Sometime, we have different browsers behaving different way making users’ life tough and it comes out to be complete wastage of time as we have to again go back to desktop site for more information. There was no optimization and user experience was impacted. Original idea was to convert the mobile users permanently stick to mobile browsing but it didn’t turn out well. From a developer’s perspective: – Page load takes a lot of on mobile which takes less time on desktop. – Phones have different form factors, screen sizes, menu and navigation hardware. – Handset variation which are getting updated daily in the global market and difficult to track each and every version. I got my first mobile website development project and got introduced to NetBiscuits. To describe it, I have included an excerpt from their site. NetBiscuits enables you to develop, publish and monetize cross-platform mobile sites and apps. At www.netbiscuits.com  all tools, interfaces and mobile enabling technologies are provided to develop, publish, monitor, and monetize professional mobile web applications. By using NetBiscuits, media companies, agencies, retailers, brand owners, developers, and service providers achieve a faster path to profit from their mobile Internet programs. How to use it? In order to use it , you need to have an account at www.netbiscuits.com   . 1.1   Creation of NetBiscuits account  Create an account on the site as shown below. Once you are done with the account creation , Go to New -> custom application In order to create a custom application , you need to fill the details in the boxes as shown below Give the Application name in the first box  eg:- XYZMobile Select the Programming language as ASP Engine –Default BackendURL :-  http://demospace.xxxxxxx.com/(ApplicationName) And click on create. This creates a custom application for you with the name XYZMobile and will start appearing in the My applications screen :-   Next Article: Creation of DemoSpace....

Read More

Nullable .Net types

We declare .net types in our project daily but still we do not recollect default values for each .net type so we end up doing null check for each data type. Its a basic mistake of a developer but .net makes us intelligent with one of their operator, ‘?’ Nullable is represented by a ‘?’. Prior to that, uninitialized variables in C# is not allowed so we should know default values of .net datatypes. For value types, mostly it is 0. For ex: int id=0; I have specified only the basic value types which we use in our daily development work. For detailed list for value type, click here. Value type Default value bool false byte 0 char ” int 0 long 0L struct The value produced by setting all value-type fields to their default values and all reference-type fields to null. For reference type, default value is null. Now, the condition we normally see is to have default value as ‘Null’ for value type. To achieve this we have nullable operator ‘?’ which we mentioned earlier. How to use it? Just put it in front of your value type variable. For ex: int? id=null; This will make int variable accept ‘Null’. Please note this one more important information, if we have one int variable and other nullable int variable then they are not equivalent and cannot be assigned to each other. For ex: int x=1; int? y=2; x=y; — It is not possible and will throw compile time error. To correct it, Both the variables should be nullable. Nullable has two more properties. ‘HasValue’ for a null condition check and ‘Value’ to get the actual value. We sometimes fail to differentiate between the uses of ‘?’ as it can be used in many ways.To avoid the confusion regarding ‘?’. One of the use is a conditional operator. For ex: int id = x > y ? x : y; it is same as – int id; if( x > y ) { id= x; } else { id= y; } Same kind of operation can be written with ‘??’ but only for null check. int? x=1; int? y= x?? 1; This exercise requires some real time practice and will be much more clear so I request you all to try it and send your feedback....

Read More

Useful ‘ref’ and ‘out’ parameters

It took me some time to realize the real potential of ref and out keywords, till the time I experience a condition which could only be simplified by implementing ref and out keyword. Lets start by a simple code example and will evolve while moving forward. public int CalculateSavings(int principal) { int result=0; //Calculate savings and return result return result; } Here we have a method ‘CalculateSavings’ which takes a input parameter ‘principal’ and returns ‘result’ as output parameter. Now, we got a requirement that this method should also return ‘interest’ but if we see that a method can only return only one output, then how can we achieve it? We can achieve it by using ref or out keyword. Both are useful in this case, though there is a major difference which we will learn shortly. Two output parameters ‘result’ and ‘interest’ can be achieved by the following code. int interest; public int CalculateSavings(int principal, out int interest) { int result=0; //set interest value; //Calculate savings and return result return result; } Here, ‘out’ parameter is used to tell the compiler that value of variable ‘interest’ will also be available to the members outside of this method and is not limited to the method. This method only sets the value of out variable. Please note that, if value of ‘out’ variable is left unattended in this method and not set then it will result in compile time error. We will achieve the same by ‘ref’ keyword. int interest=0; public int CalculateSavings(int principal, ref int interest) { int result=0; //set interest value; //Calculate savings and return result return result; } I assume just declaring and initializing ‘interest’ before the method will not satisfy you and you will search for more differences over the internet. I behaved similarly but no help till the time i got the requirement which introduces ‘ref’ to me. Previously, i have mentioned if value of ‘out’ variable is left unattended in this method and not set then it will result in compile time error. But in case of ‘ref’, no compile time error is thrown whether ‘ref’ variable value is set or not. Instead of looking for the difference between ‘ref’ and ‘out’ keyword, if 5 min. are dedicated for this exercise then surely one can easily solve any similar...

Read More
Page 1 of 41234