problem passing structure to constructor

S

Steve

Hello,

I created a public Structure in a Standard Module and also
an array of Structures. Then I load data into the array
of structures in a public sub that I call on the Form load
event. Next I have a class, and I want to pass a
structure member from my array of Structures to the class
constructor. In the form I instantiate a class object.

Module1
--------------------------
Public arrStruct() As structOne

Public Structure structOne
....
End Structure

public sub LoadStruct()
....
End Sub
-----------------------------

Form1
form_load event
LoadStruct
....
Dim clsOne(arrStruct(1)) As New Class1

In the class constructor I have this:

Public Class Class1
Public Sub New(ByVal strcOne As structOne)
var1 = strcOne.element1
var2 = strcOne.element2
...
End Sub
....

The compile error I get is

'strcOne' cannot expose a Friend type outside of the
Public class 'Class1'. strcOne is the the argument in the
constructor for Class1. When I instantiate Class1 as
clsOne I pass a member of my array of Structures,
arrStruct(1) where the member is a structure.

Is there anything I can do to get around this error?

Thanks,
Steve
 
S

Steve

OK. One workaround that I just tried which seems to work
is to declare the Class as Friend instead of Public. Any
suggestions appreciated if this is correct or I am just
getting lucky. If it is just luck, I respectfully request
how to do it correctly.
 
G

Guest

I believe you should declare you structures as public in the class module;

Class Module
Public Class MyClass

Public Structure MyStruct
......
End Structure

Public Sub New(ByVal FileName As String)
MyBase.New( data as MyStruct)
sClear()
FileH.Name = FileName
End Sub

End Class
......................................
Module 1
Public Sub Main()
dim aStruct as myClass.MyStruct

' Set aStruct members to your data
dim newobject as myClass = New MyClass(aStruct)

end sub
 
J

Jay B. Harlow [MVP - Outlook]

Steve,
Modules by default are Friends, when you define a Structure inside of a
Module it will also be Friend (despite saying Public on the Structure VB.NET
will not promote the visibility higher then the containing type.

I would move the definition of the Structure outside of the Module.

Module Module1
--------------------------
Public arrStruct() As structOne


public sub LoadStruct()
...
End Sub
-----------------------------
End Module
Public Structure structOne
...
End Structure

I normally define structures in their own source file, where each Structure
is in its own source file.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top