安全中国首页 > 编程中心 > .NET编程
 
安全中国网友投稿专用上传FTP空间:
Ftp服务器:download.anqn.com
Ftp端口:21
用户名:anqn
密 码:anqn.com
 

ASP.NET入门—语法介绍[1]

更新时间:2008-7-20 7:03:31
责任编辑:ShellExp
热 点:

  1.声明变量
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

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


    2.语句输出

Response.Write("foo");


    3.代码注释

1)' This is a comment

2)// This is a comment

3)/*
  This
  is
  a
  multiline
  comment
 */


    4.声明简单属性

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


    5.声明索引属性

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


    6.访问索引属性

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

var s : String = Request.QueryString("Name");
var value : String = Request.Cookies("key");


    7.声明和使用枚举

' 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

// Declare the Enumeration
public enum MessageSize {

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

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

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


    8.声明和使用方法

' 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!")

// 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 s1:String = stringfunction();
var s2:String = parmfunction("Hello", "World!");


    9.数组

 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"


    var a : String[] = new String[3];
    a[0] = "1";
    a[1] = "2";
    a[2] = "3";

    var a : String[][] = new String[3][3];
    a[0][0] = "1";
    a[1][0] = "2";
    a[2][0] = "3";


    10.初始化


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


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


    11.If 语句

if (Request.QueryString != null) {
  ...
}

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

if (Request.QueryString != null) {
  ...
}
 

    12.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;
}


    13.For 循环

for (int i=0; i<3; i++)
  a(i) = "test";

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

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


    14.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

var i : int = 0;
while (i < 3) {
  Console.WriteLine(i);
  i += 1;
}


    15.异常处理

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
}

 
学习软件编程开发技术,推荐加入以下软件编程培训班:
易语言软件编程培训班(简单易学)  Delphi软件编程培训班  VC++软件编程培训班
VB软件编程培训班  JAVA高端编程就业研发班

学习网站开发制作技术,推荐加入以下网站开发培训班:
ASP.net网站开发项目实战班  ASP语言网站建设培训班

学习网络安全入侵防护技术,推荐加入以下技术培训班:
大型网络安全入侵防护班  网站脚本程序全方位安全检测班

学习网络管理、网吧运营维护技术(网管),推荐加入以下培训班:
大型网吧技术管理人才特训班  Linux网络嵌入架构工程师培训班

学习专项特殊技术,推荐加入以下专项技术培训班:
软件与游戏外挂脱壳破解班(逆向工程)  赚钱王道-网赚技能培训班  Flash动画设计师就业特训班

 
相关编程
一日一文章
 
一日一软件
一日一动画