Pascal、VB、C#、Java四种语法对照表
因为工作原因,自学会了vb后陆续接触了其它语言,在工作中经常需要与各家使用不同语言公司的开发人员做程序对接,初期特别需要一个各种语法的对照比,翻看了网络上已有高人做了整理,自己在他基础上也整理了一下,摘抄如下(最后附上原作,网上也可以找到):
类别 |
Vb6 & Vb.Net |
Delphi |
C# |
语法 |
不区分大小写 |
不区分大小写 |
区分大小写 |
数据类型 |
数据 关键字 占用字节 类型符 整型 integer 长整型 long 单精度型 single 双精度型 double 货币型 currency 字节型 byte 字节型 string 布尔型 boolean 日期型 date 8对象型 object 变体型 variant |
|
|
定义变量/初始化变量 |
Dim intA as Integer Dim strA as String Dim lngA as Long
Dim strA As String = "Hello World" Dim intS As Integer = 1 Dim douA() As Double = { 3.00, 4.00, 5.00 } |
Var intA:Integer; strA:String; lngA:Long;
var strA: String; intA: Integer; douA: array of double = {3.00, 4.00, 5.00}
|
Int intA String strA Long lngA
String strA = "Hello World"; int intA = 1 double[] douA = { 3.00, 4.00, 5.00 }; |
运算符 |
+ & Chr13 chr10 |
|
|
数组应用 |
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"
Dim a(2) As Integer a(0) = 0 a(1) = 1 For i = 0 To 2 Step 1 Print a(i) Next i
|
var a[0..2]: string a[0..2,0..2]: String begin a[0] := ‘1’; a[1] := ‘2’; a[2] := ‘3’; a[0,0] := ‘1’; a[1,0] := ‘2’; a[2,0] := ‘3’; End; |
String[] a = new String[3]; a[0] = "1"; a[1] = "2"; a[2] = "3”; String[][] a = new String[3][3]; a[0][0] = "1"; a[1][0] = "2"; a[2][0] = "3"; |
定义数据集 |
Public Property Name As String
Get ... Return ...; End Get
Set ... = Value; End Set
End Property |
Public Record Name: String;
Function GetName: String; Begin ...; Result := …; End;
Procedure SetName(Value: String); Begin … := Value; End;
End |
public String name { get{ ... return ...; }
set{ ... = value; }
} |
对象的操作 |
Dim bj As MyObject = Session("Some Value") Dim iObj As IMyObject = CType(obj, IMyObject) |
var bj: MyObject; iObj: IMyObject; begin obj := Session(‘Some Value’); iObj = Obj as IMyObject; end; |
MyObject obj = (MyObject)Session["Some Value"]; IMyObject iObj = obj; |
字符串操作 |
Dim s1, s2 As String s2 = "hello" s2 &= " world" s1 = s2 & " !!!" <!--[if !supportlinebreaknewline]--> <!--[endif]--> |
var s1, s2: String; begin s2 := ‘hello’; s2 := s2 + ‘world’; s1 := s2 + ‘!!!’; End; |
String s1; String s2 = "hello"; s2 += " world"; s1 = s2 + " !!!"; |
类型转换& Format |
Dim i As Integer = 3 Dim s As String = i.ToString() Dim d As Double = Double.Parse(s): |
var i: Integer; s: String; d: Double; begin i := 3; s = i.ToString(); d := Double.Parse(s); end; |
int i = 3; String s = i.ToString(); double d = Double.Parse(s); |
代码注释 |
' 2013-06-07 商超 添加申请单函数 '================================ '取报告内容,得到电子申请单 '================================
折叠代码 #Region “读取数据列表”
#End Region |
// 2013-06-07 商超 添加申请单函数 /* 取得内容 得到电子申请单*/ |
// 2013-06-07 商超 添加申请单函数 /* 取得内容 得到电子申请单*/ |
输入输出或提示内容 |
Dim n as long N= Val(InputBox("请输入一个数:")) Dim n n = MsgBox("是:添加项目 否:删除项目", 52, "提示信息") If n = vbYes Then Dim Add As String Add = InputBox("请输入添加项目并点击确定后继续", "添加项目") If Add = "" Then Exit Sub End If
Msgbox (“您好!”) Debug.print (“您好!”) Console.writeline(“您好!”) |
showMessage(“您好!”); MessageBox(Handle, PChar('不能备份到该目录 ' + path), PChar(MDialogMsg), MB_ICONINFORMATION or MB_OK)
|
|
If语句 |
‘判断是否为空或者指定值 ‘判断按键 Dim strUser as string If strUser=”管理员” then Msgbox “您是”+strUser End if If Not (Request.QueryString = Null) ... End If <!--[if !supportlinebreaknewline]--> <!--[endif]-->
if a > 3 then msgbox "a" end if
if a = "" then msgbox "a" else msgbox "b" end if
if a ="" then msgbox "a" elseif msgbox "b" else msgbox "c" end if |
strUser:string; If (strUser=’管理员’) then Showmessage(‘您是’+strUser);
If Not (Request.QueryString = Null) then begin ... End;
//测试If Then语句//
procedure TForm1.Button6Click(Sender: TObject); var strSql: string;
begin strSql := 'Select'; if strSql = 'Select' then showmessage(PChar(strSql)) else showmessage('Sql语句不对!') end;
//输入值后按回车//
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if (Key = VK_RETURN) then begin Button7Click(Sender); //直接调用Button1的事件处理程序完成相同的功能 end; end; begin if key = #8 then exit; if not ((key >= '0') and (key <= '9')) then key := #0; end; |
if (Request.QueryString != null) { ... } |
If Else 语句 |
|
|
|
Case语句 |
Select (FirstName) case "John" : ... <!--[if !supportlinebreaknewline]--> <!--[endif]--> case "Paul" : ... case "Ringo" : ... End Select
select case index
case 0 msgbox "a" case 1 msgbox "b" end select
|
Case FirstName of ‘John’: ... ‘Paul’: … ‘Ringo’: … End
//测试Case语句//
procedure TForm1.Button5Click(Sender: TObject); var i: ShortInt; begin i := random(3); case i of //0:showmessage('测试零');; 符号错误 0: showmessage('测试一'); 1: showmessage('测试二'); 2: showmessage('测试三'); else showmessage('测试完成'); end end; |
switch (FirstName){ case "John" : ... break; case "Paul" : ... break; case "Ringo" : ... break; } |
For循环 |
Dim I As Integer For I = 0 To 2 a(I) = "test" Next
for i = 0 to list.count - 1 step 0 msgbox list(i) Next
|
Var I: Integer; Begin For I := 0 to 2 do A[i] := ‘test’; End;
//测试For循环//
procedure TForm1.Button2Click(Sender: TObject); var I: Integer; Tot: Integer; begin I := 0; Tot := 0; for i := 0 to 10 do begin Tot := Tot + i end; showMessage(IntToStr(Tot)); end;
procedure TForm1.btn1Click(Sender: TObject); var i:Integer; begin for i:=0 to lst1.Count-1 do begin ShowMessage(lst1.Items[i]); end; lst1.Items.Clear; end;
|
for (int i=0; i<3; i++) a(i) = "test"; |
While循环 |
Dim I As Integer I = 0 Do While I < 3 Console.WriteLine(i.ToString()); I = I + 1 Loop |
Var I: Integer; Begin I := 0; While i< 3 do Begin Console.WriteLen(i.ToString()); I := I + 1; End; End;
//测试While语句//
procedure TForm1.Button3Click(Sender: TObject); var i: ShortInt; begin i := 0; while i < 10 do begin i := i + 1; showmessage(IntToStr(i)); end; end;
|
int i = 0; while (i<3) { Console.WriteLine(i.ToString()); i += 1; } |
With语句 |
With list .list(0)=”是我的” .Add End with
rs.Open "man", cn, adOpenKeyset, adLockOptimistic With rs .AddNew .Fields("标题") = Trim(Text1.Text) .Fields("类别") = Trim(Combo1(2).Text) .Fields("类型") = Trim(Combo1(0).Text) If Check2.Value = 1 Then .Fields("内容") = Trim(RichTextBox1.Text) .Fields("内容2") = Trim(RichTextBox1.TextRTF) Else .Fields("内容") = Trim(RichTextBox1.Text) .Fields("内容2") = Null End If .Fields("日期") = Format(DTPicker1.Value, "short date") 'CStr(Format(DTPicker1.Value, "YYYY-MM-DD ")) .Update End With |
with TOpenPictureDialog.Create(nil) do begin Filter:='*.bmp|*.bmp'; if Execute then image1.Picture.LoadFromFile(filename); Free; end; |
|
防错语句 |
on error goto Err:
Try
Castch
End try
|
try edtIP.text:=iniinfo.readstring('Size','dbIP',''); Finally freeAndNil(IniInfo); end;
//测试Repeat语句//
procedure TForm1.Button4Click(Sender: TObject); var i: ShortInt; begin i := 0; repeat i := i + 1; showmessage(IntToStr(i)) until i = 10; end; |
|
退出和关闭 |
Exit sub Eixt Function Close End
|
exit; Self.Close; |
|
过程及函数调用 |
'调用面积计算过程
Private Sub Add_Click() Call subcompute(txtLength, txtWidth) End Sub
'定义一个计算面积的函数,有两个参数,返回计算结果 Private Function subcomputearea(Length As Long, Width As Long) 'length,width都是形式参数 subcomputearea = Length * Width End Function
'调用计算面积函数 Private Sub Add_Click() txtResult.Text = F(Val( txtResults.Text)) End Sub
'面积计算函数 Function F(n As Integer) As Single If n > 1 And n <= 30 Then F = n * F(n - 1) Else F = 1 End If End Function
递归过程 Private Sub Command8_Click() Text2.Text = F(Val(Text1.Text)) End Sub
Function F(n As Integer) As Single If n > 1 And n <= 30 Then F = n * F(n - 1) Else F = 1 End If End Function |
interface //单元的接口部分可在接口部分声明变量、常量、数据类型、过程、函数,结束于implementation的开始部分//
{ //一个简单的函数// function GetAverage(num:integer;total:Double):Double; begin GetAverage := total / num; //Result := total / num; end; //一个简单的过程// procedure GetResult() //procedure SetDate(Year: Integer; Month: Integer; Day: Integer); or procedure SetDate(Year, Month, Day: Integer); begin GetResult:=3*3; end; |
|
常用内部函数(字符串函数) |
-字符串函数 Mid Right Left Trim Rtrim Ltrim len Instr Squit -转换函数 Char Val asc char -日期时间函数 Date Now Timer Year/month/day Hour/minute/second -判断函数 Isnull isnumberic -随机函数 Rnd -格式化函数
|
Copy: S := ’’I Love China!’’; //下面将获取I Love China中的“Love”字符串。 MyStr := Copy(S, 3, 4); Concat: S1 := Concat(’’A’’, ’’B’’); // 连接两个字符串,S1变量等于AB。 S := ’’I Like Reading CPCW.’’; // 下面的代码将删除S变量中的“C”字符。 Delete: Delete(S, 16, 1); end; 此时S变量则是I Like Reading PCW.(“C”已经不存在了)。 LeftStr Length MidStr Pos: nPos: Integer; // 用于保存查找的字符所在位置 begin nPos := Pos(’’Like’’, ’’I Like Reading!’’); RightStr: A := RightStr(S, 3); // 从最右边开始,获取右边的三个字符。因此A变量则等于ger。 Trim TrimLeft TrimRight UpperCase LowerCase Low High Insert |
|
类的定义和继承 |
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
|
unit MySpace interface uses System;
type Foo = Class(Bar) private x: integer; public procedure Create; override; //构造函数 procedure Add(x: Integer); function GetNum: Integer; end;
implementation procedure Foo.Create; begin inherited; x := 4; end;
procedure Foo.Add(x: Integer); begin Self.x := Self.x + x; end;
function Foo.GetNum: Integer; begin Result := x; end;
end; |
using System;
namespace MySpace {
public class Foo : Bar { <!--[if !supportlinebreaknewline]--> <!--[endif]--> int x;
public Foo() {x = 4; } //构造函数 <!--[if !supportlinebreaknewline]--> <!--[endif]--> public void Add(int x) { this.x += x; } <!--[if !supportlinebreaknewline]--> <!--[endif]--> public int GetNum() { return x; } <!--[if !supportlinebreaknewline]--> <!--[endif]--> }
} // csc /out:librarycs.dll /t:library library.cs |
事件处理 |
Sub Button1_Click(Sender As Object, E As EventArgs) ... End Sub ‘ByVal 在VB中对象是是缺省参数传递方式
|
procedure Button1OnClick(Sender: Object;E: EventArgs); begin ... end; ‘ByRef 在Pascal中对象是缺省参数传递方式
btnSearchClick(Sender); //直接调用Button1的事件处理程序完成相同的功能
|
void Button1_Click(Object sender, EventArgs E) { ... } ‘ByRef 在C#中对象是是缺省参数传递方式
|
操作数据库的增删改查应用 (Access Sql Mysql Oracle ) |
|
try
conADOConn.Connectionstring := 'Provider=SQLOLEDB.1;Password=itpschina;Persist Security Info=True;User ID=sa;Initial Catalog=Study;Data Source=.'; conADOConn.Open; //Showmessage('连接数据库成功!'); qryAdoQ.SQL.Clear; qryAdoQ.SQL.Add('select * from Man'); qryAdoQ.Open;
except Showmessage('无法连接数据库!'); exit; end; |
|
日志记录函数 |
|
//写入日志文件//
procedure WriteLog(ErrStr:String); var LogFilename: String; LogFile: TextFile; begin LogFilename:=ExtractFilePath(ParamStr(0)) + 'LOG_' + FormatDateTime('yyyymmdd',Now) + '.LOG'; AssignFile(LogFile, LogFilename); if FileExists(LogFilename) then Append(LogFile) else Rewrite(LogFile); Writeln(Logfile,DateTimeToStr(now)+': '+ErrStr); CloseFile(LogFile); end; |
|
Ini的读写 Txt的读写 |
|
var iniinfo:TIniFile; getdir(0,dir); //获取程序所在路径地址 iniInfo:=TInIFile.Create(dir+'\Size.INI'); edtIP.text:=iniinfo.readstring('Size','dbIP','');
iniInfo.WriteString('Size','dbIp',edtIp.Text);
|
|
加密解密字符串 |
|
//字符串加密与解密函数//
//解密字符串//
function E_code(S:string):String; var n,i:integer; str:string; begin n:=length(s); str:=''; for i:=1 to n do begin str:=str+char(Ord(s[i])+2); end; E_code:=str; end;
//加密字符串//
function D_code(s:string):String; var n,i:integer; str:string; begin n:=length(s); str:=''; for i:=1 to n do begin str:=str+char(ord(S[i])-2); end; D_code:=str; end; |
|
Xml文件的读写 |
|
|
|
本地文件读写、打开文件选择框 |
'///导出文件///
Private Sub Command8_Click() Dim A As String A = Text1.Text Dim n n = MsgBox("是否导出?", 52, "提示信息") If n = vbYes Then Open "D:\\" & A & ".rtf" For Output As #1 '新建文件 Print #1, RichTextBox1.Text Close #1 MsgBox "文件已经存储在D:\,文件名为" & A & "", 64, "提示信息" ElseIf n = vbNo Then MsgBox "操作取消", 64, "提示信息" Exit Sub End If
End Sub
'///导入文件///
Private Sub Command9_Click() Open "D:\给疯狂下资料的朋友们提个醒!.txt" For Input As #1 Dim A Do While EOF(1) = False Line Input #1, A RichTextBox1 = RichTextBox1 & A & vbCrLf '这样就好了,要把数据连接起来,否则text只等于最后一行 Loop Close End Sub
'///导入文件///
Private Sub mnuOpen_Click() CommonDialog1.FileName = "" CommonDialog1.Filter = "*.txt|*.txt|*.rtf|*.rtf|*.*|*.*" CommonDialog1.DialogTitle = "打开文件" CommonDialog1.CancelError = True '捕获 取消 错误时得先设置为true 然后再showsave On Error GoTo ErrorHandler CommonDialog1.ShowOpen Text1.Text = Left(CommonDialog1.FileTitle, Len(CommonDialog1.FileTitle) - 4) RichTextBox1.LoadFile CommonDialog1.FileName, 1 ErrorHandler: Exit Sub End Sub
'///选择数据库///
Private Sub SelData_Click() CommonDialog1.FileName = "" CommonDialog1.InitDir = App.Path '设置缺省路径 CommonDialog1.Filter = "*.mdb|*.mdb|" CommonDialog1.DialogTitle = "选择数据库" CommonDialog1.CancelError = True '捕获 取消 错误时得先设置为true 然后再showsave On Error GoTo ErrorHandler CommonDialog1.ShowOpen
entry$ = Trim(CommonDialog1.FileName) dataname = Trim(CommonDialog1.FileName) R = WritePrivateProfileString("数据库", "库名", entry, iniPath) If R <> 1 Then MsgBox "写入出错!" If selectdate = True Then Combo1(0).Clear Call Form_Load Else Call Main End If
ErrorHandler: Exit Sub
End Sub |
with TOpenPictureDialog.Create(nil) do begin Filter:='*.bmp|*.bmp'; if Execute then image1.Picture.LoadFromFile(filename); Free; end; |
|
文件路径操作 |
Dir App.path
|
|
|
无边框窗体移动 |
|
|
|
颜色值函数 |
|
|
|
屏幕位置分辨率 |
|
|
|
Pascal、VB、C#、Java四种语法对照表
ASP.NET支持的几种语言:c#.net,vb.net,delphi.net(非内置支持),由于在.net框架下,所有的语言均使用同一个类库,因此功能上是基本上一模一样,只语法上各不相同。
.net与java为两大阵营,为了比较,在此列出一个简单的语法对照表,看看你到底喜欢那个?
类别 |
delphi.net语法 |
vb.net 语法(语法不区分大小写) |
c#.net 语法 |
java |
语法 |
不区分大小写 |
不区分大小写 |
区分大小写 |
区分大小写 |
定义变量 |
Var s: string; s1, s2: string; o: TObject; obj: TObject; obj := TObject.Create; Public property name: String; |
Dim x As Integer Dim s As String Dim s1, s2 As String Dim o 'Implicitly Object Dim obj As New Object() Public name As String |
int x; String s; String s1, s2; Object o; Object obj = new Object(); public String name; |
int x; String s; String s1, s2; Object o; Object obj = new Object(); public String name; |
输出内容 |
Response.write(‘foo’); |
Response.Write("foo") Debug.print ("foo")
|
Response.Write("foo"); |
Response.Write("foo"); |
注释 |
// This is a comment /* This is a |
' This is a comment ' multi-line |
// This is a comment /* This is a |
// This is a comment /* This is a |
读取数据集合数组 |
var s, value: String begin s := Request.QueryString[‘Name’]; Value := Request.Cookies(‘Key’).Value; end; |
Dim s as String = Request.QueryString("Name") Dim value As String = Request.Cookies("Key") |
String s=Request.QueryString["Name"]; String value=Request.Cookies["key"]; |
String s = request. getParameter("Name"); String value; Cookie ck = null; Cookie args[] = Request.getCookies(); for(int i = 0; i<args.length; i++){ ck = args[i]; if(ck.getName().equals("key ")) value = ck.getValue(); } |
字符串操作 |
var s1, s2: String; begin s2 := ‘hello’; s2 := s2 + ‘world’; s1 := s2 + ‘!!!’; End; |
Dim s1, s2 As String |
String s1; |
String s1; |
原作如下:Pascal、VB、C#、Java四种语法对照表
类别 |
delphi.net语法 |
vb.net 语法(语法不区分大小写) |
c#.net 语法 |
java |
初始化变量 |
var s: String; I: Integer; A: array of double = {3.00, 4.00, 5.00} |
Dim s As String = "Hello World" |
String s = "Hello World"; |
String s = "Hello World"; |
定义简单数据集 |
Public Record Name: String;
Function GetName: String; Begin ...; Result := …; End;
Procedure SetName(Value: String); Begin … := Value; End;
End |
Public Property Name As String
Get
... = Value;
|
public String name {
|
private String name;
public String getName(){ return name; }
public void setName(String name){ this.name = name; } |
数组 |
var a[0..2]: string a[0..2,0..2]: String begin a[0] := ‘1’; a[1] := ‘2’; a[2] := ‘3’; a[0,0] := ‘1’; a[1,0] := ‘2’; a[2,0] := ‘3’; End; |
Dim a(3) As String |
String[] a = new String[3]; |
String[] a = new String[3]; a[0] = "1"; a[1] = "2"; a[2] = "3"; String[][] a = new String[3][3]; a[0][0] = "1"; a[1][0] = "2"; a[2][0] = "3"; |
对象操作 |
var bj: MyObject; iObj: IMyObject; begin obj := Session(‘Some Value’); iObj = Obj as IMyObject; end; |
Dim bj As MyObject = Session("Some Value") Dim iObj As IMyObject = CType(obj, IMyObject) |
MyObject obj = (MyObject)Session["Some Value"]; |
MyObject obj = (MyObject)Session.getItem ("Some Value"); |
类型转换 |
var i: Integer; s: String; d: Double; begin i := 3; s = i.ToString(); d := Double.Parse(s); end; |
Dim i As Integer = 3 Dim s As String = i.ToString() Dim d As Double = Double.Parse(s): |
int i = 3; |
int i = 3; double d = Double.valueof(s); |
类别 |
delphi.net语法 |
vb.net 语法(语法不区分大小写) |
c#.net 语法 |
java |
If 结构 |
If Not (Request.QueryString = Null) then begin ... End; |
If Not (Request.QueryString = Null) |
if (Request.QueryString != null) { |
if (Request.QueryString != null) { |
Case 结构 |
Case FirstName of ‘John’: ... ‘Paul’: … ‘Ringo’: … End |
Select (FirstName) ... case "Paul" : |
switch (FirstName){ |
int flag = 1; switch (flag){ case 1: ... break; case 2: ... break; } |
For 循环 |
Var I: Integer; Begin For I := 0 to 2 do A[i] := ‘test’; End; |
Dim I As Integer For I = 0 To 2 |
for (int i=0; i<3; i++) a(i) = "test"; |
for (int i=0; i<3; i++) a[i] = "test"; |
While 循环 |
Var I: Integer; Begin I := 0; While i< 3 do Begin Console.WriteLen(i.ToString()); I := I + 1; End; End; |
Dim I As Integer Do While I < 3 Console.WriteLine(i.ToString()); I = I + 1 |
int i = 0; { } |
int i = 0; { } |
类别 |
delphi.net语法 |
vb.net 语法(语法不区分大小写) |
c#.net 语法 |
java |
类定义和继承 |
unit MySpace interface uses System;
type Foo = Class(Bar) private x: integer; public procedure Create; override; //构造函数 procedure Add(x: Integer); function GetNum: Integer; end;
implementation procedure Foo.Create; begin inherited; x := 4; end;
procedure Foo.Add(x: Integer); begin Self.x := Self.x + x; end;
function Foo.GetNum: Integer; begin Result := x; end;
end;
|
Imports System
Namespace MySpace
End Class
|
using System;
{
{ int x; public void Add(int x) { this.x += x; } public int GetNum() { return x; } }
|
import java.lang.*;
package MySpace;
public class Foo extends Bar{
private int x; //私有变量 public void Add(int x) { this.x += x; } //过程 public int getNum() { return x; } //函数 } |
事件处理 |
procedure Button1OnClick(Sender: Object;E: EventArgs); begin ... end; ‘ByRef 在Pascal中对象是缺省参数传递方式 |
Sub Button1_Click(Sender As Object, E As EventArgs) ... ‘ByVal 在VB中对象是是缺省参数传递方式 |
void Button1_Click(Object sender, EventArgs E) { |
jButton1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event Button1_Click(evt); } });
private void Button1_Click(java.awt. event } |
转载自网络,作者:张弓 2007/4/21于台中, 2008/4/18补充加入java