1.

Solve : Useful Pieces Of Code?

Answer»

Hi Everybody,

The point of this thread is so that people can post different pieces of code that other people can integrate into their programs.

When posting please include the following information:

- The LANGUAGE e.g. Visual Basic, C#, C++ .etc
- A description of what it does
- The code itself (In a code box)
- Where the code should be placed e.g. a Command Button's click event

I would also recommend that if you find a PIECE of code useful that you press the thanks button on their post.

Cameron GrayCracked open my old module that I no longer use (all my code that I use daily is wrapped in classes).


VB6; Place wherever...

ShellSort routine:

Code: [Select]Sub ShellSort(A As Variant, Optional ByVal LB As Long = 0, Optional ByVal UB As Long = 0)
     Dim N As Long
     Dim H As Long
     Dim I As Long
     Dim j As Long
     Dim T As Variant
     If LB = 0 And UB = 0 Then
          LB = LBound(A)
          UB = UBound(A)
     End If
               ' sort array[lb..ub]
     
               ' compute largest increment
     N = UB - LB + 1
     H = 1
     If (N < 14) Then
          H = 1
     Else
          Do While H < N
               H = 3 * H + 1
          Loop
          H = H \ 3
          H = H \ 3
     End If
     
     Do While H > 0
                    ' sort by insertion in increments of h
          For I = LB + H To UB
               T = A(I)
               For j = I - H To LB Step -H
                    If A(j) <= T Then Exit For
                    A(j + H) = A(j)
               Next j
               A(j + H) = T
          Next I
          H = H \ 3
     Loop
     
End Sub


Or, a small procedure to find the maximum of a set of items.

Code: [Select]Public Function Max(ParamArray Params) as Variant
    Dim I as long,CurrMax as Variant
    CurrMax=vbempty
    For I = 0 to ubound(Params)
        if IsEmpty(CurrMax) or Params(I) > CurrMax then
            CurrMax = params(I)
        end if
    Next I
    Max = CurrMax
End Function

a Min() function can easily be created by switching the comparison operator. (and of course the function name).

another small routine for shuffling an array.

Code: [Select]Public Sub ShuffleArray(ByRef vArray As Variant, Optional startIndex As Variant, Optional endIndex As Variant)
     Dim I As Long
     Dim rndIndex As Long
     Dim Temp As Variant
     
     If IsMissing(startIndex) Then
          startIndex = LBound(vArray)
     End If
     
     If IsMissing(endIndex) Then
          endIndex = UBound(vArray)
     End If
     
     For I = startIndex To endIndex
          rndIndex = Int((endIndex - startIndex + 1) * Rnd() + startIndex)
         
          Temp = vArray(I)
          vArray(I) = vArray(rndIndex)
          vArray(rndIndex) = Temp
     Next I
End Sub


All of these are self-contained. Either put them in a module or a class.


Thanks BC_Programmer Anybody else?

The code can be for anything, Even a simple Hello World.

Cameron Graynot really usefull....or advanced, but its used in visual basic to just reverse a string

Code: [Select]dim rvstr
rvstr = "Blarg"
rvstr = StrReverse(rvstr)
msgbox rvstr
so you can use it in VB6... 2 TEXT box(rvs and out), 1 command button(cmnd),


Command button:
Code: [Select]rvstr = StrReverse(rvs.text)
out.txt = rvstror a VB5 Version of the function:

Code: [Select]Function StrReverse(ByVal Inputstr As String)
Dim X As Integer, stringer As String, Rstr As String, s As String
Rstr = ""
stringer = Inputstr
For X = Len(stringer) To 1 Step -1
     s = Mid$(stringer, X, 1)
     Rstr = Rstr & s
Next X
StrReverse = Rstr

End Function

And it's a commandbutton, silly. Command box, LOL.

oop corrected,

terribly sorry, new to vb  well.. it is shaped LIKE a box.

As long as you don't call a form a window box...  you know, that would explain why my programs aren't working!!



Discussion

No Comment Found