Help with Visual Basic question..make $20

LostHiWay

Golden Member
Apr 22, 2001
1,544
0
76
I'm about to start breaking things. I think it's really stupid to make a Business major take a VB course. But anyway here's my problem.

I'm making a simple program..it has 5 text boxes (string data), a sort button, and a picture box. I'm suppose to use a bubble sort to get the text from the text boxes and display it sorted in the picture box after pressing the sort button. Should be easy right. Not for me!! i've been at this damn thing for the past 3 hours. Any help??!! please!!


Here's what I have so far....

Private Sub cmdSort_Click()
Dim x(1 To 5) As String

x(1) = txtOne.Text
x(2) = txtTwo.Text
x(3) = txtThree.Text
x(4) = txtFour.Text
x(5) = txtFive.Text

(bubble sort goes here)


End Sub
 

xirtam

Diamond Member
Aug 25, 2001
4,693
0
0
That got moved fast!

Why are you displaying text in a picture box?

Are all your text boxes named correctly? txtOne, txtTwo... etc

Are we supposed to provide the bubble sort code where it says "(bubble sort goes here)"? Because that'd be a little... yeah. You should either have or be able to find a code sample for a simple bubble sort.

So I'm thinking... what's the problem? Is it just that you're lost after what you already have, or is what you've typed there giving you problems?
 

LostHiWay

Golden Member
Apr 22, 2001
1,544
0
76
would a textbox make more sense for the output? Also yes all my txt boxes are named right. I'm not really sure where to put the bubble sort code. I just assumed it went there.

I've tried about 25 different bubble sort samples. I can't figure out how to adapt them to get the info from the text boxes.
 

xirtam

Diamond Member
Aug 25, 2001
4,693
0
0
Try making a label instead.

After sorting the textboxes, you can make a line of code something like lblOutput.Caption = (x(1) + x(2) + x(3) + x(4) + x(5))

Scrap the picture box.
 

VBboy

Diamond Member
Nov 12, 2000
5,793
0
0
So what's the problem? Implementing the Bubble Sort?

There are so many examples on the Web and at PlanetSourceCode.com
All you need to do is sort your strings in the x() array.
 

xirtam

Diamond Member
Aug 25, 2001
4,693
0
0
My guess, since he was using a picture box, is that he's sorting everything correctly but didn't know how to output his findings. Make sure you're dealing with x(1), x(2), x(3), x(4), and x(5) in your bubble sort, and that after you're done with it you concatenate it all together. Maybe something like:

Dim sortResults as String
sortResults=""
For n=1 To 5
sortResults = Chr(n+48) + sortResults + x(n) + Chr(13)
Next n
lblOutput.Caption = sortResults

I don't really like VB anymore, but that should work. Just throw that in after your bubble sort code and make sure you have a label named lblOutput.
 

LostHiWay

Golden Member
Apr 22, 2001
1,544
0
76
Originally posted by: VBboy
So what's the problem? Implementing the Bubble Sort?

There are so many examples on the Web and at PlanetSourceCode.com
All you need to do is sort your strings in the x() array.


I'm about to cry..I know this is simple but like I said I'm a business major..I hate programming.

Would anyone be willing to code it out for $20 by paypal. PM me
 

VBboy

Diamond Member
Nov 12, 2000
5,793
0
0
We don't cancatenate string using the "+" operator :)
It's done using the "&" operator.

Also, VbCrLf replaces the Chr$(13)...

And I'm afraid his problem was actually getting the sort to work, not the output :)
 

xirtam

Diamond Member
Aug 25, 2001
4,693
0
0
Originally posted by: VBboy
We don't cancatenate string using the "+" operator :)
It's done using the "&" operator.

Also, VbCrLf replaces the Chr$(13)...

And I'm afraid his problem was actually getting the sort to work, not the output :)

Hm. I tried concatenation with "+" and it worked. I'm used to "&" being used for reference calls and don't really like thinking of it as a concatenation operator. I guess somebody overloaded the + operator to work with strings. Glad they did.

I knew there was some lame constant substitute for Chr(13) + Chr(10), but I can remember the numbers better than I can remember the vbCRLF. And with windows you don't even need the Chr(10), isn't that great?

Is there a plus to using Chr$() over Chr()? Think they do the same thing... and it's easier to leave it out imo since Val() doesn't use a $.

I just found out why I think VB is so atrocious. In their UI, they've got "abl" on their textbox button, so you think you're getting a label, but you're not.
 

VBboy

Diamond Member
Nov 12, 2000
5,793
0
0
Originally posted by: xirtam
Originally posted by: VBboy
We don't cancatenate string using the "+" operator :)
It's done using the "&" operator.

Also, VbCrLf replaces the Chr$(13)...

And I'm afraid his problem was actually getting the sort to work, not the output :)

Hm. I tried concatenation with "+" and it worked. I'm used to "&" being used for reference calls and don't really like thinking of it as a concatenation operator. I guess somebody overloaded the + operator to work with strings. Glad they did.

