Jul 15 2009

How to write C# code?

Published by Kevin Gao under C# & Java conversion

This article is a part of CSharpJavaMerger Framework, an open source project. Join us now to make it better!
  • Use “base” instead of “super”.
  • Use “namespace” instead of “package”.
  • Use “using” instead of “import”. You can comment the import section and use the Resolve function of Visual Studio 2008 to add “using” automatically.
  • Omit “throw Exception” in the end of the functions.
  • Using “delegate callback” when accessing UI resources through workers threads.Below is the example of closing dialog boxes in workers thread:
    • Java
      public void closeDialog()
      {
        m_isCanceled = false;
        Display.getDefault().syncExec(new Runnable()
        {
          public void run()
          {
            Shell shell = getShell();
            if (shell != null)
            {
              if (!shell.isDisposed())
              {
                shell.dispose();
              }
            }
          }
        });
      }
    • C#
      delegate void GUICallback(string pos);
      //Function declaration.
      //Please note the prototype of the function is certain:
      //the return value is void and the input parameter is string.
      private void closeDialogInner(string strMessage)
      {
        if (this.InvokeRequired)
        //This is a system function.
        //If this function needs to access some control resources,
        //please change "this" to the name of the control,
        //for example: this.m_btnOK.InvokeRequired.
        {
          GUICallback callback = new GUICallback(closeDialogInner);
          this.Invoke(callback, new object[] { strMessage });
        }
        else
        {
          this.Close();
        }
      }
  • Replace Display.getDefault().getActiveShell() with Form.ActiveForm(). This function may be not used much.
  • Use “is” instead of “instanceof”.
  • Replace Integer.MAX_VALUE with int.MaxValue and replace Long.MAX_VALUE with long.MaxValue.
  • An important thing: please keep the variables of interfaces’ controls consistent.

    Don’t change logic. Don’t comment codes if you don’t need to. Please try to convert a function at a dash.
    These are the recommended steps:

    • Transfer util class.
    • Transfer IView class.
    • Draw dialog boxes. Please make sure the variables of the control names in C# and Java are the same.
    • Implement the functions in IView.
    • Convert the controller classes.
      • Convert thread classes
      • Convert the Dialog control classes
    • Add events for controls
    • Copy the implementing code of Copy event.
      One thing to notice: The constructor of Form calls InitializeComponent() directly, but in Java, createContents is called only when openDialob, so the time to create controls are different in C# and Java. The time to call FormLoad(…) method in C# is similar to that of createContents in Java.
  • Replace “final” in local constant with “const”.
  • Omit “final” in parameters.
  • Use “public const” instead of “public final static”.
  • Replace “length” with “Length” in the member of array’s length.
  • Using Long:
    • Using Long= System.Int64;
    • Integer.MaxValue->Integer.MAX_VALUE
  • Using Integer:
    • Using Integer = System.Int32
    • Long.MaxValue->Long.MAX_VALUE

 

Links:
Previous article >>>>: An example
Next article >>>>: How to write Java code?
WCF & Java Interop series home page: CSharpJavaMerger Framework

One response so far

Jul 15 2009

An example

Published by Kevin Gao under C# & Java conversion

This article is a part of CSharpJavaMerger Framework, an open source project. Join us now to make it better!

Below is a piece of code of a method. In the sample, CString, a class defined in the CSharpJavaMerger Framework, is used instead of String. This way, the method AddTwoValues can be used in both C# and Java.

public void AddTwoValues()
{
    try
    {
        CString strValue1 = m_mainForm.GetFirstTextBoxValue();
        CString strValue2 = m_mainForm.GetSecondTextBoxValue();

        int iValue1 = strValue1.ToInteger();
        int iValue2 = strValue2.ToInteger();

        int iResult = iValue1 + iValue2;
        CString strShowResult = CString.ValueOf(iResult);
        m_mainForm.ShowResult(strShowResult);

        CString strSaveResult = new CString(strValue1.GetValue() + " + " + strValue2.GetValue() + " = " + strShowResult.GetValue() + "\n");
        m_mainForm.SaveResult(strSaveResult);
    }
    catch (Exception ee)
    {
        m_mainForm.SaveResult(new CString(ee.Message + "\n"));
        m_mainForm.ShowResult(new CString("Error"));
    }
}

 

Links:
Previous article >>>>: Architecture
Next article >>>>: How to write C# code?
WCF & Java Interop series home page: CSharpJavaMerger Framework

No responses yet

Jul 15 2009

Architecture

Published by Kevin Gao under C# & Java conversion

This article is a part of CSharpJavaMerger Framework, an open source project. Join us now to make it better!

CSharpJavaMerger Framework Architecture
(CSharpJavaMerger Framework Architecture)

 

Links:
Previous article >>>>: I’m a Java developer. How to use CSharpJavaMerger?
Next article >>>>: An example
WCF & Java Interop series home page: CSharpJavaMerger Framework

No responses yet

Jul 15 2009

I’m a Java developer. How to use CSharpJavaMerger?

Published by Kevin Gao under C# & Java conversion

This article is a part of CSharpJavaMerger Framework, an open source project. Join us now to make it better!

If you are a Java developer, you can use CSharpJavaMerger Framework.java to develop the Java code and then the code can be used by C# with little modifications with the help of CSharpJavaMerger Framework.dll. See How to write Java code for more info.

 

Links:
Previous article >>>>: I’m a C# developer. How to use CSharpJavaMerger?
Next article >>>>: Architecture
WCF & Java Interop series home page: CSharpJavaMerger Framework

No responses yet

Jul 15 2009

I’m a C# developer. How to use CSharpJavaMerger?

Published by Kevin Gao under C# & Java conversion

This article is a part of CSharpJavaMerger Framework, an open source project. Join us now to make it better!

If you are a C# developer, you can use CSharpJavaMerger Framework.dll for C# coding so that the C# code can be used by Java easily later. After you complete the C# code, you can change the .cs file extension to be .java, import the CSharpJavaMerger Framework.java and do compilation. There are some differences between C# and Java that are not consolidated in the framework, such as C# uses namespace while Java uses package. You can make such changes manually. See How to write C# code for more info.

 

Links:
Previous article >>>>: Get source code
Next article >>>>: I’m a Java developer. How to use CSharpJavaMerger?
WCF & Java Interop series home page: CSharpJavaMerger Framework

No responses yet

Jul 15 2009

Get source code

Published by Kevin Gao under C# & Java conversion

This article is a part of CSharpJavaMerger Framework, an open source project. Join us now to make it better!

All the source code of CSharpJavaMerger Framework is stored in SCM Anywere Hosted repositories.

You can download the SCM Anywhere Hosted Client from link below and use the following account to log in the hosted server to get source code.

Downlad SCM Anywhere Hosted Client
Organization ID: 101968
User Name: Dynamsoft
Password: scm

You can get all the source code from the Source Control Explorer.

 

Links:
Previous article >>>>: Download
Next article >>>>: I’m a C# developer. How to use CSharpJavaMerger?
WCF & Java Interop series home page: CSharpJavaMerger Framework

One response so far

« Prev - Next »

Version Control Software/System | Source Control Software/System | Software Configuration Management | SCM Hosting Solution | Bug Tracking System
SourceSafe (VSS) Replacement/Alternative | SourceSafe (VSS) Hosting | SourceSafe (VSS) Remote/Web/Internet Access | Scanner COM
Customer Service Software | Live Chat | Live Help | Forum Software | Knowledge Base Software | Newsletter Email Marketing Software