RaKasUniverse.info

I still don't know why I registered this domain!

Archive for the ‘Programming’ tag

Automatically Add Name Attribute for Google Syntax Highlighter

with 7 comments

If you write programming related posts in your blog, you are most probably using Google Syntax Highlighter to highlight your code snippets. In case if you don’t, it’s a set of javascripts that allows you to add syntax highlighting for source code that you post on your blog. It supports a large number of programming languages. After installing “Google Syntax Highlighter for WordPress” plugin (If you are using WordPress of course), all you got to do is to enclose your code inside “pre” tags, add a name attribute with value ‘code’ and then set the language of programming code in class attribute.

Ex:-

<pre name="code" class="java"> .... </pre>

But there is a slight problem. When you use it with WordPress WYSIWYG post editor all the name attributes get removed. Therefore every time you change the post using WYSIWYG editor you have to go back to HTML view and add name attributes again. Which is really a pain if your post is a large one with lots of text and code snippets.

Read the rest of this entry »

ASP.Net: Reduce Page Size, and Make Pages Load Faster by Storing View State on Server

with 3 comments

Imagine that you have a control that allows you to edit a specific object (Lets say a very complex user information form like in facebook). Every time you change/click something on your control, something about that object is changed. But only if you click save it should update things in your database. Where do you keep your data object in between changes until the last submit is received from save button?

You may use session variables, but then the data object is not local to your control/page. It’s there in the session. If user close the browser or navigate to another page at some point it is still in the session. With complex objects that takes up memory this can be problematic. Specially if you want your application to be scalable, you don’t want to put unnecessary things in to session. And you don’t want to keep things longer than you need in memory. How would you deal with this? Very often you will find yourself keeping track of things using hidden fields in HTML forms.

Read the rest of this entry »

Fun with Java Reflections: Access Private Member Fields/Methods of other Classes

without comments

This is a follow-up to my earlier post on Java Reflections.  In the earlier post I just showed how to invoke methods of an object dynamically using reflection. Here I am going to talk about accessing attributes of another object using reflection, with a little twist.

Just like my earlier post I’ll start this with an exercise.

Write a class with a private member variable only. No get/set methods. But add a toString() method to output the content of that private variable.

Like Following….

Now write another class. It should create an instance of above class, and set your name as the value of ‘name’ attribute in that object.

Read the rest of this entry »

Java Core API: Introduction to Java Reflections

with 6 comments

Here is an exercise for you!

Write a java program that take two or more command line arguments. 1st argument is a class name. 2nd argument is a method name. Rest of the arguments are string values to be passed in to that method.

Your program has to load the class specified in first argument (this class may not be available at compile time). Create an instance of that class. Then invoke the method given in the 2nd argument on that object passing the values given in rest of the arguments as parameters to that function. To simplify things lets say all parameters are string.

The answer is reflections. To give you a very simplified description, Reflections allow you to access members of a class/object dynamically in run time without having to hard-code things in the source code. This is slower than hard-coding. But it is very useful when the class/methods/fields that you need to access are not known or not available at compile time. Also in some cases you can do cool things like accessing private members.

Read the rest of this entry »

Me, My Self and Java

without comments

Programming is my main hobby. I wrote my first computer program when I was 11 years old. Then I was using GW BBASIC. Since then I have learned QBASIC, Pascal, Visual Basic, Perl, Java, PLSQL, C++,  PHP, C#, bit of Python and many other things over time. I screwed up  my A/L for it but fortunately managed to get in to a career in Software Development. When I was a school kid I built some web sites using ASP and Perl but they were never published. And I spent a great deal of time learning regular expressions. Graphics Design was not my thing. So I ended up as an applications programmer.

Read the rest of this entry »