Declaring an array

F

Fred Blair

Where is the correct place to declare an array so that it will be available
anywhere in the project.

I have been playing with one form that has two buttons and one listbox. I
want each button to add a name from an array to the list box.

Unless I declare the array inside each button_click event, it says the array
has not been declared.

Help please,
Fred
 
M

Michel Posseth [MCP]

Hello Fred
Where is the correct place to declare an array so that it will be
available anywhere in the project.

Well if you want ti to be aviallable everywhere in your whole project i
would say declare a module ( static class ) and declare your array there as
Friend or Public
( Friend is availlable in your whole Code but only inside your assembly ,
Public is availlable in your whole Code but also outside your assembly )

However although above is technicly possible it is bypassing common coding
guides to code in a more robust self sustainable way
in the case you describe it should be enough to declare the array at Class
level .

And even in the case that you want to use the array in another class (
object ) you could add a property to that class for your array and thus
pass it over and back to the next code logic

regards

Michel Posseth
 
P

Phill W.

Fred said:
Where is the correct place to declare an array so that it will be available
anywhere in the project.

The place to declare an array accessible /anywhere/ in the project in is
a Module:

Module Globals
Public g_values as Integer() = {}
End Module


The /correct/ place for your array, though, is private to the Class or
Module in which it is required and, if required, made additionally
accessible /outside/ of that class via a Property.

Class MainForm
Inherits ... .Form

Private m_values as Integer() = {}

[Public|Protected|Friend|Private] _
[ReadOnly] _
Property Values() as Integer()
Get
Return m_values
End Get
'Set( values as Integer() )
' m_values = value
'End Set
End Property
End Class

Your buttons can access either variable or (better) the Property to
access the array.

HTH,
Phill W.
 
M

Mythran

Michel Posseth said:
Hello Fred

Well if you want ti to be aviallable everywhere in your whole project i
would say declare a module ( static class ) and declare your array there
as Friend or Public
( Friend is availlable in your whole Code but only inside your assembly ,
Public is availlable in your whole Code but also outside your assembly )
</snip>

A little wrong there. Friend is available in the same assembly while public
is outside the assembly ONLY if the containing class is Public. If the
containing class is Friend (or internal), then the methods within the class
are only visible within the same assembly as the class itself is only
visible within the assembly. (although, with reflection you can access them
either way).

HTH,
Mythran
 
M

Michel Posseth [MCP]

Mythran,

You are prooving me wrong by telling the obvious wich i did not claim at
all
So there is nothing wrong with my answer , however the person who would code
as how you describe obviously has no idea of what he/ she is doing ,or are
you going to surprise me with a valid reasson ?

With the "logic" that you used i could "proove" everyones answer wrong in
this group


regards

Michel Posseth [MCP]
http://www.vbdotnetcoder.com
 

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