Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, July 02, 2009

Record Value Frequency In SSIS

I blogged last week about a problem on Record Value Frequency in SSIS. I suggested in the blog post on how to go about taking care of this.

“The only way I could see out of this is to write a CLR function to achieve this which I am feeling so lazy to do but still has to be done.”

This I finally had time to do and pasted below is the simple CLR User Defined Functions (UDF)

static readonly Dictionary<SqlString, SqlInt32> myDic = new Dictionary<SqlString, SqlInt32>();



 



    [Microsoft.SqlServer.Server.SqlFunction]



    public static SqlString SvSequence(SqlString colValue)



    {



        SqlInt32 nextValue = 1;



        if (myDic.ContainsKey(colValue))



        {



            myDic[colValue] += 1;



            nextValue = myDic[colValue];



 



        }



        else



            myDic.Add(colValue, 1);



        // Put your code here



        return nextValue.ToSqlString();



    }



    [Microsoft.SqlServer.Server.SqlFunction]



    public static SqlString RestartSequence()



    {



        myDic.Clear();



        return new SqlString();



    }




Notice the readonly used in the static declaration for the dictionary. This is enforced by the compiler for static class member as a requirement. I also had to create a reset function which IMHO is not that cool but I need a way to reset this as the value kept increasing each time the UDF is called.





dbo.RestartSequence ;



select AccountId, dbo.SvSequence(AccountId) from dump


Monday, May 25, 2009

Tuple in C# 4.0

Tuple is defined as a Sequence (or ordered list) of finite length. Other definitions exist but I particularly like to see it as such.

I have been craving for this feature ever since seen in Cw(C-Omega). This is particularly useful when you want to return multiple values from a function/method especially when they are not related enough to want to put then in a class or struct. Some developers like myself result to using multiple out parameters. This gets messy for methods that have return type and so inconsistence in returning values from such methods.

   1: var x = DoSomething(out int a, out int b);




The compiler is smart enough to detect when you have unmatched number of arguments





   1: var myTuple = new Tuple<int, int, int>(0, 1); 




or request for a member beyond it's definition





   1: Console.WriteLine(myTuple.Item4); 




Tuples in C# 4.0 lacks the ability to get the Length/ Count/Size value even though the underlying interface ITuple specifies this. I guess the C# team pulled this out to prevent us from seeing or using this as an Array or Collections.



In the Beta 1 Release, I tried getting my hands dirty with this and was shocked on a discovery. Something about an error on the eight argument of a tuple being requeired to be ITuple. I searched online for this and came across an interesting Post about infinite Tuple (Tuple with eight parameters in constructor)






   1: Tuple<int, int, int, int, int, int, int, int> 




http://blog.dynamicprogrammer.com/2009/05/25/TupleANewTypeOnNet40.aspx

Friday, May 22, 2009

Implicitly Type Local Variable & Multiple Declarators

It is perfectly legal in C# to have declarations written this

int x, y;
which with initialized values is
int x= 5, y=6;

Alas, the C# compiler rejects this
var x=5, y=6;

This is because the C# Compiler does not allow multiple declarators on the var keyword. I wondered where this is not allowed and if you ask me so much restrictions on the use of the var keyword (can't use as method return type or method arguments).

A recent mail from Eric Lippert clarifies this. His reasons for why this feature was made illegal was that of removal of confusion. In his own words

object M() {}
string N() {}
var x = M(), y = N();
about fifty percent of people thought that the “obvious” meaning was
object x = M();
string y = N();
That is, the “obvious” meaning is “split the two apart and do them separately”.
About fifty percent thought that the “obvious” meaning was
object x = M(), y = N();
That is, “find the thing that you could replace the ‘var’ with and pretend it was there”.
No matter which we picked, half our users would think that we did it wrong. So we cut that feature.  A “var” declaration can only have one variable declared

Thursday, February 05, 2009

.Net and Java Inteoperability

I recently was saddled with the task of developing a component for a Telecoms (GSM) company. This is a company that has huge investment in Java-based applications on Sun Solaris boxes. The requirement of the component warrants a need to connect my ASP.Net based application to an existing connection pooling java-based socket application.

Immediately, interoperability comes to mind. Which is best? Some of the thots that came to mind are

- Use JNBridge (license cost)
- use a message based intermediary (MSMQ - requires modification of Java component which not allowed)
There is a whole lot of options in this space which I will not want to go into in this blog

- IKVM.Net (read all about this but not sure it works)

Other constraint is I had budget constraint that knocks off JNBridge and also not allowed to modify the service component as some other production applications depends on it. Whick left me with IKVM.Net.

First, I wrote a Java client application that uses this service, compiled and tested it well, then I use IKVM to generate a .Net dll off this java class file and Voila!, task got done!. It's no trouble getting this into my ASP.Net bin folder.


> javac SVClient
> ikvmc SVClient -target:library


Refer to IKVM.Net tutorial

The great thing about IKVM is that it works without any dependency to the original java class file. It's a complete conversion of the java code.

