Valhalla Legends Forums Archive | .NET Platform | [Vb.net] Optional or Override

AuthorMessageTime
Imperceptus
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.
[code]    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[/code]

or doing this instead

[code]    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
[/code]

Thoughts please.
July 7, 2007, 8:19 PM
Barabajagal
What's the point of a "default value" when setting an option? Default values are used in getting, not setting.
July 7, 2007, 8:27 PM
Myndfyr
Optional and override don't do the same thing.  Optional and Overloads do.  For instance:
[code]
Public Sub DoSomething(ByVal param1 as Integer, Optional ByVal param2 as Integer = 10)
  CallImplementation(param1, param2)
End Sub
[/code]

This is equivalent to:
[code]
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
[/code]
July 7, 2007, 8:50 PM
Imperceptus
[quote author=Andy link=topic=16857.msg170804#msg170804 date=1183840047]
What's the point of a "default value" when setting an option? Default values are used in getting, not setting.
[/quote]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 author=MyndFyre[vL] link=topic=16857.msg170807#msg170807 date=1183841410]
Optional and override don't do the same thing.  Optional and Overloads do.  For instance:
[code]
Public Sub DoSomething(ByVal param1 as Integer, Optional ByVal param2 as Integer = 10)
  CallImplementation(param1, param2)
End Sub
[/code]

This is equivalent to:
[code]
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
[/code]
[/quote]

Ah alright, that sorta clears it up for me. Thanks MyndFyre
July 8, 2007, 12:10 AM
Barabajagal
[quote author=Imperceptus link=topic=16857.msg170809#msg170809 date=1183853430]
[quote author=Andy link=topic=16857.msg170804#msg170804 date=1183840047]
What's the point of a "default value" when setting an option? Default values are used in getting, not setting.
[/quote]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]
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.
July 8, 2007, 12:25 AM
Imperceptus
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.
July 8, 2007, 12:54 AM

Search