Friday, November 20, 2009

It's RUBY time :) - Strongly typed arraylists in Ruby

Hey Guys,

I was trying out Ruby and in the program below I am attempting to create a strongly typed ArrayList :) Seriously, I really really enjoy programming in Ruby :) Let me know if you have any comments...!



# Author - Karthik Ananthapadmanaban
# Date/Time - 11/21/2009 1.43 AM
# Updated on - 11/21/2009 11.21 AM

# A simple module that encrypts/decypts strings
# Similar to caesar's cipher
module Crypt
class Cryptor
def Encrypt(string)
@str1 = ""
string.each_byte{|x| @str1 = @str1 + ((x+1).chr)}
return @str1
end

def Decrypt(string)
@str1 = ""
string.each_byte{|x| @str1 = @str1 + ((x-1).chr)}
return @str1
end
end
end

# A simple module that implements
# various arithmetic operations
module Math
class Arithmetic
def Add(num1,num2)
return num1+num2
end

def Sub(num1,num2)
return num1-num2
end

def Mul(num1,num2)
return num1*num2
end

def Div(num1,num2)
return num1/num2
end

def Sqr(num1)
return num1*num1
end

def Sqrt(num1)
return num1 ** 0.5
end
end
end

# As we all know, ruby is dynamically typed
# I just wanted to create a class
# that allows only manipulating a certain type
# so, the result is a strongly typed class (trivially?) :)
# Yes, in C# ArrayList items are not strongly typed,
# against List<>, where the items are strongly typed <>
# comments are welcome ;)
module Collections
class ArrayList
def initialize (defType)
@defaultType = defType
@objArr = []

@posIndex = 0
end

def Add(someObj)
if (someObj.class.to_s.eql?(@defaultType))
puts("Adding the object")
@objArr.push(someObj)
else
puts("Not adding the object")
end
end

def Get
return @objArr
end

# Why don't you just use Array.At? :)
def GetItemAt(pos)
for obj in @objArr

if (@posIndex == pos.to_i)
@posIndex = 0
return obj
else
@posIndex = @posIndex +1
end
end
end
end
end

include Crypt
include Math
include Collections

cr = Cryptor.new
puts(cr.Encrypt("this is a very very very long string"))
puts(cr.Decrypt(cr.Encrypt("this is a very very very long string")))

puts("\n")

m1 = Arithmetic.new
m2 = Arithmetic.new
m3 = Arithmetic.new
m4 = Arithmetic.new
m5 = Arithmetic.new

# coll = ArrayList.new("Math::Arithmetic")
# or
coll = ArrayList.new(m1.class.to_s)

# All of the following would be added
coll.Add(m1)
coll.Add(m2)
coll.Add(m3)
coll.Add(m4)
coll.Add(m5)

puts("\n")

# This wouldn't be added
coll.Add(cr)

puts("\n")

i = 10

for obj in coll.Get
puts(obj.Add(4,i))
i = i+1
end

puts("\n")

puts("Number of items: ")
puts(coll.Get.length)

puts("\n")

# This would work
obj = coll.GetItemAt(3)
puts(obj.Add(4,10))

puts("\n")

# This wouldn't work
# As the obj would be undefined, an error would be
# generated at the top
obj = coll.GetItemAt(30)
puts(obj.Add(4,10))



Thanks and Happy coding :)

Thursday, November 5, 2009

Blackberry - How To : Solving the "Unable to Connect to the MDS Simulator Service" problem

I am back, after a loooooong time!! Let me get in to the post directly instead of unnecessary ramblings. I am working a mobile .Net web application and had to test the same in a Blackberry Simulator. The first time I started trying out, it was a breeze, the page opened up and I was able to browse the web site. But the next time I fired up my browser from the simulator, nothing seemed to work. I was getting the following error every time I fired up my brower (obviously my MDS was up and running :D ):

"Unable to connect to the selected mobile data service..."

I tried in vain to solve this problem by various means, but nothing seemed to work (lucky me!!).

Anyways, let me get to the solution part directly ;)

Say, you are running the 9530 simulator (BB JDE version 4.7.0). Open the "9530.bat" file that launches the 9530 version of the simulator. These batch file(s) would be located in application directory's simulator folder or in the directory where you installed the simulator. Open up this file in notepad

e.g.