I knew there was some lame constant substitute for Chr(13) + Chr(10), but I can remember the numbers better than I can remember the vbCRLF. And with windows you don't even need the Chr(10), isn't that great?

Is there a plus to using Chr$() over Chr()? Think they do the same thing... and it's easier to leave it out imo since Val() doesn't use a $.

I just found out why I think VB is so atrocious. In their UI, they've got "abl" on their textbox button, so you think you're getting a label, but you're not.

Let us address these in order.

The "+" operator was originally used in BASIC for string concatenation. However, they introduced "&" operator as it makes more sense (this string and that string and another string...)

$ denotes a string. Mid$, Left$, Right$, etc. Without the $, you will be operating on variants, which are much slower. Val naturally doesn't use it because it does not return a string. Leaving it out is a bad practice. You can write a benchmark and see that it is much slower without the $. It is no longer used in variable declaration, though, because you just Dim sString as String.

I just found out why I think VB is so atrocious. In their UI, they've got "abl" on their textbox button, so you think you're getting a label, but you're not. - what do you mean by that?
 

xirtam

Diamond Member
Aug 25, 2001
4,693
0
0
Originally posted by: VBboy
Originally posted by: xirtam
Originally posted by: VBboy
We don't cancatenate string using the "+" operator :)
It's done using the "&" operator.

Also, VbCrLf replaces the Chr$(13)...

And I'm afraid his problem was actually getting the sort to work, not the output :)

Hm. I tried concatenation with "+" and it worked. I'm used to "&" being used for reference calls and don't really like thinking of it as a concatenation operator. I guess somebody overloaded the + operator to work with strings. Glad they did.

I knew there was some lame constant substitute for Chr(13) + Chr(10), but I can remember the numbers better than I can remember the vbCRLF. And with windows you don't even need the Chr(10), isn't that great?

Is there a plus to using Chr$() over Chr()? Think they do the same thing... and it's easier to leave it out imo since Val() doesn't use a $.

I just found out why I think VB is so atrocious. In their UI, they've got "abl" on their textbox button, so you think you're getting a label, but you're not.

Let us address these in order.

The "+" operator was originally used in BASIC for string concatenation. However, they introduced "&" operator as it makes more sense (this string and that string and another string...)

$ denotes a string. Mid$, Left$, Right$, etc. Without the $, you will be operating on variants, which are much slower. Val naturally doesn't use it because it does not return a string. Leaving it out is a bad practice. You can write a benchmark and see that it is much slower without the $. It is no longer used in variable declaration, though, because you just Dim sString as String.

I just found out why I think VB is so atrocious. In their UI, they've got "abl" on their textbox button, so you think you're getting a label, but you're not. - what do you mean by that?

1. I still don't see why we need more than one operator for concatenation.

2. When would you want Mid(), Left(), Right(), and Chr() them to return anything but strings? Of course, they had the $ in them in QBasic all the time. I'm just not quite sure why there has to be a difference between the Mid$() function and the Mid() function. But of course, there is. Because it's VB, I guess. I would have hoped that VB would just assign the best type for it. For example, I don't need full string allocation for Chr$(13). It's a Carriage Return. Not a Carriage Return-Line Feed, just a Carriage Return. One byte. Hexadecimal D. I've got my typecast for a string. Now where's my typecast for a char?

3. The letters "abl" are all in the world "Label" but not in "Textbox." That button looks like it's supposed to be a label, dang it!
 

VBboy

Diamond Member
Nov 12, 2000
5,793
0
0
It's not "abl". It is "ab" and the carret indicating the text insertion position ("|").

You don't need more than one concat operator. You are strongly discouraged from using "+" to concatenate strings to avoid unexpected type promotion and to improve code readability.

Again, you can but should not use Mid, Left, etc on variants because it will be much slower. The non-string variations are left there for the programmers who are clueless and don't understand the differences between strings and variants. Most programming manuals explain that variants should be avoided.
 

VBboy

Diamond Member
Nov 12, 2000
5,793
0
0
Descartes, where are you, man?! We need you :)
You need to do some typing in this thread :)
 

xirtam

Diamond Member
Aug 25, 2001
4,693
0
0
I agree. Variants should be avoided. So why have them? Either program, or don't program. So here's what I've got. I've used correct concatenation and declared all my variables like a good boy should. Agree?

LostHiWay, try this:

Private Sub cmdSort_Click()

Dim x(1 To 5) As String
Dim N As Integer
Dim I As Integer
Dim bSorted As Boolean
Dim sTemp As String

x(1) = txtOne.Text
x(2) = txtTwo.Text
x(3) = txtThree.Text
x(4) = txtFour.Text
x(5) = txtFive.Text

