A few basic questions for a newb coming from VB6

  • Thread starter Thread starter Riffrafter
  • Start date Start date
R

Riffrafter

Hi All,

Been away from programming for a while and just d/l'd the trial version of
VS10 Pro and am playing around in VB.

I've noticed more than a few differences between VB6 and VB10. That's OK
though...we must evolve right?

Here a few very basic things that have me stumped. I've looked in the
documentation, but the examples are more "technical" than I think the
answers call for. I'm hoping someone can help.

I have a bunch of labels on a form that I want to write thru while iterating
thru a loop. In VB6 this was easy with Control arrays. For example:
For X = 1 to 6
Label(x).caption = x
Next

In addition to not having a caption property (it's .text now), how do I
easily reference a label without having to type them all out each time?
That can be very cumbersome.

The Array.sort method looks very promising. I've written more sort routines
than I care to remember and don't mind never doing it again. But I'm having
trouble figuring out the correct syntax to sort an array of "type" Structure
that has two elements, and the value I want to sort on is the second
element. For example:

public structure someData
number as integer
biggernumber as long
end structure

dim Ratspatoot (100) as someData

Need to sort Ratspatoot using the value in Ratspatoot.biggernumber. This
one is probably very simple and I'm just missing something somehow.

Thanks in advance.
 
Regarding your label control question, loop through all the controls,
check to see if it a label then take corresponding action if it is.

Something liekthis:

For Each oCtrl As Control In Me.Controls

'Is this control a label?
If oCtrl.GetType.ToString() = "System.Windows.Forms.Label" Then
oCtrl.Text = "whatever you need"
End If

Next

HTH! Saga
 
Am 05.05.2010 21:25, schrieb Saga:
Regarding your label control question, loop through all the controls,
check to see if it a label then take corresponding action if it is.

Something liekthis:

For Each oCtrl As Control In Me.Controls

'Is this control a label?
If oCtrl.GetType.ToString() = "System.Windows.Forms.Label" Then

If typeof oCtrl is Label Then

is safer and quicker.
 
In fact is the only difference in this between VB6 and VB10 that you cannot
make an array of controls using the designer.

In VB10 there are hundreds ways to make whatever kind of collection so also
an array of labels

Dim labels() as label = {YourFirstLabel, YouNextLabel, YourLastLabel}
For i = 0 to 3 'be aware Net is more zero bound but in VB not always
labels(x).Text = x
next

But instead of VB6, this is like you see only one of the possibilities.

Success

Cor
 
Riffrafter said:
The Array.sort method looks very promising. I've written more sort
routines than I care to remember and don't mind never doing it again.
But I'm having trouble figuring out the correct syntax to sort an
array of "type" Structure that has two elements, and the value I want
to sort on is the second element. For example:

public structure someData
number as integer
biggernumber as long
end structure

dim Ratspatoot (100) as someData

Need to sort Ratspatoot using the value in Ratspatoot.biggernumber. This
one is probably very simple and I'm just missing something
somehow.

Given a structure:

Public Structure someData
Public number As Integer
Public name As String
Public Sub New(ByVal i As Integer, ByVal s As String)
number = i
name = s
End Sub
End Structure

....you can make a List of it and add some data:

Dim dl As New List(Of someData)
For i = 1 To 10
dl.Add(New someData(i, ChrW(91 - i)))
Next

.... then you can sort it using LINQ:

Dim sorted = From t In dl Select t Order By t.name

.... and display it in, for example, a TextBox:

Dim sb As New System.Text.StringBuilder
For Each a In sorted
sb.AppendFormat("{{{0},{1}}}" & vbCrLf, a.number, a.name)
Next
TextBox1.Text = sb.ToString
-------------------

Lists have an advantage that you don't need to bother with Dim-ing them to a
known size.

You can write your own custom array sorts - see the overloads for the
Array.Sort method - but using LINQ is much simpler.

And with LINQ you can do neat things like getting the sum of the sizes of
all files in a a directory:
Dim currentSize = Aggregate f In New DirectoryInfo(thisFolder).GetFiles Into
Sum(f.Length)

You should perhaps read the help in Visual Studio on "Structures and
Classes" and then probably decide to use classes instead of structures. I
can't find the link to it at MSDN, but this conversation may be useful to
you:
http://bytes.com/topic/visual-basic-net/answers/377989-structures-vs-classes

HTH
 
Back
Top