[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Having trouble converting to C# from Java

Hi,

I'm creating a EJB CartBean, and i used VS.NET 2003 to create a
C# Class library, how do i convert these codes (taken from Sun, copyrighted ref)
into C# and mark the BookException Serializable..

/* Taken from  Sun Microsystems. -> Copyright 2001 Sun Microsystems, Inc. All Rights Reserved. */

import java.util.*;
import javax.ejb.*;

public class CartBean {

   String customerName;
   String customerId;
   Vector contents;

   public void ejbCreate(String person) throws CreateException {
      if (person == null) {
        throw new CreateException("Null person not allowed.");
      }
      else {
         customerName = person;
      }

      customerId = "0";
      contents = new Vector();
   }

   public void ejbCreate(String person, String id) throws CreateException {
      if (person == null) {
        throw new CreateException("Null person not allowed.");
      }
      else {
         customerName = person;
      }

      customerId = id;

      contents = new Vector();
   }

   public void addBook(String title) {
      contents.addElement(title);
   }

   public void removeBook(String title) throws BookException {
      boolean result = contents.removeElement(title);
      if (result == false) {
         throw new BookException(title + " not in cart.");
      }
   }

   public Vector getContents() {
      return contents;
   }

   public CartBean() {}
   public void ejbRemove() {}
   public void ejbActivate() {}
   public void ejbPassivate() {}
   public void setSessionContext(SessionContext sc) {}
}

o      Implement the BookException class (BookException.java) as follows:

/* Taken from  Sun Microsystems. -> Copyright 2001 Sun Microsystems, Inc. All Rights Reserved. */

public class BookException extends Exception {

    public BookException() {
    }

    public BookException(String msg) {
        super(msg);
    }
}

0
jedistar
Asked:
jedistar
  • 2
1 Solution
 
nguyenvinhtuCommented:
using System;
using System.Collections;

      /// <summary>
      /// Summary description for CartBean.
      /// </summary>
      /* Taken from  Sun Microsystems. -> Copyright 2001 Sun Microsystems, Inc. All Rights Reserved. */
      public class CartBean {
            string customerName;
            string customerId;
            ArrayList contents;//Vector contents;

            //CartBean default Constructor
            public CartBean() {}

            public void ejbCreate(string person)
            {
                  if (person == null) {
                                                               //Please make sure that you had a reference to CreateException before compile this code
                        throw new CreateException("Null person not allowed.");
                  }
                  else {
                        customerName = person;
                  }

                  customerId = "0";
                  contents = new ArrayList();
            }

            public void ejbCreate(string person, string id)
            {
                  if (person == null) {
                        throw new CreateException("Null person not allowed.");
                  }
                  else {
                        customerName = person;
                  }

                  customerId = id;

                  contents = new ArrayList();
            }

            public void addBook(String title) {
                  contents.Add(title);
            }

            public void removeBook(String title)
            {
                  try
                  {
                        contents.Remove(title);
                  }
                  catch(Exception)
                  {
                        throw new BookException(title + " not in cart.");
                  }
            }

            public ArrayList getContents() {
                  return contents;
            }
      
            //The following methods come from EJB, and you are overriding them.
            /*public void ejbRemove() {}
            public void ejbActivate() {}
            public void ejbPassivate() {}
            public void setSessionContext(SessionContext sc) {}*/
}

            //Implement the BookException class (BookException.java) as follows:

            /* Taken from  Sun Microsystems. -> Copyright 2001 Sun Microsystems, Inc. All Rights Reserved. */

      public class BookException : Exception
      {
            //Book Exception default Constructor
            public BookException(){}

            public BookException(string msg) : base(msg)
            {
            }
      }
0
 
nguyenvinhtuCommented:
Put the attribute [Serializable] before any class name declaration to Serialize it. The attribute will Serialize the class as you want to.
For e.g:
[Serializable]
public class CartBean {...}
[Serializable]
public class BookException : Exception {...}
Sincerely yours,
Danny
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now