Question How do I return a dictionary collection from my function

candlestick

New member
Joined
Apr 6, 2009
Messages
3
Programming Experience
Beginner
Hi Guys,

A. Not sure if this is the correct place to post
B. Bit of a noob with the old VB.net.

Just wrote a function in my service that is meant to return a dictionary of string string but no joy. Im not sure if my syntax is correct at all. Can someone have a quick look and let me know please?

Here is the source:

VB.NET:
'===== call the function ========
Dim External_line_prefixes As Dictionary(Of String, String) = GetAllExternalLinePrefixes()

'===== the function ========
    Private Function GetAllExternalLinePrefixes()
        'pre: oncall retrives all records from the linePrefix and region columns of the site lookup table 
        'post: returns a dictionary collection of the site names and the prefixes
        'NT: Dictionary format = site_line_prefixes[<str_sitename>][<str_prefix>]
        'NT: Set the table name in the .config file under appsettings with keyname: siteTableName. 

        '==connect to db==
        Dim objconn As Object
        objconn = DatabaseConnection()
        Dim objCommand As SqlClient.SqlCommand
        objconn.Open()

        '==get site table variables for sql command from .config file==  
        Dim strTableName As String = System.Configuration.ConfigurationManager.AppSettings.Item("SiteTableName").ToString()        

        '==create and run querie for extracting all line prefixes and the site names==
        Dim returnValue As SqlClient.SqlDataReader
        objCommand = New SqlClient.SqlCommand("select lineprefix, region, didgitstouse from " & strTableName)
        returnValue = objCommand.ExecuteReader()

        '==read querie results into dictionary collection object==
        'create a string dictionary object
        Dim site_line_prefixes As New Dictionary(Of String, String)
        'loop through all results
        While returnValue.Read()
            'add sitename as dictionary key and prefix as dictionary value
            site_line_prefixes.Add(returnValue(0), returnValue(1) & "^" & returnValue(2))
        End While

        '==close and return==
        'close objects
        objconn.Close()
        objconn.Dispose()
        objCommand.Dispose()
        objconn = Nothing
        objCommand = Nothing

        'return dictionary
        Return site_line_prefixes

    End Function
 
Last edited by a moderator:
no it dosent work

No, I get a error in event viewer pointing me to the line where i call this function. Does anyone know the syntax used to return a dictionary? That's really all I am asking.
 
1. When posting code, please always use CODE tags for readability. I've added them for you this time but in future please type them in by hand yourself or use the button provided on the advanced editor.

2. If you want help with a problem and there's an error message provided by the IDE then ALWAYS post that error message, especially when someone trying to help you specifically asks for it. Even if the message doesn't mean anything to you it will probably mean something to those with more experience and it saves us a lot of time if we know exactly where to look for the issue.

With regards to point #2 above, what is your error message? I'm guessing that it's something like "type Object cannot be converted to type Dictionary(Of String, String)" although, in that case, I'd expect an error on the method declaration too.
 
Will do on the code tags in the future.

The exact error I am getting in evntvwr is:
Object reference not set to an instance of an object.
at <line where function is called>
at <line 11 in the function "objconn.Open()">

I understand this is related to a variable not being declared, however objconn has been declared. So I thought it might have somting to do with the way I am interacting with the dictionary object, hence the question: how do you return a dictionary from a function.

Cheers for the help.
 
OK, then. The answer is that this issue has absolutely nothing to do with a Dictionary. This is why you need to provide information about error messages. What you're doing as far as creating and returning the Dictionary is fine, although I will mention some improvements later. Your issue is something else entirely, which I didn't see because I wasn't looking for it because you emphasised something else.

The problem is that your 'objconn' variable refers to Nothing when you call Open and you can't open a connection that doesn't exist. That means that your DatabaseConnection method is not returning an object. That's where you should be looking.

Now, there are some other issues there too, although they won't stop your code working. First, what type of object is 'objconn' supposed to refer to? From your code it must be a SqlConnection, so why have you declared it as Object? You should ALWAYS declare EVERYTHING as the most specific type you can. In a similar vein, what type of object is your GetAllExternalLinePrefixes function supposed to return? Of course, it's Dictionary(Of String, String), so why haven't you specified it as such? If you don't specify the return type of a function then it is assumed Object by default.

The types of issues I just mentioned all come into your code because Option Strict is Off by default. With Option Strict On the compiler enforces strict typing and won't allow late-binding. This means that, for example, you cannot assign from an Object variable to something more specific, even if the actual object it refers to is that type. It also means that you MUST specify a return type for EVERY function. This may seem like more trouble but it forces you to write better code that run faster. It will also catch a lot of potential issues at compile time instead of allowing them to sneak through and cause a crash at run time.

I suggest that you turn Option Strict On for your current project and then fix all the errors that get flagged as a result. We can help you do that and you'll learn a lot about types, casting and type conversions as a result. You should then make Option Strict On the default for all future projects.
 
Back
Top