知识大全 ASP.NET入门—语法介绍
Posted 事件
篇首语:寸阳分阴须爱惜,休负春色与时光。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 ASP.NET入门—语法介绍相关的知识,希望对你有一定的参考价值。
ASP.NET入门—语法介绍 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
声明变量Dim x As IntegerDim s As StringDim s s As StringDim o Implicitly ObjectDim 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;
语句输出
Response Write( foo );
代码注释
) This is a ment
)// This is a ment
)/* This is a multiline ment */
声明简单属性
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 PropertyPublic Default ReadOnly Property DefaultProperty(Name As String) As String Get Return CStr(lookuptable(name)) End GetEnd Property
访问索引属性
Dim s value As Strings = Request QueryString( Name )value = Request Cookies( Key ) Value
Note that default non indexed properties must be explicitly named in VB
var s : String = Request QueryString( Name );var value : String = Request Cookies( key );
声明和使用枚举
Declare the EnumerationPublic Enum MessageSize
Small = Medium = Large = End Enum
Create a Field or PropertyPublic MsgSize As MessageSize
Assign to the property using the Enumeration valuesMsgSize = small
// Declare the Enumerationpublic enum MessageSize
Small = Medium = Large =
// Create a Field or Propertypublic var msgsize:MessageSize;
// Assign to the property using the Enumeration valuesmsgsize = Small;
声明和使用方法
Declare a void return functionSub VoidFunction() End Sub
Declare a function that returns a valueFunction StringFunction() As String Return CStr(val)End Function
Declare a function that takes and returns valuesFunction ParmFunction(a As String b As String) As String Return CStr(A & B)End Function
Use the FunctionsVoidFunction()Dim s As String = StringFunction()Dim s As String = ParmFunction( Hello World! )
// Declare a void return functionfunction voidfunction() : void
// Declare a function that returns a valuefunction stringfunction() : String return String(val);
// Declare a function that takes and returns valuesfunction parmfunction(a:String b:String) : String return String(a + b);
// Use the Functionsvoidfunction();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 (Request QueryString != null)
If Not (Request QueryString = Nothing) End If
if (Request QueryString != 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< ) Console WriteLine(i ToString()); i += ;
Dim I As IntegerI = Do While I < Console WriteLine(I ToString()) I += Loop
var i : int = ;while (i < ) Console WriteLine(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 exceptionsCatch E As OverflowException Catch a specific exceptionCatch E As Exception Catch the generic exceptionsFinally Execute some cleanup codeEnd 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 StringsString s ;String s = hello ;s += world ;s = s + !!! ;
// Using StringBuilder class for performanceStringBuilder s = new StringBuilder();s Append( hello );s Append( world );s Append( !!! );
Using StringsDim s s As Strings = hello s &= world s = s & !!!
Using StringBuilder class for performanceDim s As New StringBuilder()s Append( hello )s Append( world )s Append( !!! )
// Using Stringsvar s : String;var s : String = hello ;s += world ;s = s + !!! ;
// Using StringBuilder class for performancevar s :StringBuilder = new StringBuilder();s Append( hello );s Append( world );s Append( !!! );
事件处理程序委托
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 eventpublic event EventHandler MyEvent;
// Create a method for firing the eventprotected void OnMyEvent(EventArgs e) MyEvent(this e);
Create a public eventPublic Event MyEvent(Sender as Object E as EventArgs)
Create a method for firing the eventProtected Sub OnMyEvent(E As EventArgs) RaiseEvent MyEvent(Me E)End Sub
JScript does not support the creation of events JScript can onlyconsume events by declaring event handler delegates and adding thosedelegates to the events of another control
向事件添加事件处理程序或从事件移除事件处理程序
Control Change += new EventHandler(this ChangeEventHandler);Control Change = new EventHandler(this ChangeEventHandler);
AddHandler Control Change AddressOf Me ChangeEventHandlerRemoveHandler Control Change AddressOf Me ChangeEventHandler
Control Change += this ChangeEventHandler;Control Change = this ChangeEventHandler;
强制类型转换
MyObject obj = (MyObject)Session[ Some Value ];IMyObject iObj = obj;
Dim obj As MyObjectDim iObj As IMyObjectobj = Session( Some Value )iObj = CType(obj IMyObject)
var obj : MyObject = MyObject(Session( Some Value ));var iObj : IMyObject = obj;
转换
int i = ;String s = i ToString();double d = Double Parse(s);
Dim i As IntegerDim s As StringDim d As Double
i = s = i ToString()d = CDbl(s)
See also CDbl( ) CStr( )
var i : int = ;var s : String = i ToString();var d : double = Number(s);
带继承的类定义
using System;
namespace MySpace
public class Foo : Bar
int x;
public Foo() x = ; 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 = 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
import System;
package MySpace
class Foo extends Bar
private var x : int;
function Foo() x = ; function Add(x : int) this x += x; override function GetNum() : int return x;
// jsc /out:libraryjs dll library js
实现接口
public class MyClass : IEnumerable
IEnumerator IEnumerable GetEnumerator()
Public Class MyClass : Implements IEnumerable
Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable GetEnumerator End FunctionEnd Class
public class MyClass implements IEnumerable
function IEnumerable GetEnumerator() : IEnumerator
带 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
class ConsoleCS
function ConsoleCS() print( Object Created );
static function Main (args : String[]) print( Hello World ); var ccs : ConsoleCS = new ConsoleCS();
// jsc /out:consolejs exe /exe console js
标准模块
cha138/Article/program/net/201311/14535using 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
print( Hello World );
相关参考
知识大全 ASP.NET入门教程 11.2 Wrox United中角色的介绍[2]
ASP.NET入门教程11.2WroxUnited中角色的介绍[2] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快
知识大全 ASP.NET入门教程 11.2 Wrox United中角色的介绍[5]
ASP.NET入门教程11.2WroxUnited中角色的介绍[5] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快
知识大全 ASP.NET入门教程 11.2 Wrox United中角色的介绍[3]
ASP.NET入门教程11.2WroxUnited中角色的介绍[3] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快
知识大全 ASP.NET入门教程 11.2 Wrox United中角色的介绍[4]
ASP.NET入门教程11.2WroxUnited中角色的介绍[4] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快
知识大全 ASP.NET入门教程 11.2 Wrox United中角色的介绍[1]
ASP.NET入门教程11.2WroxUnited中角色的介绍[1] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快
ASP.NET开发宝典:ASP.NET网页语法基础 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
ASP.NET开发宝典:ASP.NET服务器控件语法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
知识大全 ASP.NET入门教程 7.2 ASP.NET 2.0的数据控件
ASP.NET入门教程7.2ASP.NET2.0的数据控件 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一
知识大全 ASP.NET入门教程 10.5.3 ASP.NET 2.0的新特性[2]
ASP.NET入门教程10.5.3ASP.NET2.0的新特性[2] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快
知识大全 ASP.NET入门教程 10.5.3 ASP.NET 2.0的新特性[1]
ASP.NET入门教程10.5.3ASP.NET2.0的新特性[1] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快