Jul 15 2009

How to write C# code?

Published by Kevin Gao at 10:52 pm 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

One Response to “How to write C# code?”

  1. VIJAYon 28 Dec 2009 at 1:23 am

    Wat are u trying to tell us .. we cant get any convertion
    On the whole this is dubaakoor site.

Trackback URI | Comments RSS

Leave a Reply

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