views:

29

answers:

2
Private _bgWorker As BackgroundWorker = Nothing
Private _bgWorkerMessage As String = String.Empty
Private _bgPercentComplete As Integer = 0
Private _dictQueries As Dictionary(Of String, String) = New Dictionary(Of String, String)

Public Sub New()
    _dictQueries.Add("IPB", "")
    _dictQueries.Add("Figure", "")
    _dictQueries.Add("Part", "")
    _dictQueries.Add("Tags", "")
End Sub

Public Sub New(ByRef bgWorker As BackgroundWorker)
    Me.New()
    _bgWorker = bgWorker
End Sub

Public Sub New(ByVal dictQueries As Dictionary(Of String, String))
    Me.New()
    If Not dictQueries.ContainsKey("IPB") Or Not dictQueries.ContainsKey("Figure") Or Not dictQueries.ContainsKey("Part") Or Not dictQueries.ContainsKey("Tags") Then
        'I want to throw an exception/warning right here, or make it error out'
    End If
    _dictQueries = dictQueries
End Sub

Public Sub New(ByRef bgWorker As BackgroundWorker, ByVal dictQueries As Dictionary(Of String, String))
    Me.New(bgWorker)
    If Not dictQueries.ContainsKey("IPB") Or Not dictQueries.ContainsKey("Figure") Or Not dictQueries.ContainsKey("Part") Or Not dictQueries.ContainsKey("Tags") Then
        'I want to throw an exception/warning right here, or make it error out'
    End If
    _dictQueries = dictQueries
End Sub

My goal is to make a class that can import the same file type and structure to any database as long as a connection and queries are set. We have a "common" database schema, but whenever we create SQLite apps, it's not guaranteed that the developer will use the same schema. So, in the main constructor, I'm going to define the common queries. But if they define the queries, I want to make sure there are four specific ones as those will be the only ones I use. Now, I wanted to check it in the constructor so that maybe the developer would catch it. It's pointless for the user to catch it, cause then the product shouldn't have been released. So, is there a way to check a parameter on a constructor to make sure it is "valid". I know Visual Studio does this automatically with type, but I don't know if there's a way to check the values to make sure they're what's needed. All I want to do is throw a warning rather than keep it from building (they may pass in a value that is built from a database, who knows).

+2  A: 

I've seen this called "Stringy typing" in another SO thread. No, not possible, you cannot find out what the string contains until runtime. Even the value of testing it at runtime is iffy, there could be many more things wrong with the query statement. You do seem to be re-inventing stored procedures, consider using the dbase engine support for them.

Hans Passant
The reason it seems like that is because we try to keep all decision making in the code so when we distribute the product, it's less likely the end user can see the logic behind it. Job Security. :) Not my decision though. :(
XstreamINsanity
A: 

Nah, I think the closest you can get is to actually strongly type the parameters. Create IPBString, FigureString, etc. so that developers have no other choice but to pass in the right thing. This seems a bit overkill though. What about using Code Contracts?

Brian Gideon