Wednesday, May 31, 2006

some c# stuff

I have been for sometime working on a .NET project where we are using c# to make web service calls and populate an excel sheet. I would be blogging my experience while working on this project as it relates to .net, c#, and vsto (excel mainly). I have mostly J2EE experience so this is a very interesing as well as challenging experience. I would also like to blog some cookie cutter code or just what I think is useful.

Some .net training notes

Below are some notes that I took while attending .NET training which is useful.

there is hard cast and soft cast, examples below

int x = (int) obj; //hard cast, so if obj is not of type int there will be class cast exception
int x = obj as int; //soft cast, in this case if obj is not of type int than x will be null

Boxing C# has a feature called boxing and unboxing which is basically copying value types from stack
to object on heap and vice versa. An example is below:

int x = 3;
Object obj = x; //boxing
int y = (int) obj; //un-boxing.

Second more subtle example below:

Console.WriteLine(" {0} ", 3); //boxing

Boxing from performance perspective is not good so best to avoid. So for the above
examples change as below.

int x = 3;
Object obj = x.ToString();
int y = int.Parse((string)obj);

Console.WriteLine(" {0} ", 3.ToString());

Use command line utility, ildasm, to check generated MSIL code if boxing code is generated.

ildasm <name of dll>

WMI -- windows management and instrumentation, can be used for performance analysis.

keywords: constructor chaining, virtual chain of methods, sealed classes,
marker interfaces (interface with no methods)

use "is" in C# is same as "instanceof" in java

in c# it is possible to hide methods of implemented interfaces by using interface
name for method declaration. So to use it you have to cast object to the specific

DataSet -- reads and remembers -- composed of DataTables. DataTables composed of
DataRows and DataColumns

DataReader -- just reads

DataSet uses DataReader to get the data.

For very small amount of data use DataReader to get the data and store in some
collection object like arraylist, etc instead of using DataSet.

With DataReader you have to explicity open/close connection but with
DataSet since you are using DataAdapter it will open/close connection

best practices --

DataSets are typed and un-typed. If you have xml xchema (or can create one) than
DataSets can be created out of this schema. typed are faster and compiler checked.
Also, you are using GUI to auto generate DataSet code.

DataTables and DataSets can keep track of changes (diffgram).

Use Transaction objects if rollback is needed.

Config file
App.config is default, do not chnage the name. This file can be added to projects. Works only
for .exe and not for .dll

Web Services
proxies stand between client code and web service.
.asmx files represent web service in .net world.
.net has something similar to java annotations called "attributes" (just declare above method definition).
you can type the location of .asmx file in browser which can be used for testing.
.asmx?wsdl will return the wsdl file
wsdl.exe /l:CS /o:c:\testproxy.cs <wsdl-url> //will generate client proxy code, another way is to use webreference from IDE.
for webrefernce there is an option to update web service
for webservice proxy you can set credential, timeout, proxy, etc.

to run .xls without running dll run the .xls by pressing shift key

if a solution has only execute permission (and not full trust) than web service call will fail.

PIO code is under c:\windows\assembly folder, format of the file name is

+= is used for event registration called delegation

-= is used for de-registration

if += is called 3 times and -= is called once than the registration of event is still ther, you would
need to call -= 2 more times to actually un-register.

dlls get downloaded in "documents and settings"...."application data" directory.

AssemblyInfo.cs has version number, [assembly: AssemblyVersion(" 1.0.*")], which is used for creating
different versions of the dll. this version gets referenced in c:\windows\assembly\download folder

to combine multipe ranges can use "union"

RecordSet in un-managed code is equivalent to DataSet in .net, there are apis to copy from one to another.

To add a windows form to excel, add a form and than from officecodebehind create an instance of

Also, since windows form is built into .net no need to worry about registering events like
onclick, etc but for excel you have to worry.

When need to pass data from form to OfficeCodeBehind, it is possible to pass OfficeCodeBehind
to form class or other way around. Second approach is preferred where form class provides public
api to access data which is than called by OfficeCodeBehind.

Delegates holds references to callback methods

delegates can be called asynchronously.

difference between ABO and ABO.NET

At a high level ABO.NET is an enhancement over ABO as it provides support for disconnected mode and is integrated with XML. Will make more enhancement to this blog as I learn more. One more note is that ABO.NET looks very similar to SDO (Service Data Object) in J2EE world.