Usage of query in other procedures (public scope)

gs99

Active member
Joined
May 8, 2010
Messages
42
Programming Experience
Beginner
Using Express VB 2010 and SQL 2008.

When a query is made, how can the results be made available to other procedures?
For example, in Module:
VB.NET:
Sub makeQuery()
    dim myQuery ...
End sub

Sub useQuery()
    for each p in myQuery ...
End sub
 
It is best practice to intentionally limit the scope of variables to where they are needed. There is no problem with declaring the variable at module level if that is what is needed. That is why the mechanism exists. This should make the concept of scope easier to understand... The proof is in the electron in the atom in the element in the molecule in the compound in the pudding!

Namespace MyCRMApplication
    Imports Microsoft.VisualBasic ' This import from Microsoft.VisualBasic makes it accessible from everywhere in the MyCRMApplication namespace.
    
    Namespace PurchaseManagement
        Imports System.Text ' This import to the System.Text namespace makes it accessible from everywhere in the PurchaseManagement namespace.
    
        Class PurchaseLog
            Dim PurchaseQuery ' This query variable can be accessed from both RemovePurchase and AddPurchase, but not outside PurchaseLog.
        
            Sub RemovePurchase
                Dim Address ' This variable exists only within the confines of the RemovePurchase procedure. 
                For Each Record In PurchaseQuery ' Record is only accessible from within the For...Next loop, but it executes the Purchase Query.
                Next
            End Sub
        
            Sub AddPurchase
                Dim Address ' This variable exists only within the confines of the AddPurchase procedure. 
                For Each Record In PurchaseQuery ' Record is only accessible from within the For...Next loop, but it executes the Purchase Query too.
                Next
            End Sub
        End Class
    End Namespace

    Namespace OrderManagement
        ...
    End Namespace
End Namespace
 
Last edited:
Yes, you can use a Linq query with a named type, like you posted before
This code works:
VB.NET:
Module Module2
    Dim db As New MyFamilyDataContext
    Dim list5

    Sub makeQuery() 'SQL table Person
        Dim Query5 = From p In myContext.Persons
            Select New Person1 With {.firstName = p.firstName, .lastName = p.lastName}
        list5 = Query5.ToList()
    End Sub

    Sub DisplayList()
        Dim s1 As String
        Dim s2 As String
        For Each p In list5
            s1 = p.firstName
            s2 = p.lastName
        Next
    End Sub

Public Class Person1
    Property firstName As String
    Property lastName As String
End Class

The “ToList()” option puts the Person1 instances in a collection, that can be accessed in other subs.
This is what I was trying to do.

Thanks for your patience!
 
Dim list5
Declare the type for this module-level field, otherwise it is Object !
The “ToList()” option puts the Person1 instances in a collection, that can be accessed in other subs.
Also a IQueryable(Of T) for example can be accessed in other procedures, which is convenient if you always want the latest results at different times in application.
 
Back
Top