|
-
Aug 1st, 2003, 12:51 PM
#1
Thread Starter
Hyperactive Member
Unloading a Module before calling it again.
In the Code examples below I call a module called Compile with a function called Compile. In that module you will see that I define a new form called Errorfrm as new Form3. If I click the meun button once and the errorfrm pops up and displays an error. I then close the newly displayed form and click the menu item again and I get an error in the module compile in the function compile when I try to display the errorfrm. So my question is, Do I have to unload the module or something so I do not get an error?
[code]
Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
Compile.Compile(Code)
End Sub
[\code]
[code]
Module Compile
Dim Errorfrm As New Form3
Public Function Compile(ByVal Code As RichTextBox
' SHOW THE ERROR FORM INCASE THERE IS AN ERROR
Errorfrm.Show() ' THE SECOND TIME I RUN THIS FUNCTION THIS IS WHERE I GET THE ERROR. THE FIRST TIME EVERYTHING RUNS FINE
' CHECK TO SEE IF THERE ARE ANY LINES IN THE CODE
' IF NOT REPORT A MAJOR ERROR AND EXIT
If (Code_Length = 0) Then
' ADD THE ERROR TO THE LIST
Errorfrm.Errorlst.Items.Add("Line 0 - Error Code 1 - Major error, no code to be compiled")
' SINCE IT IS A MAJOR ERROR, NOTHING TO COMPILE
' EXIT THE FUNCTION AND RETURN CONTROL BACK TO THE CALLING FUNCTION
Exit Function
End If
End Function
End Module
[\code]
-
Aug 1st, 2003, 01:22 PM
#2
Why use a module at all? I would switch it to a shared class instead but either way there is no reason to have the instance of form3 to be created at that scope. You can have the compile function create the instance and solve the problem.
VB Code:
Module Compile
Public Function Compile(ByVal Code As RichTextBox
[b]Dim Errorfrm As New Form3[/b] 'inside the method call
' SHOW THE ERROR FORM INCASE THERE IS AN ERROR
Errorfrm.Show() ' THE SECOND TIME I RUN THIS FUNCTION THIS IS WHERE I GET THE ERROR. THE FIRST TIME EVERYTHING RUNS FINE
' CHECK TO SEE IF THERE ARE ANY LINES IN THE CODE
' IF NOT REPORT A MAJOR ERROR AND EXIT
If (Code_Length = 0) Then
' ADD THE ERROR TO THE LIST
Errorfrm.Errorlst.Items.Add("Line 0 - Error Code 1 - Major error, no code to be compiled")
' SINCE IT IS A MAJOR ERROR, NOTHING TO COMPILE
' EXIT THE FUNCTION AND RETURN CONTROL BACK TO THE CALLING FUNCTION
Exit Function
End If
End Function
End Module
-
Aug 1st, 2003, 01:26 PM
#3
Thread Starter
Hyperactive Member
I was going to do that but when my code hits the exit function statment, it will or should (I think) close the errorfrm that was created inside the compile function. And I need the errorfrm to stay open so the user can see his mistakes and fix them.
-
Aug 1st, 2003, 01:35 PM
#4
You should read up on OOP Object Oriented Programming. I started to convert your function for you but there are values that must come from some global values elsewhere because it doesn't make sense. Or maybe you just didn't post all of the code. Try using Showdialog instead of show to keep the form active.
For instance where is CODE_LENGTH declared or where does the value come from?
-
Aug 1st, 2003, 01:41 PM
#5
Try this:
VB Code:
Class Compile
Public Shared Sub Compile(ByVal Code As RichTextBox)
If Code.Text.Length=0 then
Dim Errorfrm As New Form3
Errorfrm.Errorlst.Items.Add("Line 0 - Error Code 1 - Major error, no code to be compiled")
Errorfrm.ShowDialog()
End if
End Sub
End Class
-
Aug 1st, 2003, 11:23 PM
#6
Thread Starter
Hyperactive Member
Thanks, oop in c++ is easy, so this should be a snap, just never cared to use it. Mostly because all of my books are on vb6 and none talk about oop, so i did't know how. But I see what you are talking about. and Code_Length is a value that holds the length of a line of text in my rich text box, so i used a varible insted of doing len(code.line(X)) since a var is faster to access then a function. But thank you for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|