| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2247 人关注过本帖
标题:[原创]360度全方位比较 c# 和 VB~~
只看楼主 加入收藏
HankStar
Rank: 1
等 级:新手上路
帖 子:230
专家分:0
注 册:2006-10-4
收藏
 问题点数:0 回复次数:5 
[原创]360度全方位比较 c# 和 VB~~

1 变量声明

int x;
String s;
String s1, s2;
Object o;
Object obj = new Object();
public String name;


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

2 语句

Response.Write("foo");


Response.Write("foo")


3 注释

// This is a comment

/*
This
is
a
multiline
comment
*/


' This is a comment

' This
' is
' a
' multiline
' comment

4 访问索引属性

String s = Request.QueryString["Name"];
String value = Request.Cookies["key"];


Dim s, value As String
s = Request.QueryString("Name")
value = Request.Cookies("Key").Value
'Note that default non-indexed properties
'must be explicitly named in VB


5 声明索引属性

// Default Indexed Property
public String this[String name] {
get {
return (String) lookuptable[name];
}
}

' Default Indexed Property
Public Default ReadOnly Property DefaultProperty(Name As String) As String
Get
Return CStr(lookuptable(name))
End Get
End Property


6 声明简单属性

public String name {

get {
...
return ...;
}

set {
... = value;
}

}


Public Property Name As String

Get
...
Return ...
End Get

Set
... = Value
End Set

End Property


7 声明和使用枚举

// Declare the Enumeration
public enum MessageSize {

Small = 0,
Medium = 1,
Large = 2
}

// Create a Field or Property
public MessageSize msgsize;
// Assign to the property using the Enumeration values
msgsize = Small;




' Declare the Enumeration
Public Enum MessageSize

Small = 0
Medium = 1
Large = 2
End Enum

' Create a Field or Property
Public MsgSize As MessageSize

' Assign to the property using the Enumeration values
MsgSize = small


8 枚举集合

foreach ( String s in coll ) {
...
}


Dim S As String
For Each S In Coll
...
Next


9 声明和使用方法

// Declare a void return function
void voidfunction() {
...
}

// Declare a function that returns a value
String stringfunction() {
...
return (String) val;
}

// Declare a function that takes and returns values
String parmfunction(String a, String b) {
...
return (String) (a + b);
}

// Use the Functions
voidfunction();
String s1 = stringfunction();
String s2 = parmfunction("Hello", "World!");



' Declare a void return function
Sub VoidFunction()
...
End Sub

' Declare a function that returns a value
Function StringFunction() As String
...
Return CStr(val)
End Function

' Declare a function that takes and returns values
Function ParmFunction(a As String, b As String) As String
...
Return CStr(A & B)
End Function

' Use the Functions
VoidFunction()
Dim s1 As String = StringFunction()
Dim s2 As String = ParmFunction("Hello", "World!")


10 自定义属性

// Stand-alone attribute
[STAThread]

// Attribute with parameters
[DllImport("ADVAPI32.DLL")]

// Attribute with named parameters
[DllImport("KERNEL32.DLL", CharSet=CharSet.Auto)]


' Stand-alone attribute
<STAThread>

' Attribute with parameters
<DllImport("ADVAPI32.DLL")>

' Attribute with named parameters
<DllImport("KERNEL32.DLL", CharSet:=CharSet.Auto)>


11 数组

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";


Dim a(2) As String
a(0) = "1"
a(1) = "2"
a(2) = "3"

Dim a(2,2) As String
a(0,0) = "1"
a(1,0) = "2"
a(2,0) = "3"


12 初始化

String s = "Hello World";
int i = 1;
double[] a = { 3.00, 4.00, 5.00 };


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


13 If 语句
if (Request.QueryString != null) {
...
}


If Not (Request.QueryString = Nothing)
...
End If


14 Case 语句
switch (FirstName) {
case "John" :
...
break;
case "Paul" :
...
break;
case "Ringo" :
...
break;
default:
...
break;
}


Select Case FirstName
Case "John"
...
Case "Paul"
...
Case "Ringo"
...
Case Else
...
End Select


15 For 循环
for (int i=0; i<3; i++)
a(i) = "test";


Dim I As Integer
For I = 0 To 2
a(I) = "test"
Next


16 While 循环
int i = 0;
while (i<3) {
Console.WriteLine(i.ToString());
i += 1;
}



Dim I As Integer
I = 0
Do While I < 3
Console.WriteLine(I.ToString())
I += 1
Loop


17 异常处理
try {
// Code that throws exceptions
} catch(OverflowException e) {
// Catch a specific exception
} catch(Exception e) {
// Catch the generic exceptions
} finally {
// Execute some cleanup code
}


Try
' Code that throws exceptions
Catch E As OverflowException
' Catch a specific exception
Catch E As Exception
' Catch the generic exceptions
Finally
' Execute some cleanup code
End Try


18 字符串连接
// Using Strings
String s1;
String s2 = "hello";
s2 += " world";
s1 = s2 + " !!!";

