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

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

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

    16.字符串连接

// 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(" !!!")

// Using Strings
var s1 : String;
var s2 : String = "hello";
s2 += " world";
s1 = s2 + " !!!";

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


    17.事件处理程序委托

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) {
...
}


    18.声明事件

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


    19.向事件添加事件处理程序或从事件移除事件处理程序

Control.Change += new EventHandler(this.ChangeEventHandler);
Control.Change -= new EventHandler(this.ChangeEventHandler);

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

Control.Change += this.ChangeEventHandler;
Control.Change -= this.ChangeEventHandler;


    20.强制类型转换

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;


    21.转换

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(...), ...

var i : int = 3;
var s : String = i.ToString();
var d : double = Number(s);


    22.带继承的类定义

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


import System;

package MySpace {

  class Foo extends Bar {

    private var x : int;

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

}

// jsc /out:libraryjs.dll library.js


    23.实现接口

public class MyClass : IEnumerable {
 ...

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

Public Class MyClass : Implements IEnumerable
 ...

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


public class MyClass implements IEnumerable {
 ...

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


    24.带 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


    25.标准模块

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

print("Hello World");

// jsc /out:consolejs.exe /exe console.js

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

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

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

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

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

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