Wednesday, October 04, 2006

.NET Developer beginning Ruby (rails) - Part 1

Now that I am hooked on to Ruby (rails), I am teaching myself Ruby using this book. I have decided to post about Ruby features which impress me as a .NET developer, in the form of a series of posts. So, here is my first one…

In Ruby, you can define a "code block". All lines of code enclosed within the curly braces (or do ...end) make one code block.

{ puts "Hello World!" ...}
# OR
do puts "Hello World!" ... end

Also, you can make a code block and a method "talk" to each other. Here is a simple example:

def methodSimple()
# execute code block associated with the method call
yield
end

# Call methodSimple
methodSimple() { doSomthing1() doSomething2() }

In the example above, "doSomething1" and "doSomething2" will be executed when the interpreter reaches the "yield" (special) statement in methodSimple. We can also make "methodSimple" pass parameters to the code block by making the following changes:

def methodSimple()
# execute code block associated with the method call
yield(param)
end



# Call methodSimple
methodSimple() {|param| doSomthing1(param) doSomething2() }
This is interesting right? Let us try a more useful example:

class Website

def initialize()
@header = "Header"
@footer = "Footer"
@content = "Content"
end

def displayPage
# Display site header
display(@header)

# Display article content
display(yield(@content))

# Display the site footer
display(@footer)
end
end

# English version of the website
w1= Website.new()
w1.displayPage() { |content| return translate_to_english(content)}
# Hindi version of the website
w2 = Website.new()
w2.displayPage() { |content| return translate_to_hindi(content)}

Most of the code is trivial. Notice the display(yield(@content)) line in the displayPage method. This is what is happening there :What did we gain by doing this? Our "Website" class remains independent of the translation logic. I think, we can make use of the "Code block" feature in Ruby to write more generic code.

Saturday, September 30, 2006

.NET developer's first look at Ruby(rails)

Ok so ...all the buzz about Ruby on Rails finally got to me and I decided to check out the official website and see what I am missing.

This is what I had heard about Ruby on rails:After several minutes of browsing, I now know that:
Pretty cool huh!! However because of my .NET and PHP background, it took me a while to get used to the Ruby syntax. Here is some Ruby code to display all items of array "names".

@names.each do
|name|
puts "Hello #{name}!"
end

Maybe it is just a matter of getting used to. Anyway, If I have gotten you excited about Ruby(rails) and you want to check it out yourself, I strongly recommend this 15 minute interactive Ruby tutorial .

I am going to download this Ruby editor for windows next and write some "hello world" type programs. Based on how things look right now ...I might have to do this in the future:

MyBlog.Title[".NET"] = "Ruby on Rails"

Get it?

Friday, September 29, 2006

Best practices for .Net Performance

Pretty useful article:

http://www.c-sharpcorner.com/UploadFile/bhakeeswaran/BPNetPerformance107292006234943PM/BPNetPerformance1.aspx

Monday, September 18, 2006

In quest of Truly Modal popup in ASP.NET

This is how it all started...I stumbled upon this cool Modal popup control in the Atlas control kit:

http://atlas.asp.net/atlastoolkit/ModalPopup/ModalPopup.aspx

Notice how it blocks the activity on the screen until you are done interacting with the popup. I decided to put this in one of my ASP.NET web applications. But this control immediately became useless to me because I wanted to display the popup based on some logic in the application and the Atlas popup control can only be bound to an ASP.NET control.

After googling on this topic a bit it was clear to me that it cannot be done without using JavaScript (which I was hoping to avoid). I also found this interesting article which explains "how to fill in ASP.NET gaps using client-side magic"

http://www.ondotnet.com/pub/a/dotnet/2003/09/15/aspnet.html

Here is some sample code from this article which shows how to include JavaScript in your ASP.NET code using Page.RegisterStartupScript method.

string popupScript = "window.open('PopUp.aspx', 'CustomPopUp', " +
"'width=200, height=200, menubar=yes, resizable=no')" +

Page.RegisterStartupScript("PopupScript", popupScript);

The Javascript will get placed in the onLoad() event of the page and hence will be executed as soon as the page loads (which would create a popup effect). So now I had an efficient way of managing Javascript in my ASP.NET application. But I still needed the JavaScript for the modal popup when I hit this gem (click on [Test the Alert]) :

http://slayeroffice.com/code/custom_alert/

Also notice how the alert box remains frozen even if you scroll up and down the page. Very COOL!!

The approach is to over-ride the default window.alert javascript function. The person who made this calls it "Pseudo-modality" because this is achieved by using a 100% wide and 100% tall absolutely positioned DIV element that acts as the parent element of the custom alert. This DIV overlays everything on the page and prevents user interaction with elements other than the custom alert.

So now I have this cool Javascript code and also know how to use the Page.RegisterStartupScript method to put Javascript in my ASP.NET application in an organized way. I can't wait to implement this in my application.

I will post about how that goes some other time ...

Sunday, September 17, 2006

Enum and CallbyName .NET example

As some of you already know …I am working on cleaning up (re-writing) report generator tool (written in VB.NET) at work. Following impressed some of my colleagues, and so I am putting it here. Please feel free to comment your opinions on this approach …

I created an enum, which holds a list of all date validations that should be applied to a “report”.

Public Enum defaultDateValidation
blankDates 'Verify that user has supplied “From” and “To” dates
FourDigitYear ' Verify that user supplied dates have year in 4 digit format
End Enum

Then I have the following validation functions somewhere in my class …

public function blankDates() as boolean
' See if dates were actually suuplied and return true or false accordngly
msgbox (....)
return true/false
end function


public function FourDigitYear() as boolean

' See if dates are in 4 digit format and return true or false accordingly
msgbox (....)
return true/false
end function

And finally here is the glue code that gets executed when input dates for the report are accepted from the user and it is time to perform date validations.

Dim dateValidation As String
For Each dateValidation In System.Enum.GetNames(GetType(defaultDateValidation))
If Not CallByName(Me, dateValidation, CallType.Method) Then
Exit Sub
End If
Next


HINT: Notice that the enum instances and the corresponding validation functions have the same name.

For the uninitiated - I am looping through all the Enum instances (using For each) and calling a function that has the same name as the Enum instance (using CallByName).

Why do it this way? I think this code is easier to maintain. Suppose I (or my fellow software developers) have to add another validation type called “myBirthMonth” for a new report. To achieve this we would just have to create a new entry “MyBirthMonth” in the defaultDateValidation Enum and then create a boolean function (with the same name ofcourse) that does the actual validation. And my glue code will take care of the rest.

If you liked this post, please subscribe to the RSS feed of this blog.

Saturday, September 16, 2006

Cannot bind to the new display member

I am working on an internal "report generator" tool at work. One of the forms has a listbox control. This is what I was trying to do:


' myHashTable holds objects of type "Report"
ListBox.DataSource = myHashTable.Values
ListBox.DisplayMember = "ReportName"
ListBox.ValueMember = "ReportCode"


This looks pretty straight forward. But when I ran the code, I got "Cannot bind to the new value member" exception. Googling this error did not get me anywhere. While mucking aound with the code I changed "ReportName" and "ReportCode" in my "Report" class from members to properties. And WALLA this worked!!

Hopefully this post will save someone a headache.

My First Blog

This is my "Hello World" blog.

I have always wanted to blog about issue-->solution---> issue cycle I go through day to day at my job as a full-time software developer.

Lets see how this goes ....

This page is powered by Blogger. Isn't yours?