vb与C 的语法区别
C#: vb与C#的语法区别 public String name {
1.get { 变量声名
C# : ... int x; return ...; String s; } String s1, s2;
set { Object obj;
... = value; Object obj = new Object();
} public String name;
VB语法
} Dim x As Integer
Dim s As String
VB: Dim s1, s2 As String
Public Property Name As String Dim obj 'Implicitly Object
Dim obj As New Object() Get Public name As String ...
Return ...;
End Get 2语句
C#: Set Response.Write("123456"); ... = Value; VB: End Set Response.Write("123456")
End Property 3.注释语句
//这是c#注释 6.数组 /* C# C#的多行注 String[] a = new String[10]; 释 a[0] = "1"; */ a[1] = "2";
a[2] = "3"; VB: ... '这是VB的注释 //二维数组
String[][] a = new String[3][3]; 4.获得URL 传递的变量
a[0][0] = "1"; C#:
a[1][0] = "2"; String s = Request.QueryString["Name"];
a[2][0] = "3"; String value = Request.Cookies["key"];
VB: VB:
Dim a(10) As String Dim s, value As String
a(0) = "1" s = Request.QueryString("Name")
a(1) = "2" value = Request.Cookies("Key").Value
a(2) = "3"
... 5.声明属性
Dim a(3,3) As String break; a(0,0) = "1" }
a(1,0) = "2" VB:
a(2,0) = "3" Select (a)
case 0 : Dim a() As String ...
a(0,0) = "1" case1 : a(1,0) = "2" ...
a(2,0) = "3" case 2 :
...
Dim a(,) As String End Select a(0,0) = "1"
a(1,0) = "2" 10 .For循环语句 a(2,0) = "3" C#
for (int i=0; i<10; i++)
a(i) = i; 7.变量初始化 VB:
C#: Dim I As Integer String s = "Hello World"; For I = 0 To 10 int i = 10; a(i) = i double[] a = { 3.14, 4.53, 5.10 }; Next
VB:
Dim s As String = "Hello World" 11. While 循环 Dim i As Integer = 10 C#:
Dim a() As Double = { 3.14, 4.53, 5.10 } int i = 0;
while (i<10) { 8.判断语句(If 语句) Console.WriteLine(i.ToString());
if (i!=10) { i ++; ... }
} VB:
Dim I As Integer VB: I = 0 If Not (i=10) Do While I < 10 ... Console.WriteLine(I.ToString())
End If I = ++
Loop 9.分支语句(case 语句) 12 .字符串连接 C#: C#: switch (a) { String s1; case 0 : String s2 = "hello"; ... s2 += " world"; break; s1 = s2 + " !!!"; case 1 : VB: ... Dim s1, s2 As String break; s2 = "hello" case 2 :
...
s2 &= " world" namespace MySpace { s1 = s2 & " !!!" public class A : B {
int x;
public F() { x = 10; } 声明事件 public void Add(int x) { this.x += x; }
C#: public int GetNum() { return x; }
void MyButton_Click(Object sender, }
EventArgs E) { }
...
} VB:
VB: Imports System Sub MyButton_Click(Sender As ObjecNamest, E pace MySpace As EventArgs) Public Class A : Inherits B
Dim x As Integer... End Sub Public Sub New()
MyBase.New()
x = 10
13 .声明Object End Sub
C# :
MyObject obj = Public Sub Add(x As Integer) (MyObject)Session["XiaoYear"]; Me.x = Me.x + x IMyObject iObj = obj End Sub
VB:
Public Function GetNum() As Integer Dim bj As MyObject
Return x Dim iObj As IMyObject
End Function obj = Session("XiaoYear")
iObj = CType(obj, IMyObject)
End Class
End Namespace 14. 数据类型转换
C#
16. 声明类的主函数 int i = 3;
C#: String s = i.ToString();
using System; double d = Double.Parse(s);
public class ConsoleCS { VB:
public ConsoleCS() { Dim i As Integer
Console.WriteLine("Object Created"); Dim s As String
} Dim d As Double
public static void Main (String[] args) i = 3
{ s = i.ToString()
Console.WriteLine("Hello World"); d = CDbl(s)
ConsoleCS ccs = new ConsoleCS();
}
15 .类的声明和继承
} C#:
using System;
VB "
Imports System
Public Class ConsoleVB
Public Sub New()
MyBase.New()
Console.WriteLine("Object Created") End Sub
Public Shared Sub Main() Console.WriteLine("Hello World") Dim cvb As ConsoleVB
cvb = New ConsoleVB()
End Sub
End Class