Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET构造函数_Vb.net_Visual Studio 2008_Constructor - Fatal编程技术网

VB.NET构造函数

VB.NET构造函数,vb.net,visual-studio-2008,constructor,Vb.net,Visual Studio 2008,Constructor,我正在为学校做一个实验室,我遇到了一件我以前从未做过的事情:在我的类中创建一个默认构造函数。它包括创建一个专用字段来存储连接字符串,然后创建一个设置连接字符串的默认构造函数 以下是我到目前为止的情况: Public Class Appointments Private sqlconnection As String = ConfigurationSettings.AppSettings("ConnectionString") Private Property connecti

我正在为学校做一个实验室,我遇到了一件我以前从未做过的事情:在我的类中创建一个默认构造函数。它包括创建一个专用字段来存储连接字符串,然后创建一个设置连接字符串的默认构造函数

以下是我到目前为止的情况:

Public Class Appointments

    Private sqlconnection As String = ConfigurationSettings.AppSettings("ConnectionString")

    Private Property connectionstring() As String
        Get
            Return sqlconnection
        End Get
        Set(ByVal value As String)
        End Set
    End Property

    Public Sub New(ByVal sConnectionString As String)
        sqlconnection = sConnectionString
    End Sub

我这样做对吗?发生了什么事?

在我看来不错,但您已经将连接字符串初始化为顶部的私有变量

您应该允许某人传入连接字符串吗

您的设备可能需要:

Set(ByVal value as String)
 sqlconnection = value
End Set
您还需要一个无参数构造函数,它在实例化对象时为字符串提供一个值

例如,无参数构造函数可以由web/app配置文件设置:

public sub new()
 sqlconnection = ConfigurationSettings.AppSettings("ConnectionString")
end sub
Private sqlconnection As String = ConfigurationSettings.AppSettings("ConnectionString")
整个事情可能是:

    Public Class Appointments 
        Private sqlconnection As String

        Private Property connectionstring() As String 
            Get 
                Return sqlconnection 
            End Get 
            Set(ByVal value As String) 
               sqlconnection = value
            End Set 
        End Property 

        Public Sub New() 
        sqlconnection = ConfigurationSettings.AppSettings("ConnectionString")
        End Sub 

        'optional you could add this but not sure how much of a fuss your professor might make it
    'parameterized constructor
    Public Sub New(ByVal sConnectionString As String) 
        sqlconnection = sConnectionString 
    End Sub 
End Class

默认构造函数是不带参数的构造函数。在本例中,您定义了一个构造函数,它接受1个参数并将其设置为私有字段。您需要通过以下方式更改代码

  • 让构造函数不带任何参数
  • 将私有字段的初始化移到构造函数中
  • 像这样

    Public Class Appointments
    
      Private sqlconnection As String
    
      ...
    
      Public Sub New()
        sqlconnection = ConfigurationSettings.AppSettings("ConnectionString")
      End Sub
    End Class
    

    你是否做得对取决于你想做什么

    您正在执行的操作如下:

    您可以声明一个名为
    sqlconnection
    的私有
    String
    字段,并将其初始化为包含配置文件中的值:

    public sub new()
     sqlconnection = ConfigurationSettings.AppSettings("ConnectionString")
    end sub
    
    Private sqlconnection As String = ConfigurationSettings.AppSettings("ConnectionString")
    
    …然后您设置了一个属性以公开该字段:

    Private Property connectionstring() As String
        Get
            Return sqlconnection
        End Get
        Set(ByVal value As String)
        End Set
    End Property
    
    这里值得注意的是,您没有在
    集合
    访问器中执行任何操作。如果该属性是只读的;移除
    访问器,并将属性标记为
    只读
    。此外,属性是私有的,有效地赋予它与字段相同的作用域。也许这是有意的,但是如果您希望能够从类外部获取(或设置)属性的值,那么它不应该是
    Private

    最后,您定义一个构造函数,使用
    String
    参数,其值分配给私有字段:

    Public Sub New(ByVal sConnectionString As String)
        sqlconnection = sConnectionString
    End Sub
    

    这看起来很正常,即使在替换构造函数中的值时初始化字段有点不必要。由于该类未定义无参数构造函数,如果不替换
    sqlconnection

    的值,就无法创建它。您到底想做什么?我会更进一步,实际创建一个构造函数重载来处理设置sqlconnection,并使用默认值将配置设置传递给重载。这不起作用,因为未定义该集。可能只是指出它就忘了。@john,我只是从OP的原始问题中逐字复制了这一部分。我将删除它,因为这部分答案不需要它。谢谢你指出,好吧,这更有意义。我想这就是我想要实现的。