|
-
Sep 25th, 2007, 04:23 AM
#1
Thread Starter
Addicted Member
[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.
-
Sep 25th, 2007, 07:19 AM
#2
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.
-
Sep 25th, 2007, 07:37 AM
#3
Thread Starter
Addicted Member
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'.
-
Sep 25th, 2007, 07:38 AM
#4
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.
-
Sep 25th, 2007, 07:41 AM
#5
Thread Starter
Addicted Member
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?
-
Sep 25th, 2007, 07:42 AM
#6
Re: [2005] How do you pass a structure to another form
 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?
-
Sep 25th, 2007, 08:02 AM
#7
Thread Starter
Addicted Member
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?!!
-
Sep 25th, 2007, 07:26 PM
#8
Re: [2005] How do you pass a structure to another form
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|