ASP.NET(C#与VB对照表)

这几天要做一个FLASH与ASP.NET的项目,就研究了以下ASP.NET的基本语法,以前就不喜欢VB的语法,没有规范性,所以决心借这次机会接触一下C#,感觉有了C#编写出来的东西规矩多了。找到一张C#与VB对照表,整理了一下,粘出来给大家分享。

ASP+内置支持3种语言:C#Visual Basic(注意不是VBScript),JScript。下面列出一个简单的C#VB语法对照表,看看你到底喜欢那个?
C#
语法                           VB 语法

定义变量
int x;
                 
        Dim x As Integer
String s;
               
           Dim s As String
String s1, s2;
             
           Dim s1, s2 As String
Object o;
               
         Dim o ‘Implicitly Object
Object obj = new Object();
                    Dim obj As New Object()
public String name;
                      Public name As String 

输出内容

Response.Write("foo");                   Response.Write("foo") 

注释

// This is a comment                        ‘ This is a comment
/* This is a multi-line comment */
            ‘ This is a multi-line comment 

读取数据集合数组

String s=Request.QueryString["Name"];                    Dim s, value As String
value = Request.Cookies("Key").Value
    
      s = Request.QueryString("Name")
String value=Request.Cookies["key"];
 
                 ‘Note that default non-indexed properties
                        ‘must be explicitly named in VB 
定义简单数据集

public String name {                                                  Public Property Name As String
get {                                                                           Get
                                                                              
return …;                                                                    Return   …;
}                                                                                 End Get
set {                                                                           Set
… = value;                                                                  … = Value;
}                                                                                End Set
}
 
                                                                            End Property

数组
                                                                                   ‘ Array of unspecified bounds (NA in C#)
String[] a = new String[3];                                         Dim a() As String
a[0] = "1";                                                                  a(0,0) = "1"
a[1] = "2";                                                                  a(1,0) = "2"
a[2] = "3”;                                                                  a(2,0) = "3"
String[][] a = new String[3][3];                                 Dim a(,) As String
a[0][0] = "1";                                                             a(0,0) = "1"
a[1][0] = "2";                                                             a(1,0) = "2"
a[2][0] = "3";
 
                                                         a(2,0) = "3"
                                                                                  Dim a(3) As String
                                                                                  a(0) = "1"
                                                                                  a(1) = "2"
                                                                                  a(2) = "3"
                                                                                  Dim a(3,3) As String
                                                                                  a(0,0) = "1"
                                                                                  a(1,0) = "2" 
                                                                                  a(2,0) = "3"

初始化变量

String s = "Hello World";                                           Dim s As String = "Hello World"
int i = 1                                                                       Dim i As Integer = 1
double[] a = { 3.00, 4.00, 5.00 };
 
                           Dim a() As Double = { 3.00, 4.00, 5.00 }

If 结构

if (Request.QueryString != null) {                            If Not (Request.QueryString = Null)
                                                                              
}
 
                                                                            End If

Case 结构

switch (FirstName){                                                  Select (FirstName)
case "John" :                                                              case "John" :
                                                                              
break;
case "Paul" :                                                               case "Paul" :
                                                                              
break;
case "Ringo" :                                                            case "Ringo" :
                                                                                
break;
}
 
                                                                            End Select

For 循环

for (int i=0; i<3; i++)                                                 Dim I As Integer
a(i) = "test";
 
                                                           For I = 0 To 2
                             a(I) = "test"
                  
           Next

While 循环

int i = 0;                                                                     Dim I As Integer
while (i<3) {                                                              I = 0 Do While I ❤
 

Console.WriteLine(i.ToString());                              Response.Write(I)
i += 1;                                                                        I = I + 1
}
                                                                              Loop

字符串操作

String s1;                                                                    Dim s1, s2 As String
String s2 = "hello";                                                     s2 = "hello"
s2 += " world";                                                          s2 &= " world"
s1 = s2 + " !!!";
 
                                                      s1 = s2 & " !!!"

事件处理

void MyButton_Click(Object sender, EventArgs E) { Sub MyButton_Click(Sender As Object, E As EventArgs)
…                                                                                    
}
 
                                                                                 End Sub
                                                            注意 ByVal VB中是省缺参数 

对象操作

MyObject obj = (MyObject)Session["Some Value"]; Dim bj As MyObject
IMyObject iObj = obj
 
                                            Dim iObj As IMyObject
                                obj = Session("Some Value")
                  
              iObj = CType(obj, IMyObject)

类型转换

int i = 3;
String s = i.ToString();                                                Dim i As Integer
double d = Double.Parse(s);
 
                                 Dim s As String
                              Dim d As Double
                  
            i = 3
                  
            s = i.ToString()
                  
            d = CDbl(s)
                              参见 CDbl(…), CStr(…), … 

类定义和继承

using System;                                                             Imports System
namespace MySpace {                                               Namespace MySpace
public class Foo : Bar {                                              Public Class Foo : Inherits Bar
int x;                                                                           Dim x As Integer
public Foo() {x = 4; }                                                Public Sub New()
public void Add(int x) { this.x += x; }                      MyBase.New()
public int GetNum() { return x; }                              x = 4
}                                                                                 End Sub
}                                                                                 Public Sub Add(x As Integer)
// csc /out:librarycs.dll /t:library library.cs
 
              Me.x = Me.x + x
                              End Sub
                  
            Public Function GetNum() As Integer
                  
            Return x
                  
            End Function
                                                                                    End Class
                  
            End Namespace
                              ‘ vbc /out:libraryvb.dll /t:library library.vb

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: