views:

242

answers:

3

I'm doing a lab for school, and I came across something I have never done before: create a default constructor within my class. It involves creating a private field to store the connection string, then create a default constructor that sets the connection string.

Here is what I have so far:

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

Am i doing this right? What the heck is going on?. Please help a noob out.

+2  A: 

Looks good to me but you've already initilized the connection string as a private variable up top.

Are you supposed to allow someone to pass in a connection string?

Your set may need to be:

Set(ByVal value as String)
 sqlconnection = value
End Set

You also need a parameterless constructor, something which gives the string a value when you instantiate an object.

For instance the parameterless constructor could be set by a web / app config file:

public sub new()
 sqlconnection = ConfigurationSettings.AppSettings("ConnectionString")
end sub

The whole thing could be:

    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
JonH
Okay this makes more sense. I think this is what I was trying to accomplish.
broke
+2  A: 

A default constructor is a constructor which takes no arguments. In this case you've defined a constructor which takes 1 argument and sets it to a private field. You'll need to change your code in the following ways

  1. Have the constructor take no parameters
  2. Move the initialization of the private field into the constructor

Like so

Public Class Appointments

  Private sqlconnection As String

  ...

  Public Sub New()
    sqlconnection = ConfigurationSettings.AppSettings("ConnectionString")
  End Sub
End Class
JaredPar
I would take this a step farther and actually create a constructor overload to handle setting sqlconnection and use the default to pass the config setting to the overload.
Joel Etherton
This won't work because the set isn't defined. Could be forgotten just pointing it out.
JonH
@JonH, I just copied that part verbatum from the OP's original question. Going to remove it because it's not necessary for this part of the answer. Thanks for pointing it out
JaredPar
+1  A: 

Whether you are doing it right or not depends on what you want to do.

What you are doing is the following:

You declare a private String field called sqlconnection and you initialize it to contain a value from the config file:

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

...then you have set up a property to expose the field:

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

Noticable here is that you do not do anything in the Set accessor. If the property is to be readonly; remove the Set accessor and mark the property as ReadOnly. Also, the property is Private, effectively giving it the same scope as the field. Perhaps this is intentional, but if you want to be able to get (or set) the value of the property from outside the class, it should not be Private.

Finllaly, you define a constructor, taking a String parameter, the value of which is assigned to the private field:

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

This looks quite OK, even though it is a bit unnecessary to initialize the field, when you replace the value in the constructor. Since the class does not define a parameter-less constructor, there is no way to create it without replacing the value of sqlconnection.

Fredrik Mörk