熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET入門—語法介紹

2013-11-13 10:48:10  來源: .NET編程 
    聲明變量

Dim x As Integer
Dim s As String
Dim s s As String
Dim o Implicitly Object
Dim obj As New Object()
Public name As String

var x : int;
var s : String;
var s : String s : String;
var o;
var obj : Object = new Object();
var name : String;


    語句輸出

ResponseWrite(foo);


    代碼注釋

This is a comment

)// This is a comment

)/*
  This
  is
  a
  multiline
  comment
 */


    聲明簡單屬性

Public Property Name As String

  Get
   
    Return
  End Get

  Set
    = Value
  End Set

End Property

function get name() : String {
   
    return ;
}

function set name(value : String) {
    = value;
}


    聲明索引屬性

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


    訪問索引屬性

Dim s value As String
s = RequestQueryString(Name)
value = RequestCookies(Key)Value

Note that default nonindexed properties
must be explicitly named in VB

var s : String = RequestQueryString(Name);
var value : String = RequestCookies(key);


    聲明和使用枚舉

Declare the Enumeration
Public Enum MessageSize

    Small =
    Medium =
    Large =
End Enum

Create a Field or Property
Public MsgSize As MessageSize

Assign to the property using the Enumeration values
MsgSize = small

// Declare the Enumeration
public enum MessageSize {

    Small =
    Medium =
    Large =
}

// Create a Field or Property
public var msgsize:MessageSize;

// Assign to the property using the Enumeration values
msgsize = Small;


    聲明和使用方法

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 s As String = StringFunction()
Dim s As String = ParmFunction(Hello World!)

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

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

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

// Use the Functions
voidfunction();
var s:String = stringfunction();
var s:String = parmfunction(Hello World!);


    數組

 Dim a() As String
    a() =
    a() =
    a() =

    Dim a() As String
    a() =
    a() =
    a() =


    var a : String[] = new String[];
    a[] = ;
    a[] = ;
    a[] = ;

    var a : String[][] = new String[][];
    a[][] = ;
    a[][] = ;
    a[][] = ;


    初始化


Dim s As String = Hello World
Dim i As Integer =
Dim a() As Double = { }


var s : String = Hello World;
var i : int = ;
var a : double[] = [ ];


    If 語句

if (RequestQueryString != null) {
 
}

If Not (RequestQueryString = Nothing)
 
End If

if (RequestQueryString != null) {
 
}
 

    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

switch (FirstName) {
  case John :
   
    break;
  case Paul :
   
    break;
  case Ringo :
   
    break;
  default:
   
    break;
}

 
    For 循環

for (int i=; i<; i++)
  a(i) = test;

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

for (var i : int = ; i < ; i++)
  a[i] = test;


    While 循環

int i = ;
while (i<) {
  ConsoleWriteLine(iToString());
  i += ;
}

Dim I As Integer
I =
Do While I <
  ConsoleWriteLine(IToString())
  I +=
Loop

var i : int = ;
while (i < ) {
  ConsoleWriteLine(i);
  i += ;
}


    異常處理

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

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


    字符串連接

// Using Strings
String s;
String s = hello;
s += world;
s = s + !!!;

// Using StringBuilder class for performance
StringBuilder s = new StringBuilder();
sAppend(hello);
sAppend( world);
sAppend( !!!);

Using Strings
Dim s s As String
s = hello
s &= world
s = s & !!!

Using StringBuilder class for performance
Dim s As New StringBuilder()
sAppend(hello)
sAppend( world)
sAppend( !!!)

// Using Strings
var s : String;
var s : String = hello;
s += world;
s = s + !!!;

// Using StringBuilder class for performance
var s:StringBuilder = new StringBuilder();
sAppend(hello);
sAppend( world);
sAppend( !!!);

 
    事件處理程序委托

void MyButton_Click(Object sender
                    EventArgs E) {

}

Sub MyButton_Click(Sender As Object
                   E As EventArgs)

End Sub

function MyButton_Click(sender : Object
                        E : EventArgs) {

}


    聲明事件

// 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

JScript does not support the creation of events  JScript can only
consume events by declaring event handler delegates and adding those
delegates to the events of another control


    向事件添加事件處理程序或從事件移除事件處理程序

ControlChange += new EventHandler(thisChangeEventHandler);
ControlChange = new EventHandler(thisChangeEventHandler);

AddHandler ControlChange AddressOf MeChangeEventHandler
RemoveHandler ControlChange AddressOf MeChangeEventHandler

ControlChange += thisChangeEventHandler;
ControlChange = thisChangeEventHandler;

 
    強制類型轉換

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)

var obj : MyObject = MyObject(Session(Some Value));
var iObj : IMyObject = obj;


    轉換

int i = ;
String s = iToString();
double d = DoubleParse(s);

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

i =
s = iToString()
d = CDbl(s)

See also CDbl() CStr()

var i : int = ;
var s : String = iToString();
var d : double = Number(s);


    帶繼承的類定義

using System;

namespace MySpace {

  public class Foo : Bar {

    int x;

    public Foo() { x = ; }
    public void Add(int x) { thisx += x; }
    override public int GetNum() { return x; }
  }

}

// csc /out:librarycsdll /t:library
// librarycs


Imports System

Namespace MySpace

  Public Class Foo : Inherits Bar

    Dim x As Integer

    Public Sub New()
      MyBaseNew()
      x =
    End Sub

    Public Sub Add(x As Integer)
      Mex = Mex + x
    End Sub

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

  End Class

End Namespace

vbc /out:libraryvbdll /t:library
libraryvb


import System;

package MySpace {

  class Foo extends Bar {

    private var x : int;

    function Foo() { x = ; }
    function Add(x : int) { thisx += x; }
    override function GetNum() : int { return x; }
  }

}

// jsc /out:libraryjsdll libraryjs


    實現接口

public class MyClass : IEnumerable {
 

    IEnumerator IEnumerableGetEnumerator() {
        
    }
}

Public Class MyClass : Implements IEnumerable
 

    Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerableGetEnumerator
        
    End Function
End Class


public class MyClass implements IEnumerable {
 

    function IEnumerableGetEnumerator() : IEnumerator {
        
    }
}

 
    帶 Main 方法的類定義

using System;

public class ConsoleCS {

  public ConsoleCS() {
    ConsoleWriteLine(Object Created);
  }

  public static void Main (String[] args) {
    ConsoleWriteLine(Hello World);
    ConsoleCS ccs = new ConsoleCS();
  }

}

// csc /out:consolecsexe /t:exe consolecs

Imports System

Public Class ConsoleVB

  Public Sub New()
    MyBaseNew()
    ConsoleWriteLine(Object Created)
  End Sub

  Public Shared Sub Main()
    ConsoleWriteLine(Hello World)
    Dim cvb As New ConsoleVB
  End Sub

End Class

vbc /out:consolevbexe /t:exe consolevb

class ConsoleCS {

  function ConsoleCS() {
    print(Object Created);
  }

  static function Main (args : String[]) {
    print(Hello World);
    var ccs : ConsoleCS = new ConsoleCS();
  }
}

// jsc /out:consolejsexe /exe consolejs


    標准模塊

using System;

public class Module {

public static void Main (String[] args) {
  ConsoleWriteLine(Hello World);
}

}
// csc /out:consolecsexe /t:exe consolecs

Imports System

Public Module ConsoleVB

  Public Sub Main()
    ConsoleWriteLine(Hello World)
  End Sub

End Module

vbc /out:consolevbexe /t:exe consolevb

print(Hello World);

// jsc /out:consolejsexe /exe consolejs


From:http://tw.wingwit.com/Article/program/net/201311/14535.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.