N = 5
bSorted = False
While (Not (bSorted))
bSorted = True
For I = 1 To (N - 1)
If (x(I) > x(I + 1)) Then
sTemp = x(I)
x(I) = x(I + 1)
x(I + 1) = sTemp
bSorted = False
' list wasn't sorted
End If
Next I
Wend
sTemp = ""
For I = 1 To N
sTemp = sTemp & Chr$(I + 48) & x(I) & vbCrLf
Next I


lblOutput.Caption = sTemp

End Sub
 

LostHiWay

Golden Member
Apr 22, 2001
1,544
0
76
Originally posted by: xirtam
I agree. Variants should be avoided. So why have them? Either program, or don't program. So here's what I've got. I've used correct concatenation and declared all my variables like a good boy should. Agree?

LostHiWay, try this:

Private Sub cmdSort_Click()

Dim x(1 To 5) As String
Dim N As Integer
Dim I As Integer
Dim bSorted As Boolean
Dim sTemp As String

x(1) = txtOne.Text
x(2) = txtTwo.Text
x(3) = txtThree.Text
x(4) = txtFour.Text
x(5) = txtFive.Text

N = 5
bSorted = False
While (Not (bSorted))
bSorted = True
For I = 1 To (N - 1)
If (x(I) > x(I + 1)) Then
sTemp = x(I)
x(I) = x(I + 1)
x(I + 1) = sTemp
bSorted = False
' list wasn't sorted
End If
Next I
Wend
sTemp = ""
For I = 1 To N
sTemp = sTemp & Chr$(I + 48) & x(I) & vbCrLf
Next I


lblOutput.Caption = sTemp

End Sub

OMG!! thank you...also is there a way to remove the numbering of the output...just have it list words in order?
 

LostHiWay

Golden Member
Apr 22, 2001
1,544
0
76
nevermind I figured it out. Let me know your PayPal address and I'll send over the $20
 

VBboy

Diamond Member
Nov 12, 2000
5,793
0
0
We are going to split the $20 since I did most of the typing :D

As for you, LostHiWay, I would recommend that you read a good book instead of looking for quick solutions in the software forum :)
 

xirtam

Diamond Member
Aug 25, 2001
4,693
0
0
Originally posted by: VBboy
We are going to split the $20 since I did most of the typing :D

As for you, LostHiWay, I would recommend that you read a good book instead of looking for quick solutions in the software forum :)

Pssst... you were supposed to ask for a separate cut, VBboy. =P
 

VBboy

Diamond Member
Nov 12, 2000
5,793
0
0
Originally posted by: xirtam
Originally posted by: VBboy
We are going to split the $20 since I did most of the typing :D

As for you, LostHiWay, I would recommend that you read a good book instead of looking for quick solutions in the software forum :)

Pssst... you were supposed to ask for a separate cut, VBboy. =P

LOL. I don't want money. I want fame and respect, and a lifetime membership at ScoreLand.com :)
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
Using & instead of + improves code readability?

I think not.... By convention & is a bitwise and operator in C/C++ and Perl and awk and many others. Just using + to concatenate strings like every other language would be more intuitive/readable. (Of course I use & in VB to avoid some of the side affects I ran into using + before I knew about &). If you want to avoid your string getting converted to a number based on the first thing being concatenated just use CStr to cast the first thing being added....... one of my many pet peeves with VB syntax.

Also the array is of course a variant as there is no other type of VB array, and I strongly suspect the values being pulled out of it are as well and are getting converted back and forth for assignments to sTemp during the sort.
 

VBboy

Diamond Member
Nov 12, 2000
5,793
0
0
Originally posted by: glugglug
Using & instead of + improves code readability?

I think not.... By convention & is a bitwise and operator in C/C++ and Perl and awk and many others. Just using + to concatenate strings like every other language would be more intuitive/readable. (Of course I use & in VB to avoid some of the side affects I ran into using + before I knew about &). If you want to avoid your string getting converted to a number based on the first thing being concatenated just use CStr to cast the first thing being added....... one of my many pet peeves with VB syntax.

Also the array is of course a variant as there is no other type of VB array, and I strongly suspect the values being pulled out of it are as well and are getting converted back and forth for assignments to sTemp during the sort.

An Array is a variant? Visual Basic has String, Integer, Long, and other array types.

Dim saStringArray(1 to 10) As String '-- Dimm'ed as a string array with 10 elements.

Other than that, best regards :)
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
Yes ALL arrays in VB are Variants containing a SAFEARRAY pointer.
Although I haven't seen the "Dim x(1 To 5) As String" syntax used before so that probably makes all the safearray elements strings instead of variants eliminating that extra conversion I thought was there.

Actually I suppose its possible that that's just how it looks externally when you expose the VB functions through COM since COM doesn't marshall naked SAFEARRAY pointers outside a VARIANT.

Dim x(5) creates a variant containing a safearray pointer where the safearray contains 5(6?) variants.

Accessing the SAFEARRAY is pretty inefficient regardless, dwarfing the variant overhead...