teamdad
asked on
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(ByVa l 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.Val ue, 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)).Lengt h = 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
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(ByVa
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.Val
For j = 0 To results.GetUpperBound(0)
'Can change the "0" to " " to use a space
Dim temp As String = IIf(CStr(results(j)).Lengt
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
' 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
Class and Shared methods.
Bob
Bob
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
'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
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
'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
Corey,
Public Shared Sub DoSomething
You wouldn't need an instance of the class to call:
MyClass.DoSomething
Bob
Public Shared Sub DoSomething
You wouldn't need an instance of the class to call:
MyClass.DoSomething
Bob
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
Regards
Corey
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