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

Tuesday, January 31, 2012

Tuples in Java

I came across the concept of tuples in C# which is quite interesting and useful. A tuple is a data structure that has a specific number and sequence of elements.  C# introduced this feature since .NET 3.5. It supports upto 8 generic objects. Unfortunately Java haven't got one yet.


In my current project there was a need for a similar Data-structure, where I had to pass back a large amount of data of a particular object. But I was interested only in just two properties of that object. I could have created a proxy class, but that would have ben a sort of over engineering (Which I don't want to do). At that time I thought of a simple attempt to write a Tuple. I wrote one and it's really reusable and clean. You can find the  the code snippet below.

public abstract class Tuple<T1, T2, T3, T4>{


    protected T1 t1;
    protected T2 t2;
    protected T3 t3;
    protected T4 t4;
    
    public static    < T1, T2> Tuple <T1, T2, ?, ?> create(T1 aT1, T2 aT2){
        return new MultipleTuple(aT1, aT2);
    }
    
    public  T1 getFirstItem(){
        return t1;
    }
    
    public T2 getSecondItem(){
        return t2;
    }
    
    public T3 getThirdItem(){
        return t3;
    }
    
    public T4 getFourthItem(){
        return t4;
    }

}

A MultiTuple


final class MultipleTuple<T1, T2> extends Tuple {
    
     MultipleTuple(T1 aT1, T2 aT2) {
        t1 = aT1;
        t2 = aT2;
    }
}


Example


String employee = "Diego Maradona";
double renumeration = 56.5;
        
Tuple  tuple = Tuple.create(employee, renumeration);


System.out.println(tuple.getFirstItem());
System.out.println(tuple.getSecondItem());




You can extend as many classes you want to create. You might need to restrict the visiblilty and make them final so that it's secure. Or you could even extend the MultipleTuple(Hoping it's no longer final) class for the next tuples. Make sure the create method is overloaded with the right parameters.




Happy Hacking.

Monday, January 04, 2010

String to Stream in C#

There are multiple ways, best two:

Us the System.IO.StringReader.

//Good when you want to preserve the UTL character
byte[] bytesFromString = System.Text.Encoding.UTF8.GetBytes(xml);
System.IO.Stream stream = new System.IO.MemoryStream(xmlBytes);

Tuesday, December 22, 2009

CodeGenerator From XML Schemas using Visual Studion XSD.exe

XSD.exe is a nice tool for .NET Code-Generation from an existing xml Schema. It's very similar like its java counterpart (JAXB- XJC)


Syntax:

Visual Studio 9.0\VC>xsd /c /out:c:\aneesh\classes c:\Aneesh\dataTypes.xsd

/c stands for generating classes
/out specifies the output directory
/namespace can give a specific namespace for the generated class


A bit complex situation, with multiple schema's

Scenario:

You have an XSD, objectrSchema.xsd and you have another XSD dataTypes.xsd. Some of the DataTypes in the dataTypes.xsd are used in the objectSchema.xsd. In this situation if you run the above command, it will give you a straight error:

Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.
Error: Error generating classes for schema 'c:\ Aneesh\objectSchema.xsd '.

We can overcome this situation with a few tweaks. Thought I searched google but didn’t yield any satisfactory results. These are thing steps I did to make it work.

Step 1:

Import the dataTypes.xsd into the objectrSchema.xsd using the More details of XD IMport can be found here.

Note that the “namespace=http://www.aneesh.com/datatypes “should be the same as the one defined in dataTypes.xsd

Step 2:

Finally hit the command:

Visual Studio 9.0\VC>xsd /c /out:c:\aneesh\classes c:\Aneesh\dataTypes.xsd c:\Aneesh\objectSchema.xsd

Here you go. Your code is generated. Happy Hacking!

More Reading


Friday, November 30, 2007

Back to .NET

After a gap of 3 years, I am again back to dotNET. The company for which I ma consulting now, have a small requirement to do a small project in dotNET 2.0. My manager asked me if I am interested....and thats it.

I downloaded the Microsoft Visual C# 2005 & Microsoft Visual Web Developer 2005. .Net 2.0 has some good features such as the partial class, and the web builder has got some kind of template built in with it, which really eases the coding time. Well Done Microsoft. But the C# studio has a bit more to mature. Earlier days, when somebody moves into java from vc++,VB, it was a pain in the ***. But now it has turned the other way round. Somebody who was using Eclipse till yesterday and today on seeing the VC#, feels pity.

After installing IIS, I thought I am done, and updated my taklist as "Environment setup completed". Suddenly, like a thunderstorm in the summer, I got this error below :


The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

A name was started with an invalid character. Error processing resource 'http://localhost/pages/Default.aspx'. Line 1, Pos...

<%@ Page Language="C#" %>
-^


I didnt have any clue of this error, until I knew that, in the IIS properties, we had to set the ASP.NET version. It is/was blank by default.

MyComputer-RightClick->Manage->Services and pplications ->IIS->Default Website->-Properties->ASP.NET(TAB)->ASP.NET version

There you go. Restart IIS.

Here comes hailstorm, the next error, when I tried to access.


Failed to access IIS metabase.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.Hosting.HostingEnvironmentException: Failed to access IIS metabase.

The process account used to run ASP.NET must have read access to the IIS metabase (e.g. IIS://servername/W3SVC). For information on modifying metabase permissions, please see http://support.microsoft.com/?kbid=267904.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HostingEnvironmentException: Failed to access IIS metabase.]
System.Web.Configuration.MetabaseServerConfig.MapPathCaching(String siteID, VirtualPath path) +3609834
System.Web.Configuration.MetabaseServerConfig.System.Web.Configuration.IConfigMapPath2.MapPath(String siteID, VirtualPath vpath) +9
System.Web.Hosting.HostingEnvironment.MapPathActual(VirtualPath virtualPath, Boolean permitNull) +169
System.Web.CachedPathData.GetConfigPathData(String configPath) +382
System.Web.CachedPathData.GetConfigPathData(String configPath) +243
System.Web.CachedPathData.GetApplicationPathData() +68
System.Web.CachedPathData.GetVirtualPathData(VirtualPath virtualPath, Boolean permitPathsOutsideApp) +3503459
System.Web.Configuration.RuntimeConfig.GetLKGRuntimeConfig(VirtualPath path) +189



On Successfull googly, MSDN guided me to reinstall dotNET framework :( . No Way, I don't want my manager to be behind the bars, after killing me....Blistering barnacles and thundering typhoons. I finally got the solution.

Issue:
The IIS was installed after .NET framework was installed. SO the IIS should be regestered for ASP.NET application unless, and for security rights.

Solution:


C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_regiis.exe -i
Where "i" is Install this version of ASP.NET and update scriptmaps at the
IIS metabase root and for all scriptmaps below the
root. Existing scriptmaps of lower version are upgraded to this version.

Bingo!!!! I am back to dotNET. Pause J2ee for a while :)