Tuesday, February 27, 2007

InitilizeComponent() visited

Well these are two scenarios that happened to me:
In one of the earlier projects, just before a release I was working on a alignment issue in an aspx page and no change to the code behind file was done. But when the setup was tested on a fresh machine, the application threw exceptions !!. Finally what we found out was that the code behind of the file has really changed, to make things worse, the URL of the web service was hardcode to “Localhost” . After the release (went home at 6.30 in the morning that day), had some time to investigate this matter.
What I found was that someone has instantiated the web service proxy reference inside the InitilizeComponent() , and every time there is a change in the aspx page design , the InitilizeComponent() gets regenerated, and because the web service proxy reference was instantiated inside this method , the auto-generated code assigns the URL property of the proxy to “Localhost”.

I ran into the same sort of problem yesterday, the application was running fine. But once I changed the language property of the form and ran, a NullReferenceException was thrown. Again some one has instantiated a custom resource manager inside the InitilizeComponent(), and when the language is changed for the form , this method is regenerated , and the user written code is lost!
Thus, a NullRefernceException

So, don’t every try to change the InitilizeComponent() method, you may never know where the problem may come from.
If you need custom code that needs to be written when initializing components, do it in the constructor, just after the Initilize component method.


This problem was in VS2003, but in VS2005 the auto generated designer code goes into a seperate partial class.

Sunday, February 25, 2007

More on localization...

I was changing the Region and language settings in the control panel, and was expecting that text in the forms to show up in the language that I had set, with no luck.Did some reading and ……
CurrentUICulture is a per thread settings that governs from which resource file the ResourceManager class uses in a localized application. CurrentUICulture is a language setting and does not have any thing to do with date or currency formats.
The CurrentUICulture is determined from the users default language, or if this is not set, the language used by the o/s
The CurrentUICulture can be changed as follows
Thread.CurrentThread.CurrentUICulture = new CultureInfo(“fr”);

The CurrentCultrue is not a language setting, CurrentCultutre determines the date formats , currency formats etc.. This can be changed from the Regional option from your control panel. The CurrentCulture ctor takes a culture (a region and a language) as a parameter and will throw an exception if a neutral culture is specified
In a web application, the browsers default language can be determined to respond with the appropriate web page, as shown below
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);

Thursday, February 22, 2007

Arguing with a egg head !

Well, after posting a mail on Stringbuilder vs String to a group in Sri Lanoka (BITPROJECTS), A guy named Ranga, started pointing out a mistake in my example, well I proved that he was wrong !!.
But I guess the guy did not wanted to give up, so he started talking about everything else other then StringBuilder
Think he might have got really frustrated, loosing !!!.
So finally I thought of giving up, as I did not want him to go to ANGODA .
Damn, I can’t think there are people in this world who think they are PERFECT. All for sure he was not perfect, he was a egg headed MUTT, who was stubborn to listen to anyone. People like him are going to have a hard time living in this world !!!.
I hope god helps him out!!!.

Monday, February 19, 2007

String vs StringBuilder

Take a look at the code given below

string str = string.empty;

for(int i= 0 ; I < 100; i++)

{

str += i.ToString();

}

You should never use string concatenation inside a loop, the reason for this being that strings in java and c# are Immutable.What this mean is that each time you concatenate a string, a whole new string object is created with the older value appended to the concatenated value, ok what happens to the older string object ?? well it is left for the garbage collector.

So we got two points here

1) object creation means processing power and memory usage 2) in this example the garbage collector needs to clean up nearly 100 string objects !!!

So what’s the solution ??

Use StringBuilder class (C# - found in the System.Text anmespace), StringBuilder (java not sure of the namespace).

What StringBuilder class does is it actually appends, without actually creating a string object on concatenating , therefore u will be using only one StringBuilder instance in this example, which basically means less processing power and memory usage when instantiationg the StringBuilder instance and garbage collector need to clean only one object !!

Revise code

StringBuilder builder = new StringBuilder(100);

for(int i= 0 ; I < 100; i++)

{

builder.Append( i);

// Use builder.toString() to get the whole string back

}

But , don’t over use StringBuilders as well, for an example, don’t use StringBuilders when u try to concatenate 2 strings, this degrades performance

StringBuilder class has methods that can be used to optimize the string concatenations, for an example a stringBuilder object can be created using a specified string length to store like StringBuilder builder = new StringBuilder(100)

Any question on this or If u want to know how StringBuilders work internally ??, please send me a reply.

Friday, February 16, 2007

Java vs C# - Virtual methods

In Java, instance methods are virtual by default—they can be overridden in subclasses unless they are explicitly declared final. In C#, by contrast, instance methods are non-virtual by default. To make a method virtual, the programmer must explicitly declare it virtual. Why is non-virtual the default in C#?

There are several reasons. One is performance. We can observe that as people write code in Java, they forget to mark their methods final. Therefore, those methods are virtual. Because they're virtual, they don't perform as well. There's just performance overhead associated with being a virtual method (more on this in the next mail). That's one issue.

There are two schools of thought about virtual methods. The academic school of thought says, "Everything should be virtual, because I might want to override it someday." The pragmatic school of thought, which comes from building real applications that run in the real world, says, "We've got to be real careful about what we make virtual."

So Java sticks to the academic school of thought and c# for performance sticks to the second school of thought

So if u think of performance in java better to mark your methods with the final keyword for methods that you don’t think clients should not override.

This may be not a performance problem at all with hardware in this era, but in performance related systems even the small bit counts !!!.

 

 

 

 

Friday, February 9, 2007

Something I ran into

This something I found out about ASP.NET 2.0, As I am not doing ASP.net at the moment, I thought of just logging this, incase I may need it.

You can not simply address a control in java script. (I am using Master page) 
document.getElementById('
TextBoxEmpName').value you have to replace the above code line with..
document.getElementById(
'<%=TextBoxEmpName.ClientID %>').value

If you use GridView and  you have a  " Date " field column that you want to format it using 
DataFormatString="{0:d}" , this won't happen unless you change the property HtmlEncode="false".

Actually this is not an issue, this is what my friend Sanjaya has found, for 24 hour display time column field you have to use formating string as DataFormatString="{0:HH:mm}".

[http://www.sumudusharp.blogspot.com/]

 

 

 

 

Tuesday, February 6, 2007

Workout machine

I was gaining waight has hell, so i decided to buy my own work-out machine.What really caught my eyes was the Obitrek, it was a cool peice of machine and cheap, I decided to buy it.
Now it is laying near the kitchen(not in the kitchen), I had a go yesterday and today morning, it was amazing cos' it does not develope cramps as normal jogging does.

Back to work !

After a long relexation,I am back in a new project. This time it was a project from a huge service provider, the part I am working on is a plug in to a bigger project. Currently it just me and the tech-lead in the project, the scary thing about this project is that I am playing the role of a senior software engineer. Well anyway I am only allocated into the project for a period of less then 2 months, Thats a relief !!!.
My frist task is to localize the appliaction into 14 different languages, hope everything would go fine.