none
Passing a list of Structured Data to a Subroutine of a Custom User Control.

    Question

  • Hi,

    I created a structure in my application's main form (Form_Main).

     

    Public Structure dbAcctPricing

            Public columnNameDesc As String
            Public description As String
            Public columnNamePrice As String
            Public price As String

        End Structure

    Now when I reference the Structure in my Custom User Control Subroutine,

    Public Sub EnableDescriptionAutoComplete(list_AcctPricing As List(Of dbAcctPricing))

    End Sub

    I get an error "Type 'dbAcctPricing' is not defined.  Basically saying the custom control's code doesn't know what 'dbAcctPricing' is.

    I somehow need to pass the definition of the Structure 'dbAcctPricing' to the custom control, but not sure how. 

    I think it needs an Import Statement of some kind, but not sure. Would using a Tuple be a better choice in this situation? 

    Any help would be greatly appreciated.

    Thanks

    Sunday, March 19, 2017 12:22 AM

Answers

  • Thanks for your reply,

    I thought that the scope of the structure was the problem, but was hoping that structure could be brought into scope with a Imports statement or something like that.

    I have had this work in the past, but in that case, the custom control was outside the project and was imported in. I then referenced and imported the main form into the custom control. Unfortunately, the custom control is within the project this time and I can't get that approach to work.

    I'll try the "Class" approach, although I'm a little unsure exactly how to implement it. Not sure exactly what you mean by "Declare you class outside of the form"?

    Thanks again for your help.

     


    For example:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            ' Code here if applicable...
    
    
        End Sub
    End Class
    
    
    
    Public Class MyNewClass
        ' Code here...
    End Class

    Now "MyNewClass" is scoped outside of the class called "Form1".

    If I put it in Form1, it would be a nested class (of the class named "Form1").


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by 1JRoberts Sunday, March 19, 2017 2:19 AM
    Sunday, March 19, 2017 1:27 AM
  • I'll try the "Class" approach, although I'm a little unsure exactly how to implement it. Not sure exactly what you mean by "Declare you class outside of the form"?

    Class won't change anything.  'Imports' will allow you to abbreviate references, but can make your code more difficult to understand because the location of the definition is not obvious.

    The simplest option is to use the class name of the form to unambiguously identify where this definition is located.

    To declare it outside the form your code would look like this:

    Public Class Form1
        '...
    End Class
    
    Public Structure dbAcctPricing
        Public columnNameDesc As String
        Public description As String
        Public columnNamePrice As String
        Public price As String
    End Structure

    • Marked as answer by 1JRoberts Sunday, March 19, 2017 2:18 AM
    Sunday, March 19, 2017 1:32 AM

All replies

  • That should be a class rather than a structure, but consider the scope of it - it has no way to know what that is.

    Declare your class outside of the form's class instead.

    ***** EDIT *****

    If you're opposed to that idea then fully qualify it when you pass it in. For example "Form1.MyNewClass".


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Sunday, March 19, 2017 12:26 AM
  • Thanks for your reply,

    I thought that the scope of the structure was the problem, but was hoping that structure could be brought into scope with a Imports statement or something like that.

    I have had this work in the past, but in that case, the custom control was outside the project and was imported in. I then referenced and imported the main form into the custom control. Unfortunately, the custom control is within the project this time and I can't get that approach to work.

    I'll try the "Class" approach, although I'm a little unsure exactly how to implement it. Not sure exactly what you mean by "Declare you class outside of the form"?

    Thanks again for your help.

     


    • Edited by 1JRoberts Sunday, March 19, 2017 1:21 AM Grammer
    Sunday, March 19, 2017 1:20 AM
  • Thanks for your reply,

    I thought that the scope of the structure was the problem, but was hoping that structure could be brought into scope with a Imports statement or something like that.

    I have had this work in the past, but in that case, the custom control was outside the project and was imported in. I then referenced and imported the main form into the custom control. Unfortunately, the custom control is within the project this time and I can't get that approach to work.

    I'll try the "Class" approach, although I'm a little unsure exactly how to implement it. Not sure exactly what you mean by "Declare you class outside of the form"?

    Thanks again for your help.

     


    For example:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            ' Code here if applicable...
    
    
        End Sub
    End Class
    
    
    
    Public Class MyNewClass
        ' Code here...
    End Class

    Now "MyNewClass" is scoped outside of the class called "Form1".

    If I put it in Form1, it would be a nested class (of the class named "Form1").


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by 1JRoberts Sunday, March 19, 2017 2:19 AM
    Sunday, March 19, 2017 1:27 AM
  • I'll try the "Class" approach, although I'm a little unsure exactly how to implement it. Not sure exactly what you mean by "Declare you class outside of the form"?

    Class won't change anything.  'Imports' will allow you to abbreviate references, but can make your code more difficult to understand because the location of the definition is not obvious.

    The simplest option is to use the class name of the form to unambiguously identify where this definition is located.

    To declare it outside the form your code would look like this:

    Public Class Form1
        '...
    End Class
    
    Public Structure dbAcctPricing
        Public columnNameDesc As String
        Public description As String
        Public columnNamePrice As String
        Public price As String
    End Structure

    • Marked as answer by 1JRoberts Sunday, March 19, 2017 2:18 AM
    Sunday, March 19, 2017 1:32 AM
  • As far as "why a class and not a structure", it's a judgment call really, but with four strings, a class is (in my opinion) a better choice:

    https://msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Sunday, March 19, 2017 1:40 AM
  • Thanks that did the trick.

    Sunday, March 19, 2017 2:21 AM
  • Thanks for the Help
    Sunday, March 19, 2017 2:21 AM