// Using StringBuilder class for performance
StringBuilder s3 = new StringBuilder();
s3.Append("hello");
s3.Append(" world");
s3.Append(" !!!");


' Using Strings
Dim s1, s2 As String
s2 = "hello"
s2 &= " world"
s1 = s2 & " !!!"

' Using StringBuilder class for performance
Dim s3 As New StringBuilder()
s3.Append("hello")
s3.Append(" world")
s3.Append(" !!!")


19 事件处理程序委托
void MyButton_Click(Object sender,
EventArgs E) {
...
}


Sub MyButton_Click(Sender As Object,
E As EventArgs)
...
End Sub


20 声明事件
// Create a public event
public event EventHandler MyEvent;

// Create a method for firing the event
protected void OnMyEvent(EventArgs e) {
MyEvent(this, e);
}


' Create a public event
Public Event MyEvent(Sender as Object, E as EventArgs)

' Create a method for firing the event
Protected Sub OnMyEvent(E As EventArgs)
RaiseEvent MyEvent(Me, E)
End Sub


21 向事件添加事件处理程序或从事件移除事件处理程序
Control.Change += new EventHandler(this.ChangeEventHandler);
Control.Change -= new EventHandler(this.ChangeEventHandler);

AddHandler Control.Change, AddressOf Me.ChangeEventHandler
RemoveHandler Control.Change, AddressOf Me.ChangeEventHandler


22 强制类型转换
MyObject obj = (MyObject)Session["Some Value"];
IMyObject iObj = obj;

Dim obj As MyObject
Dim iObj As IMyObject
obj = Session("Some Value")
iObj = CType(obj, IMyObject)


23 转换
int i = 3;
String s = i.ToString();
double d = Double.Parse(s);


Dim i As Integer
Dim s As String
Dim d As Double

i = 3
s = i.ToString()
d = CDbl(s)

' See also CDbl(...), CStr(...), ...


24 带继承的类定义
using System;

namespace MySpace {

public class Foo : Bar {

int x;

public Foo() { x = 4; }
public void Add(int x) { this.x += x; }
override 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

Overrides Public Function GetNum() As Integer
Return x
End Function

End Class

End Namespace

' vbc /out:libraryvb.dll /t:library
' library.vb


25 实现接口
public class MyClass : IEnumerable {
...

IEnumerator IEnumerable.GetEnumerator() {
...
}
}


Public Class MyClass : Implements IEnumerable
...

Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
...
End Function
End Class


26 带 Main 方法的类定义

using System;

public class ConsoleCS {

public ConsoleCS() {
Console.WriteLine("Object Created");
}

public static void Main (String[] args) {
Console.WriteLine("Hello World");
ConsoleCS ccs = new ConsoleCS();
}

}

// csc /out:consolecs.exe /t:exe console.cs


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 New ConsoleVB
End Sub

End Class

' vbc /out:consolevb.exe /t:exe console.vb


27 标准模块
using System;

public class Module {

public static void Main (String[] args) {
Console.WriteLine("Hello World");
}

}
// csc /out:consolecs.exe /t:exe console.cs




Imports System

Public Module ConsoleVB

Public Sub Main()
Console.WriteLine("Hello World")
End Sub

End Module

' vbc /out:consolevb.exe /t:exe console.vb



28 .....省略~~


请不要把帖子发到VB区~谢谢~



[此贴子已经被作者于2006-10-19 8:53:18编辑过]

搜索更多相关主题的帖子: String Object Dim 全方位 
2006-10-19 08:48
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
好像也没什么的..也差不多...不过..也表示支持..

我的msn: myfend@
2006-10-19 09:06
CrazyWeed0907
Rank: 2
等 级:新手上路
威 望:5
帖 子:1385
专家分:0
注 册:2006-5-30
收藏
得分:0 

他们没有什么好比的


“十步杀一人,千里不留行。事了拂衣去,深藏身与名。”
2006-10-19 09:17
HankStar
Rank: 1
等 级:新手上路
帖 子:230
专家分:0
注 册:2006-10-4
收藏
得分:0 
那我们设想一下~

如果有一个新手要学ASP。NET~

那他就会去选择编程语言~

当然~这个帖子自然就对他很有帮助~

换个角度~

如果是一个老鸟看到这个帖子~

他也很大程度上会不以为然~~

2006-10-19 09:37
noshow
Rank: 2
等 级:新手上路
威 望:4
帖 子:1127
专家分:0
注 册:2006-4-21
收藏
得分:0 
只要是原创就该支持

此号自封于2006年11月30日
2006-10-19 09:45
漯河
Rank: 4
等 级:贵宾
威 望:12
帖 子:1255
专家分:0
注 册:2006-8-8
收藏
得分:0 
好东西就顶

——life is full of ups and downs!
2006-10-23 17:05
快速回复:[原创]360度全方位比较 c# 和 VB~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042133 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved