another array question

  • Thread starter Thread starter Maarten
  • Start date Start date
M

Maarten

i'm making a form where you can add textboxes with a button.
the max number of textboxes to add is 100

but i also must create an array of al thes textboxes,

is there a way of creating an array of thextboxes that aren't created yet.

fot exapmle
Dim delayarray(100) As TextBox(100) <--------------- this won't work
ofcourse

For inti = 1 To 5

Me.Controls.Add(delayarray(inti))

delayarray(inti).Location = New Point(56, inti *10 + 64)

delayarray(inti).Visible = True

Next



thanks Maarten
 
Dim mytextboxes As TextBox()
ReDim textboxes(100)

for i as integer to 100
mytextboxes(i) = new Textbox
mytextboxes(i).Text = i.tostring
next i
 
Maarten,
Dim delayarray(100) As TextBox(100) <--------------- this won't work
Here you try to create an array of 101 textbox arrays (where the last wont
even go)

When you know how many textboxes you want than use the fixed array
\\
Dim delayarray(99) As TextBox()
For inti = 0 To 99
delayarray(inti).name = "MyTextBox" & inti.tostring
Me.Controls.Add(delayarray(inti))
delayarray(inti).Location = New Point(56, inti *10 + 64)
delayarray(inti).Visible = True
Next
///
When you do not know how many you want to use (strange in this kind of
situations however let us suppose, you can use the arraylist)
\\\
dim delayarray as new arraylist
For inti = 0 To 99
directcast(delayarray,textbox).name = "MyTextBox" & inti.tostring
dlayarray.add(delayarray)
Me.Controls.Add(delayarray(inti))
directcast(delayarray(inti),textbox).Location = New Point(56, inti *10 + 64)
directcast(delayarray(inti),textbox).Visible = True
Next
///
All typed(changed) in this message so watch typos.

I hope this helps?

Cor
 
Dennis said:
Dim mytextboxes As TextBox()
ReDim textboxes(100)

for i as integer to 100
mytextboxes(i) = new Textbox
mytextboxes(i).Text = i.tostring
next i

\\\
Const TextBoxesCount As Integer = 100
Dim TextBoxes(TextBoxesCount - 1) As TextBox
For i = 0 To TextBoxes.Length - 1
TextBoxes(i) = New TextBox()
...
Next i
Me.Controls.AddRange(TextBoxes)
///

;-)
 
Maarten,

Luckily Herfried shows an ommission in my samples.
\\
Dim delayarray(99) As TextBox()

For inti as integer = 0 To 99
delayarray(inti) = new Textbox
delayarray(inti).name = "MyTextBox" & inti.tostring
Me.Controls.Add(delayarray(inti))
delayarray(inti).Location = New Point(56, inti *10 + 64)
delayarray(inti).Visible = True
Next
///
When you do not know how many you want to use (strange in this kind of
situations however let us suppose, you can use the arraylist)
\\\
Dim delayarray As New ArrayList
For inti As Integer = 0 To 99
delayarray.Add(New TextBox)
Me.Controls.Add(DirectCast(delayarray(inti), TextBox))
DirectCast(delayarray(inti), TextBox).Name = "MyTextBox" & inti.ToString
DirectCast(delayarray(inti), TextBox).Location = New Point(56, inti * 10
+ 64)
DirectCast(delayarray(inti), TextBox).Visible = True
Next
///

The last one I checked now.

Cor
 
Cor Ligthert said:
For inti as integer = 0 To 99
delayarray(inti) = new Textbox

\\\
Dim delayarray As New ArrayList
For inti As Integer = 0 To 99
delayarray.Add(New TextBox)
Me.Controls.Add(DirectCast(delayarray(inti), TextBox))
DirectCast(delayarray(inti), TextBox).Name = "MyTextBox" & inti.ToString
DirectCast(delayarray(inti), TextBox).Location = New Point(56, inti * 10
+ 64)
DirectCast(delayarray(inti), TextBox).Visible = True

I'd try to get rid of the 'DirectCast's:

\\\
With DirectCast(delayarray(inti), TextBox)
.Name = ...
.Location = ...
.Visible = ...
End With
///
 
Herfried,

See below
\\\

I'd try to get rid of the 'DirectCast's:

\\\
With DirectCast(delayarray(inti), TextBox)
.Name = ...
.Location = ...
.Visible = ...
End With
///
I find "With" ugly, I know maybe personal, however when it are a lot I would
just do
\\\
dim thistextbox as textbox = directcast(Delayarray(inti),Textbox)
thistextbox.name = ....
thistextbox.location = .....
thistextbox.visible = ....
////.

Cor
 
Cor Ligthert said:
I find "With" ugly, I know maybe personal, however when it are a lot I
would just do
\\\
dim thistextbox as textbox = directcast(Delayarray(inti),Textbox)
thistextbox.name = ....
thistextbox.location = .....
thistextbox.visible = ....
////.

That's personal preference. I didn't use a variable because in this case a
name IMO doesn't add a benefit. My intention to reply was to show a way
that might have a better performance and less "redundancy" in the code ;-).
 
Herfried,
That's personal preference. I didn't use a variable because in this case
a name IMO doesn't add a benefit. My intention to reply was to show a way
that might have a better performance and less "redundancy" in the code
;-).

The "with" has no better performance where you got that idea?
Typing less characters has nothing to do with redundancy or performance.

In all ways it should be exactly the same intermidiate code.

Although you use more rows of code does that as well affects nothing.

Where did you get that idea that "with" will give any better runtime
performance?

Cor
 
Cor Ligthert said:
The "with" has no better performance where you got that idea?
Typing less characters has nothing to do with redundancy or performance.

Sure it has. The cast only needs to be done once opposed to casting every
time you access the object. Other sample:

\\\
Me.Foo(100).Bar("Goo").GetSomething(1).ID = 120
Me.Foo(100).Bar("Goo").GetSomething(1).Name = "Bla"
....
///

vs.

\\\
With Me.Foo(100).Bar("Goo").GetSomething(1)
.ID = 120
.Name = "Bla"
...
End With
///

Notice that the latter solution will have approx. the same "performance" as
your solution with the variable.
In all ways it should be exactly the same intermidiate code.

No!

Sample:

\\\
Public Sub A()
DirectCast(Me, Form1).Enabled = False
DirectCast(Me, Form1).Height = 120
End Sub

Public Sub B()
With DirectCast(Me, Form1)
.Enabled = False
.Height = 120
End With
End Sub
///

.... corresponding MSIL:

\\\
..method public instance void A() cil managed
{
// Codegröße 30 (0x1e)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0
IL_0002: castclass WindowsApplication3.Form1
IL_0007: ldc.i4.0
IL_0008: callvirt instance void
[System.Windows.Forms]System.Windows.Forms.Control::set_Enabled(bool)
IL_000d: nop
IL_000e: ldarg.0
IL_000f: castclass WindowsApplication3.Form1
IL_0014: ldc.i4.s 120
IL_0016: callvirt instance void
[System.Windows.Forms]System.Windows.Forms.Control::set_Height(int32)
IL_001b: nop
IL_001c: nop
IL_001d: ret
} // end of method Form1::A

..method public instance void B() cil managed
{
// Codegröße 29 (0x1d)
.maxstack 2
.locals init ([0] class WindowsApplication3.Form1 _Vb_t_ref_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: castclass WindowsApplication3.Form1
IL_0007: stloc.0
IL_0008: ldloc.0
IL_0009: ldc.i4.0
IL_000a: callvirt instance void
[System.Windows.Forms]System.Windows.Forms.Control::set_Enabled(bool)
IL_000f: nop
IL_0010: ldloc.0
IL_0011: ldc.i4.s 120
IL_0013: callvirt instance void
[System.Windows.Forms]System.Windows.Forms.Control::set_Height(int32)
IL_0018: nop
IL_0019: ldnull
IL_001a: stloc.0
IL_001b: nop
IL_001c: ret
} // end of method Form1::B
///

In 'A' there are two 'castclass' operations.
Where did you get that idea that "with" will give any better runtime
performance?

.... by understanding 'With' and type casts ;-).
 
"Herfried K. Wagner [MVP]"
... by understanding 'With' and type casts ;-).

Will you than explain the ils from main beneath of this sample for me?.

\\\testprogram
Public Class thisClass
Public Shared Sub main()
Dim we As New mytopclass
we = New mysubclass
Dim comments As String = "Start of Directcast"
comments = DirectCast(we, mysubclass).he
comments = "Start of With"
With DirectCast(we, mysubclass)
comments = .he
End With
comments = "Start of setting an adress holder"
Dim c1 As mysubclass = DirectCast(we, mysubclass)
comments = c1.he
comments = "End comments"
End Sub
End Class
Public Class mytopclass
End Class
Public Class mysubclass
Inherits mytopclass
Public Shared ReadOnly Property he() As String
Get
Return "Herfried"
End Get
End Property
End Class
///
\\\Ils from the mainmethode for this
..method public static void main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01
00 00 00 )
// Code size 71 (0x47)
.maxstack 1
.locals init (class WindowsApplication3.mysubclass V_0,
string V_1,
class WindowsApplication3.mytopclass V_2,
class WindowsApplication3.mysubclass V_3)
IL_0000: newobj instance void WindowsApplication3.mytopclass::.ctor()
IL_0005: stloc.2
IL_0006: newobj instance void WindowsApplication3.mysubclass::.ctor()
IL_000b: stloc.2
IL_000c: ldstr "Start of Directcast"
IL_0011: stloc.1
IL_0012: call string WindowsApplication3.mysubclass::get_he()
IL_0017: stloc.1
IL_0018: ldstr "Start of With"
IL_001d: stloc.1
IL_001e: ldloc.2
IL_001f: castclass WindowsApplication3.mysubclass
IL_0024: stloc.3
IL_0025: call string WindowsApplication3.mysubclass::get_he()
IL_002a: stloc.1
IL_002b: ldnull
IL_002c: stloc.3
IL_002d: ldstr "Start of setting an adress holder"
IL_0032: stloc.1
IL_0033: ldloc.2
IL_0034: castclass WindowsApplication3.mysubclass
IL_0039: stloc.0
IL_003a: call string WindowsApplication3.mysubclass::get_he()
IL_003f: stloc.1
IL_0040: ldstr "End comments"
IL_0045: stloc.1
IL_0046: ret
} // end of method thisClass::main
///

Do not ask me to explain it, I did not make that statement as you.

Cor
 
Cor,

Cor Ligthert said:
Will you than explain the ils from main beneath of this sample for me?.

\\\testprogram
Public Class thisClass
Public Shared Sub main()
Dim we As New mytopclass
we = New mysubclass
Dim comments As String = "Start of Directcast"
comments = DirectCast(we, mysubclass).he
comments = "Start of With"
With DirectCast(we, mysubclass)
comments = .he
End With
comments = "Start of setting an adress holder"
Dim c1 As mysubclass = DirectCast(we, mysubclass)
comments = c1.he
comments = "End comments"
End Sub
End Class
Public Class mytopclass
End Class
Public Class mysubclass
Inherits mytopclass
Public Shared ReadOnly Property he() As String
Get
Return "Herfried"
End Get
End Property
End Class

Remove the 'Shared' from the 'he' property and take a look at the IL again.
Otherwise you cannot compare your sample with the situation we are
discussing.
 
Herfried,

I did already not understood why that first casting was not done.

Of course if it is about less than 3 settings probably faster to use the
directcast directly and with more equily and than not the method with "with"
however the reference setting as I showed. (However I would not even talk
about that)

And now I understand what you mean, can you explain how many minutes there
will be gained with this, assuming with building one form as in this sample?

:-) (not angry of course laughing)

Cor


\\\
..method public static void main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01
00 00 00 )
// Code size 79 (0x4f)
.maxstack 1
.locals init (class WindowsApplication3.mysubclass V_0,
string V_1,
class WindowsApplication3.mytopclass V_2,
class WindowsApplication3.mysubclass V_3)
IL_0000: newobj instance void WindowsApplication3.mytopclass::.ctor()
IL_0005: stloc.2
IL_0006: newobj instance void WindowsApplication3.mysubclass::.ctor()
IL_000b: stloc.2
IL_000c: ldstr "Start of Directcast"
IL_0011: stloc.1
IL_0012: ldloc.2
IL_0013: castclass WindowsApplication3.mysubclass
IL_0018: callvirt instance string
WindowsApplication3.mysubclass::get_he()
IL_001d: stloc.1
IL_001e: ldstr "Start of With"
IL_0023: stloc.1
IL_0024: ldloc.2
IL_0025: castclass WindowsApplication3.mysubclass
IL_002a: stloc.3
IL_002b: ldloc.3
IL_002c: callvirt instance string
WindowsApplication3.mysubclass::get_he()
IL_0031: stloc.1
IL_0032: ldnull
IL_0033: stloc.3
IL_0034: ldstr "Start of setting an adress holder"
IL_0039: stloc.1
IL_003a: ldloc.2
IL_003b: castclass WindowsApplication3.mysubclass
IL_0040: stloc.0
IL_0041: ldloc.0
IL_0042: callvirt instance string
WindowsApplication3.mysubclass::get_he()
IL_0047: stloc.1
IL_0048: ldstr "End comments"
IL_004d: stloc.1
IL_004e: ret
} // end of method thisClass::main
///
 
Cor,

Cor Ligthert said:
I did already not understood why that first casting was not done.

I didn't understand that too, but then I saw the 'Shared' ;-).
Of course if it is about less than 3 settings probably faster to use the
directcast directly and with more equily and than not the method with
"with" however the reference setting as I showed. (However I would not
even talk about that)

As said before, it's all about personal preference. Using a variable or
'With' makes maintainance easier and will save time when typing code, plus a
small speed benefit. Sure, that doesn't really matter...
:-) (not angry of course laughing)

:-)))
 

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

Back
Top