@echo off
fledge.exe /app=Jvm.dll /handheld=9530 /session=9530 /app-param=DisableRegistration /app-param=JvmAlxConfigFile:9530.xml /data-port=0x4d44 /data-port=0x4d4e /pin=0x2100000A

Add the attribute /clear-flash after /pin=0x2100000A (i.e. towards the end of the file). So the modifed batch file would look like:

@echo off
fledge.exe /app=Jvm.dll /handheld=9530 /session=9530 /app-param=DisableRegistration /app-param=JvmAlxConfigFile:9530.xml /data-port=0x4d44 /data-port=0x4d4e /pin=0x2100000A /clear-flash


Now try launching the simulator and you should not be facing this problem again!!!

Let me tell you that this is just a temporary solution and I would be on the look out to find a concrete solution to this problem!

Happy coding ;)

Thursday, September 17, 2009

C# - An Interview w/ the Design Team

Hey Guys,

Check out this interview w/ the C# design team. It's cool! C# is one language I admire the most next to C/C++ ;) Enjoy!

http://channel9.msdn.com/posts/Charles/C-40-Meet-the-Design-Team/

Happy coding :)

Tuesday, August 11, 2009

Multiple Main() methods inside a project

Hey All,

I am back :)

If you try to compile a project having multiple Main() methods, the compiler will throw an error. The simplest solution in this case would be to comment out/ remove teh Main() methods apart from the one we currently wish to use.

If you do not want to delete the additional Main() methods, do this:

Choose Project -> Properties
Choose the "Application" tab
In this tab, there is a drop down list titled "Startup object"
Choose the class that contains the "entry point" that should be run, ignorin the remaining Main() methods

I found this out in a MS developer site and thought of posting it. Hope this helps somebody!

Happy Coding :)

~ Karthik

Thursday, July 23, 2009

Implementing Functions that Times out after a pre-defined time interval

Hi All!

Been a long time! I found this post by Ohad extremely useful. This is about implementing functions that will time out after a specified time interval. This behavior would be expected in various cases - for example - when you wait for a web service to respond to you. So in this case you may choose to time out after you wait for 's' seconds and try some other service, for instance.

This is the link -

http://weblogs.asp.net/israelio/archive/2004/06/19/159985.aspx

Happy coding!

~ Karthik

Friday, June 12, 2009

Debugging Windows Services

Hey Guys!

This post is some info. about windows services. There are lot of blog posts/tutorials floating around in the web that deals with creation of windows services. But this post deals with the methods to debug a windows service. After you start your service you can attach it to a debugger, so that you can debug a windows service! Conventionally it wouldn't be possible to debug a windows service by pressing F5, as you would have known. This is because services are run from the context of a services control manager.

Now let me stop these stories and get directly in to reality!!

In a nutshell, these are the steps to be followed:

* Develop the service
* Start the service
- Start / Control Panel / Administrative Services / Computer Management
- Click on Services and Applications / Services
- Scroll to your service
- Start it (Right click - start)
* Open your project for this service in VS.Net
* Set breakpoints (Obviously you cannot debug the onStart method or the Main method!)
* Choose Debug -> Processes
* Choose the windows service in the list of "Available Processes"
* Click on "Attach"

There you go!! Hope this helps somebody!!!

Want a "professional" view of the same? Refer to,

http://msdn.microsoft.com/en-us/library/7a50syb3(VS.80).aspx

Happy coding :)

Friday, June 5, 2009

Getting the Type of the current instance and Name of the method currently being executed

Hi Folks,

I am back, after a long time :) This post describes how we could get the name of the instance that is currently being executed and also the method name that is currently being executed.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace TestProject
{
class ToCall
{
public ToCall()
{
Console.WriteLine("This is " + this.GetType());
MethodBase method = MethodInfo.GetCurrentMethod();
string methodname = method.DeclaringType.FullName + "." + method.Name;
Console.WriteLine("Method name is " + methodname);

sample();
}

public void sample()
{
Console.WriteLine("This is " + this.GetType());
MethodBase method = MethodInfo.GetCurrentMethod();
string methodname = method.DeclaringType.FullName + "." + method.Name;
Console.WriteLine("Method name is " + methodname);
}
}

class Program
{
static void Main(string[] args)
{
ToCall d = new ToCall();
}
}
}


I will add some comments soon, just wanted to get started initially!!

Happy Coding :)