Results 1 to 8 of 8

Thread: [RESOLVED] [2005] How do you pass a structure to another form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Resolved [RESOLVED] [2005] How do you pass a structure to another form

    Hello all,
    I received some excellent advice from this forum yesterday concerning structures as opposed to arrays. I had wanted to create an array, and send it to another form to display its contents in a list box. I had assumed that I could pass the structure to the form in the same way I am currently passing strings and integers - but it doesn't appear to be quite as simple.

    What I'm getting is an error which says:
    Value of type 'System.Collections.Generic.List(Of MyApplication.CalculateResults.Products)' cannot be converted to 'System.Collections.Generic.List(Of MyApplication.DisplayResultsWindow.Products)
    ???? Can't be converted from a Generic List to a Generic List??

    As I'm still quite low down on my learning curve, I've hacked it about a bit, and my main form calls a class, which is set up like:
    Code:
    Public Class CalculateResults
    
        Public Structure Products
            Sub New(ByVal strID As String, ByVal strQuantity As String, ByVal strValue As String)
                ID = strID
                Quantity = strQuantity
                Value = strValue
            End Sub
    
            Dim ID As String
            Dim Quantity As String
            Dim Value As String
        End Structure
    
        Public strProducts As New List(Of Products)
    
        Private Sub DisplayResultsWindow(ByVal etc, etc......
    
            Dim frmDisplayResultsWindow As New DisplayResultsWindow
    
            With frmDisplayResultsWindow
                ... a list of some other variables to pass to frmDisplayResultsWindow
                .ProductsOutOfBalance = strProducts
            End With
    
            frmDisplayResultsWindow.ShowDialog()
    
         End Sub
    And my form to display the results looks like:
    Code:
    Public Class DisplayResultsWindow
    
        Public Structure Products
            Sub New(ByVal strID As String, ByVal strQuantity As String, ByVal strValue As String)
                ID = strID
                Quantity = strQuantity
                Value = strValue
            End Sub
    
            Dim ID As String
            Dim Quantity As String
            Dim Value As String
        End Structure
    
        Public WriteOnly Property ProductsOutOfBalance() As List(Of Products)
            Set(ByVal value As List(Of Products))
    
                .... still learning, so not sure what I need to do at this point, but I'll figure it out!
    
            End Set
        End Property
    Thanks in advance for your comments.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] How do you pass a structure to another form

    you don't want to declare the structure on both forms, rather you should be declaring the structure as public on the main form so that it is accessible to other forms or you can declare it as public in a module.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2005] How do you pass a structure to another form

    Thanks for your reply. I've made the following changes, and the error has moved to somewhere else.
    • I've created a module, and I've put the Products structure in it, and I've removed it from everywhere else.
    • I've put the strProducts declaration in the module as public, too.


    The error is now here:
    Code:
    Public Class DisplayResultsWindow
    
        Public WriteOnly Property ProductsOutOfBalance() As List(Of Products)
            Set(ByVal value As List(Of Products))
    
                ....
    
            End Set
        End Property
    and reports:
    'ProductsOutOfBalance' cannot expose type 'PublicDeclarations.Products' outside the project through class 'DisplayResultsWindow'.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How do you pass a structure to another form

    Don't declare the structure in either form. It's a type, just like a class. You should be declaring it in its own code file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2005] How do you pass a structure to another form

    I think our posts may have crossed. Can you provide any pointers on my latest attempt?
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How do you pass a structure to another form

    Quote Originally Posted by penguin5000
    Thanks for your reply. I've made the following changes, and the error has moved to somewhere else.
    • I've created a module, and I've put the Products structure in it, and I've removed it from everywhere else.
    • I've put the strProducts declaration in the module as public, too.


    The error is now here:
    Code:
    Public Class DisplayResultsWindow
    
        Public WriteOnly Property ProductsOutOfBalance() As List(Of Products)
            Set(ByVal value As List(Of Products))
    
                ....
    
            End Set
        End Property
    and reports:
    Again, you shouldn't be declaring the structure inside any other type, including a module. Declare it in its own code file. It's a type, just like a class or a module. Treat it like any other type and declare it in its own code file. Don't nest it inside any other type unless you have a specific reason for doing so.

    As to your issue, I'm guessing that your module is declared Friend, so any types declared inside it are also Friend. You can't then expose a Friend type via a Public variable. If a type cannot be seen from outside the assembly then how can an instance of the type?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2005] How do you pass a structure to another form

    Brilliant! Thanks. I had no idea you could make declarations in their own files. Where do you learn this stuff?!!
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How do you pass a structure to another form

    Quote Originally Posted by penguin5000
    I had no idea you could make declarations in their own files.
    Yes you did, because you do it all the time. Every time you add a new form, class or module to your project you're doing exactly that. A structure is a type just like any of them. There's no right-click option to create one because they aren't so common. You can either add a code file from the project items dialogue and create your own declaration or I just add a class and then change the 'Class' key words to 'Structure'. I also add a code file named "Enumerations.vb" to any projects in which I need to declare enumerated types. That's the only time I'll put more than one type in the same code file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width