static Array in VB.NET (newbie ques.)

  • Thread starter Thread starter chris
  • Start date Start date
C

chris

Hello all -

I have a form that has several checked list boxes. I want to maintain a
static (Shared) array to make looping through them easier. I have tried
several things unsuccessfully.

I wanted to declare a the array like this:

'private shared foo() as checkedlistbox = {item, item, item}' and access it
with a ReadOnly property. I received compile errors here (need reference to
instance). Anyway, what would be the recommended way of tackling this
problem? Any suggestion is appreciated. Example if possible. Thanks.
 
Hi chris,

I made this sample yesterday.

It is about textboxes, to trim the spaces on both sides, but you can use it
for your listboxes as well

I hope it helps?

Cor

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim txtboxarea As TextBox() = New TextBox() {TextBox1, TextBox2}
For Each txt As TextBox In txtboxarea
AddHandler txt.LostFocus, AddressOf TextBox_LostFocus
Next
End Sub
Private Sub TextBox_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, TextBox).Text = _
Trim(DirectCast(sender, TextBox).Text)
End Sub
///
 
chris said:
Hello all -

I have a form that has several checked list boxes. I want to
maintain a static (Shared) array to make looping through them
easier. I have tried several things unsuccessfully.

I wanted to declare a the array like this:

'private shared foo() as checkedlistbox = {item, item, item}' and
access it with a ReadOnly property. I received compile errors here
(need reference to instance). Anyway, what would be the recommended
way of tackling this problem? Any suggestion is appreciated.
Example if possible. Thanks.

What's the content of the array 'foo' before an instance of the form exists?
What if multiple instances of the Form exist? Why not make an instance
(not-shared/static) array?
 
Armin said:
What's the content of the array 'foo' before an instance of the form exists?

The array is part of the application startup form.
What if multiple instances of the Form exist?

Then multiple copies of the array would also exist.

Why not make an instance (not-shared/static) array?

I think that is the question I was asking. I do not yet have a full
understanding of the VB.NET language. It is my understanding that Shared in
VB is equivalent to static in other languages. If this is not the case,
could you provide a sample dispelling any mysteries?


TIA
 
chris said:
The array is part of the application startup form.

Then the hidden sub main is:

shared sub main
application.run(new startupform)
end sub

What is the content of 'foo' before the startupform is created?
Then multiple copies of the array would also exist.

No. It is a shared array that exists exactly once.
Why not make an instance (not-shared/static) array?

I think that is the question I was asking. I do not yet have a full
understanding of the VB.NET language. It is my understanding that
Shared in
VB is equivalent to static in other languages. If this is not the
case, could you provide a sample dispelling any mysteries?

Remove the shared keyword. Then you have one array for each instance.
 
Back
Top