2008年8月26日 星期二

c# 與 vb.net 互轉的網站

http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx ,看該來不錯。
一個簡單的例子,

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Configuration;
using BankPro.EI2.BusinessComponents;

namespace EI2.Utility
{
  public class CommonUtil
  {
    /// 
    /// 虛擬目錄名稱
    /// 
    public static string EInvoiceVDName
    {
      get
      {
        return WebConfigurationManager.AppSettings["VDName"];
      }
    }

    /// 
    /// 登入者所屬公司的 CompanyOid
    /// 
    public static int LoginUserCompanyOid
    {
      get
      {
        return UserBC.GetUserCompanyOidByAccount(
          HttpContext.Current.User.Identity.Name).Value;
      }
    }
  }
}

經過轉換後,可以轉成 VB.NET 無慮。
Imports System 
Imports System.Data 
Imports System.Configuration 
Imports System.Linq 
Imports System.Web 
Imports System.Web.Security 
Imports System.Web.UI 
Imports System.Web.UI.HtmlControls 
Imports System.Web.UI.WebControls 
Imports System.Web.UI.WebControls.WebParts 
Imports System.Xml.Linq 
Imports System.Web.Configuration 
Imports BankPro.EI2.BusinessComponents 

Namespace EI2.Utility 
    Public Class CommonUtil 
        '''  
        ''' 虛擬目錄名稱 
        '''  
        Public Shared ReadOnly Property EInvoiceVDName() As String 
            Get 
                Return WebConfigurationManager.AppSettings("VDName") 
            End Get 
        End Property 
        
        '''  
        ''' 登入者所屬公司的 CompanyOid 
        '''  
        Public Shared ReadOnly Property LoginUserCompanyOid() As Integer 
            Get 
                Return UserBC.GetUserCompanyOidByAccount(HttpContext.Current.User.Identity.Name).Value 
            End Get 
        End Property 
    End Class 
End Namespace 
但是,碰到新的 c# 3.0 (linq 的語法就會失敗了) 例如
protected void gvInvoices_DataBound(object sender, EventArgs e)
    {
      foreach (GridViewRow row in gvInvoices.Rows)
      {
        for (int i = 0; i < gvInvoices.Columns.Count; i++)
        {
          DataControlField item = gvInvoices.Columns[i];
          BoundField boundField = item as BoundField;
          if ((boundField != null) && string.IsNullOrEmpty(boundField.DataField))
          {
            string headerText = item.HeaderText;
            int oid = Convert.ToInt32(gvInvoices.DataKeys[row.RowIndex].Value);
            var tmpType = tmpTypes.SingleOrDefault(t => t.OID == oid && t.GroupName == headerText);
            if ((tmpType != null) && (!string.IsNullOrEmpty(tmpType.ObjectValue)))
            {
              row.Cells[i].Text = tmpType.ObjectValue;
            }
          }
        }
      }

    }
會產生 Conversion was attempted, however the following errors were reported: -- line 1 col 11: invalid TypeDecl

沒有留言:

Share with Facebook