[
教育]C 和VB语法对照
---摘自《ChinaAsp网络》(文/讨饭猫)
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
// This is a comment ' is
/* This is a multi-line comment */ ' a
' multi-line
' comment 读取数据集合数组
Dim s, value As String
s = Request.QueryString("Name")
String s=Request.QueryString["Name"]; value = Request.Cookies("Key").Value
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 Property } End Property
数组
String[] a = new String[3]; Dim a(3) As String a[0] = "1"; a(0) = "1"
a[1] = "2"; a(1) = "2"
a[2] = "3"; a(2) = "3"
String[][] a = new String[3][3]; Dim a(3,3) 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"
' Array of unspecified bounds (NA in C#)
Dim a() As String
a(0,0) = "1"
a(1,0) = "2"
a(2,0) = "3"
Dim a(,) 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" : .. ... case "Ringo" break; ... case "Ringo" : End Select ...
break;
}
For 循环 Dim I As Integer For I = 0 To 2 for (int i=0; i<3; i++) a(I) = "test" a(i) = "test";
Next
While 循环
int i = 0; Dim I As Integer
while (i<3) {I = 0 Do While I Console.WriteLine(i.ToString()); i += 1;
}
字符串操作
Dim s1, s2 As String String s1; s2 = "hello" String 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"]; IMyObject iObj = obj
Dim bj As MyObject
Dim iObj As IMyObject
obj = Session("Some Value")
iObj = CType(obj, IMyObject)
Dim i As Integer 类型转换
Dim s As String int i = 3;
Dim d As Double String s = i.ToString();
i = 3 double d = Double.Parse(s); s = i.ToString() d = CDbl(s) ' 参见 CDbl(...), CStr(...), ...
类定义和继承
using System;
namespace MySpace {
public class Foo : Bar {
int x;
public Foo() {x = 4; }
public void Add(int x) { this.x += x; }
public int GetNum() { return x; }
}
}
// csc /out:librarycs.dll /t:library library.cs
Imports System
Namespace MySpace
Public Class Foo : Inherits Bar
Dim x As Integer
Public Sub New()
MyBase.New()
x = 4
End Sub
Public Sub Add(x As Integer)
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