Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: Imperceptus on July 07, 2007, 03:19 PM

Title: [Vb.net] Optional or Override
Post by: Imperceptus on July 07, 2007, 03:19 PM
I was wondering which is better approach? I would guess the one with Optional seeing its less code in the project. I don't know for sure though, so I ask.
    Public Shared Sub SetOption(ByVal OptionName _
             As String, ByVal OptionValue As String)
        Dim dv As DataView = DSoptions.Tables("ConfigValues").DefaultView
        dv.RowFilter = "OptionName='" & OptionName & "'"
        If dv.Count > 0 Then
            dv.Item(0).Item("OptionValue") = OptionValue
        Else
            Dim dr As DataRow = DSoptions.Tables("ConfigValues").NewRow()
            dr("OptionName") = OptionName
            dr("OptionValue") = OptionValue
            DSoptions.Tables("ConfigValues").Rows.Add(dr)
        End If
    End Sub
    Public Shared Sub SetOption(ByVal OptionName _
             As String, ByVal OptionValue As String, ByVal DefaultValue As String)
        Dim dv As DataView = DSoptions.Tables("ConfigValues").DefaultView
        dv.RowFilter = "OptionName='" & OptionName & "'"
        If dv.Count > 0 Then
            dv.Item(0).Item("OptionValue") = OptionValue
        Else
            Dim dr As DataRow = DSoptions.Tables("ConfigValues").NewRow()
            dr("OptionName") = OptionName
            dr("OptionValue") = OptionValue
            DSoptions.Tables("ConfigValues").Rows.Add(dr)
        End If
    End Sub


or doing this instead

    Public Shared Sub SetOption(ByVal OptionName _
             As String, ByVal OptionValue As String,Optional Byval DefaultValue as string)
        If OptionValue = "" Then OptionValue = defaultvalue
        Dim dv As DataView = DSoptions.Tables("ConfigValues").DefaultView
        dv.RowFilter = "OptionName='" & OptionName & "'"
        If dv.Count > 0 Then
            dv.Item(0).Item("OptionValue") = OptionValue
        Else
            Dim dr As DataRow = DSoptions.Tables("ConfigValues").NewRow()
            dr("OptionName") = OptionName
            dr("OptionValue") = OptionValue
            DSoptions.Tables("ConfigValues").Rows.Add(dr)
        End If
    End Sub


Thoughts please.
Title: Re: [Vb.net] Optional or Override
Post by: Barabajagal on July 07, 2007, 03:27 PM
What's the point of a "default value" when setting an option? Default values are used in getting, not setting.
Title: Re: [Vb.net] Optional or Override
Post by: MyndFyre on July 07, 2007, 03:50 PM
Optional and override don't do the same thing.  Optional and Overloads do.  For instance:

Public Sub DoSomething(ByVal param1 as Integer, Optional ByVal param2 as Integer = 10)
  CallImplementation(param1, param2)
End Sub


This is equivalent to:

Public Overloads Sub DoSomething(ByVal param1 as Integer)
  CallImplementation(param1, 10)   ' Alternatively, call DoSomething(param1, 10) and that overload does parameter checks
End Sub

Pubic Overloads Sub DoSomething(ByVal param1 as Integer, ByVal param2 as Integer)
  CallImplementation(param1, param2)
End Sub
Title: Re: [Vb.net] Optional or Override
Post by: Imperceptus on July 07, 2007, 07:10 PM
Quote from: Andy on July 07, 2007, 03:27 PM
What's the point of a "default value" when setting an option? Default values are used in getting, not setting.
If you code your application to make its own config file instead of deploying it with a config file. You will need to generate default values. You could make another routine to save those values and do some checks to see if it was first run. or you could just try to load the values from the config file and if they don't exist use default ones that are specified in the call.  Those values would then be used and saved when ever you save them.


Quote from: MyndFyre[vL] on July 07, 2007, 03:50 PM
Optional and override don't do the same thing.  Optional and Overloads do.  For instance:

Public Sub DoSomething(ByVal param1 as Integer, Optional ByVal param2 as Integer = 10)
  CallImplementation(param1, param2)
End Sub


This is equivalent to:

Public Overloads Sub DoSomething(ByVal param1 as Integer)
  CallImplementation(param1, 10)   ' Alternatively, call DoSomething(param1, 10) and that overload does parameter checks
End Sub

Pubic Overloads Sub DoSomething(ByVal param1 as Integer, ByVal param2 as Integer)
  CallImplementation(param1, param2)
End Sub


Ah alright, that sorta clears it up for me. Thanks MyndFyre
Title: Re: [Vb.net] Optional or Override
Post by: Barabajagal on July 07, 2007, 07:25 PM
Quote from: Imperceptus on July 07, 2007, 07:10 PM
Quote from: Andy on July 07, 2007, 03:27 PM
What's the point of a "default value" when setting an option? Default values are used in getting, not setting.
If you code your application to make its own config file instead of deploying it with a config file. You will need to generate default values. You could make another routine to save those values and do some checks to see if it was first run. or you could just try to load the values from the config file and if they don't exist use default ones that are specified in the call.  Those values would then be used and saved when ever you save them.
For loading values, not saving values. Your SetOption sub looks like saving, not loading. I see absolutely no reason to have a default value when saving values.
Title: Re: [Vb.net] Optional or Override
Post by: Imperceptus on July 07, 2007, 07:54 PM
Ok so if I put it in the getOption to use a default instead of SetOption.. You wouldn't be arguing this needless point?  This has grown a bit off topic. Thanks for your comments. But The question has already bin answered.