I got curious a bit and opened the dll in Red Gate's .Net Reflector and learned a lot on what underlying code.

Saturday, May 15, 2004

Chat with a C# newbie (Convert)



xxxxxxx: you're using C#?

onawoleco: Yeah! and its sweet

onawoleco:

onawoleco: what would u recommend?

xxxxxxx: recommend?? how do you mean???

onawoleco: I was wondering y u asked if I used C#

onawoleco: So i thot u had another language in mind

xxxxxxx: you need to use what you "think in".you see i was trying to learn C# but i

onawoleco: So what do u think in?

xxxxxxx: but i need to know its advantages b4 moving in...

onawoleco: What do u "Think In"

onawoleco: It has all the advantage of Java

onawoleco: its smoother and simpler

onawoleco: but better features

onawoleco: and soon Generics will be introduced which is a better implementation of Templates in C++

xxxxxxx: actually "C++"

xxxxxxx: but i suspect there is a very strong similarity b/w them. thats why i am considering it

onawoleco: I do think in C++ too but commercial product always have stringent deadline of which C++ is not appropriate

onawoleco: There is

onawoleco: Same syntax

xxxxxxx: very much like visual c++ (i suspect)

onawoleco: Only u cannot will miss some features in C++

onawoleco: though most has better replacement in it

onawoleco: Not really

onawoleco: VC++ -> MFC is trying to give C++ an OOP look

onawoleco: everything in C# is an object

onawoleco: there is nothing like primitives

onawoleco: like u can say 3.ToString()

onawoleco: This is impossible in C++

onawoleco: but in C# numerics are classes too

onawoleco: Sorry objects too

xxxxxxx: what is my fastest way of learning C#?

onawoleco: Get a book on it

onawoleco: Thinking in C# could be okay

onawoleco: read from the numerous articles on the internet

onawoleco: I learnt a lot thru that way

onawoleco: and join a .Net user group

onawoleco: I am starting a very strong one in Naija soon

xxxxxxx: like which group?

onawoleco: Check www.gotdotnet.com

onawoleco: U will find numerous leads

xxxxxxx: or which website has good tutorials?

onawoleco: read articles from www.codeproject.com, www.theserverside.com

onawoleco: Gotdotnet supports MS passport

onawoleco: all u will use is ur MSN/Hotmail ID

onawoleco: The other is quite easy to get one

xxxxxxx: so C# creates stand-alone applications just like MFC does?

onawoleco: Better and Quicker than MFC

onawoleco: .Net compiled codes are not in Binaries like C++

onawoleco: and MFC alike

onawoleco: C# compiled codes are in IL

onawoleco: which is more like Java bytecodes

onawoleco: but better than Java bytecodes 'cos it is in itself OO

onawoleco: This what the .Net CLR understands and execute

onawoleco: it actually uses JIT

onawoleco: Just-In-Time compilation

onawoleco: which is an optmized compilation anyway

onawoleco: so u may experience the running slow the first time u execute

onawoleco: subsequent time, its faster

onawoleco: The IL made it possible to have language agnostics on .Net

onawoleco: which means it is language neutral

xxxxxxx: this will only run on systems which have .Net installed?

onawoleco: U can program in any language as long as there is compiler to compile to IL

onawoleco: Sure on systems with .Net framework

onawoleco: Just as u cannot run a java code without a JRE

onawoleco: This gives u lighter exectables

xxxxxxx: what is IL?

onawoleco: Intermediate Language

xxxxxxx: .Net can be installed on Win98?

onawoleco: Sure!

xxxxxxx: i will start off tonight! have some kind of tutorials which i save on my notebook about five weeks ago

onawoleco: C# also comes with a garbage collector

onawoleco: better than Java GC

onawoleco: Gooooooooooooooooo

onawoleco: Feel free to ask any que. Will sure like to help

onawoleco: and u can join my group

xxxxxxx: i have never understood that concept.. "garbage collector"

onawoleco: I will even need u to enter Ife well

onawoleco: as soon as I have the draft plan

onawoleco: I will let u see it

xxxxxxx: is it like clearing up unallocated memory?

onawoleco: In C++ u have what u call destructors

onawoleco: yeah it clears unused memory

onawoleco: U sure know what u used destructors for

onawoleco: but sometimes, developers get careless

onawoleco: and forget to clear up allocated memories

xxxxxxx: i mean like freeing allocated memory after use?

onawoleco: which leads to memory leakage

onawoleco: Not immediately after use

onawoleco: at specific time determined by the system

onawoleco: The concept of pointers make momory leakage even more on C++

onawoleco: which is y C# and Java avoided pointers

onawoleco: Though C# provided a workaround under the unbrella of DELEGATES

onawoleco: thats another powerful concept in C#

xxxxxxx: then i will start up with c#, one has to update with time

onawoleco: Sure

onawoleco: thats y I made the decision sometimes ago

onawoleco: even though I still code in VC++ and VC++.Net