[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

Module or Class

Tell me if I am wrong but can I place the code below in a class or a module and call it from my form to be used?

Example: I have a form that uses a button and depending on which radiobutton is pressed it will do either one thing or the other like in this code:

'Get known / unknown
        Dim known As ListBox
        If rbnLeast.Checked = True Then
            known = lstLeast
        Else
            known = lstMost
        End If

I want to add this as a 3rd option:

 If unKnown.Checked = True Then
            ' Do what the class or module tells me to do
        End If

I think it would work like calling a public or private function if i'm thinking right but I don't know if or how it could be done.
       

' Code I want in the class or module  ***********************

Imports System
Imports System.IO
Public Class Form1
    Inherits System.Windows.Forms.Form

+ Windows Form Designer generated code

Dim R As New System.Random()

    Private Sub rangeMin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        rangeMax.Minimum = rangeMin.Value
        numsPerLine.Maximum = (rangeMax.Value - rangeMin.Value) + 1
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim j As Integer
        Dim results() As Integer
        Dim output As String

        ListBox1.Items.Clear()


        For i = 1 To numLines.Value
            results = pickFromRange(rangeMin.Value, rangeMax.Value, numsPerLine.Value)

            For j = 0 To results.GetUpperBound(0)
                'Can change the "0" to " " to use a space
                Dim temp As String = IIf(CStr(results(j)).Length = 1, "0" + CStr(results(j)), CStr(results(j)))
                If output <> "" Then output += "-"
                output += temp
            Next
            ListBox1.Items.Add(output)
            output = ""
        Next
    End Sub

    Private Function pickFromRange(ByVal minValue As Integer, ByVal maxValue As Integer, ByVal numberToPick As Integer) As Integer()
        Dim i As Integer
        Dim temp As Integer
        Dim swapWith As Integer
        Dim values(maxValue - minValue) As Integer
        Dim upperBound As Integer

        upperBound = values.GetUpperBound(0)

        ' create array with the range of values
        For i = 0 To upperBound
            values(i) = i + minValue
        Next

        ' for every value, pick another and swap them
        For i = 0 To upperBound
            swapWith = R.Next(0, upperBound + 1)
            temp = values(i)
            values(i) = values(swapWith)
            values(swapWith) = temp
        Next
        ' return the number of values requested
        Dim output(numberToPick - 1) As Integer
        Array.Copy(values, output, numberToPick)
        Return output
    End Function

End Class
0
teamdad
Asked:
teamdad
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Is it the pickFromRange() function you want to move?  You can just add a module and declare it as Public.  Then it can be accessed from anywhere just as if it was in the form itself:

Module Module1
    Public Function pickFromRange(ByVal minValue As Integer, ByVal maxValue As Integer, ByVal numberToPick As Integer) As Integer()
        Dim i As Integer
        Dim temp As Integer
        Dim swapWith As Integer
        Dim values(maxValue - minValue) As Integer
        Dim upperBound As Integer

        upperBound = values.GetUpperBound(0)

        ' create array with the range of values
        For i = 0 To upperBound
            values(i) = i + minValue
        Next

        ' for every value, pick another and swap them
        For i = 0 To upperBound
            swapWith = R.Next(0, upperBound + 1)
            temp = values(i)
            values(i) = values(swapWith)
            values(swapWith) = temp
        Next
        ' return the number of values requested
        Dim output(numberToPick - 1) As Integer
        Array.Copy(values, output, numberToPick)
        Return output
    End Function
End Module

0
 
PaulHewsCommented:
>Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

The best way to do this one would be to create a sub in your module and call it from the button click event.  You have to pass references of the objects you wish to use... Eg

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  MyButtonClick(TextBox1)

End Sub


In the module

Public Sub MyButtonClick(Byval Textbox1 as Textbox)
  Textbox1.Text = "Hello"
End Sub
0
 
teamdadAuthor Commented:
What I am trying to do is what's commented in the sections in this piece of code:

' Use known / unknown
        Dim known As ListBox
        If rbnLeast.Checked = True Then
            known = lstLeast
        Else
            known = lstMost
        End If

        If rbtnBoth.Checked = True Then
            ' use both Least and Most frequent numbers
        End If
        If rbtnnoKnowns.Checked = True Then
            ' Just pick random numbers between rngMin & rngMax for as many numsPerLine and numLines as there are
        End If

It's a ton of code for my whole program so I have put a link up with a zip file of the whole thing if anyone can look at it and help me get the use both Least and Most frequent numbers and just pick random numbers between the variables to working, the least and most parts of the code are working great.

http://webpages.charter.net/teamdad/WindowsApplication41.zip
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
Bob LearnedCommented:
Class and Shared methods.

Bob
0
 
Corey ScheichDeveloperCommented:
Here is a simple example of how to declare and use a class.



'Form code contains

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 If unKnown.Checked = True Then
       Dim MyNewClass as New MyClass
       MyNewClass.DoSomething
 End If  
End Sub


'Your class code

class MyClass
 
Public Sub DoSomething () ' must be public to call it from outside the class
                                       ' Outside classes and modules will see this if you explicitly
                                       ' declare an object like this
                                       ' Dim MyObject as New MyClass
                                       ' OR
                                       ' Dim MyObject as MyClass
                                       ' MyObject = New MyClass
    RunMyPrivateSub
end sub

Regards,

Corey


Private Sub MyPrivateSub () ' since this is private it must be called from within the class
                                         ' Outside classes and modules will not know this exists.
   Msgbox ("You are running MyPrivateSub in the class MyClass")
sub


0
 
Corey ScheichDeveloperCommented:
Sorry about the sloppy post the class should look like this

'Your class code

class MyClass
 
Public Sub DoSomething () ' must be public to call it from outside the class
                                       ' Outside classes and modules will see this if you explicitly
                                       ' declare an object like this
                                       ' Dim MyObject as New MyClass
                                       ' OR
                                       ' Dim MyObject as MyClass
                                       ' MyObject = New MyClass
    MyPrivateSub
end sub



Private Sub MyPrivateSub () ' since this is private it must be called from within the class
                                         ' Outside classes and modules will not know this exists.
   Msgbox ("You are running MyPrivateSub in the class MyClass")
sub

End Class

Regards

Corey
0
 
Bob LearnedCommented:
Corey,

Public Shared Sub DoSomething

You wouldn't need an instance of the class to call:

MyClass.DoSomething

Bob
0
 
Corey ScheichDeveloperCommented:
I had forgotten about that but I don't think it fits the OPs needs.  Otherwise He could just add the code to the form, but if he wants to access it from many different forms then you do have a point.

Regards

Corey

0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now