我们在程序设计中使用很多系统类的时候,往往赋以参数,比如说
dim conn as new sqlconnection(connectionString),高手指点如何在自定义类中实现可以在类实例化的时候就可以指定参数

解决方案 »

  1.   

    推荐找一本C#的基础的书看了,我这里有一本要的话发给你好了。像你上面说的那种叫做“重载”,它重载的是“构造函数”。例如public class SqlConnection
    {
       public SqlConnection()
       public SqlConnection(string conn)
       public SqlConnection(Object ob)
    }像这个类就有3个构造函数,我们可以用下面3种方法实例化。SqlConnection conn = new SqlConnection();
    string str = "ConoString";
    SqlConnection conn = new SqlConnection(str);
    Object ob = new Object()
    SqlConnection conn = new SqlConnection(ob);
      

  2.   

    VB.net中和这用法一样吗?我是在VB.net中这样用的,可是不对
      

  3.   

    这个是C#,VB是这样的:Public Class SqlConnection
        
        Public Sub New()
            MyBase.New
        End Sub
        
        Public Sub New(ByVal conn As String)
            MyBase.New
        End Sub
        
        Public Sub New(ByVal ob As Object)
            MyBase.New
        End Sub
    End ClassDim conn As SqlConnection = New SqlConnection
    Dim str As String = "ConoString"
    Dim conn As SqlConnection = New SqlConnection(str)
    Dim ob As Object = New Object