• Welcome to Valhalla Legends Archive.
 

[Vb.net] Optional or Override

Started by Imperceptus, July 07, 2007, 03:19 PM

Previous topic - Next topic

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.
    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.
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

Barabajagal

What's the point of a "default value" when setting an option? Default values are used in getting, not setting.

MyndFyre

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
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Imperceptus

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
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

Barabajagal

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.

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.
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.