coding help
Public
Class Form1Dim ansAsStringPublicSub Form1_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load'whatever is in here will be executed when you start the program'EndSubPublicSub Button1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button1.Click'whatever is in here will be executed when you press the button 1'test()
'this gives me "test is type and cannot be used as expression"
ans = InputBox(
" thats it")EndSubPublicSub Button2_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button2.ClickEndSubEnd
ClassPublic
Module testDim ansAsStringmsgbox(
" hello ")' this gives me declaration expected'
End
Modulethe compiling of these, and the passing of values im finding is not logical, if i have a variable i should be able to call it whereever i want
[3654 byte] By [
chaza] at [2007-12-25]
the name of your module is test. You create an instance of this module:
Dim theTestModule as new Test()
next up, in your module, you need to have a sub/function in order to execute code. you cannot just leave it like the way it is right now otherwise you will get errors
Now, doing any form of user prompts most be done at the Windows Forms level, not a module level. It was stated before as well in one of your other threads.
So the correct code for your module would be this:
Public Module test
public sub DoSayHello()
Dim ans As String
msgbox(" hello ")
end sub
End Module
Now the "ans" is pretty useless there doing nothing, of course I believe all of this is incomplete code.
So now from your main form you can call the module:
Dim theTestModule as new Test()
theTestModule.DoSayHello()
In order for other classes to access your module properties/functions/subs, you need to make them public. Public exposes them publically so the caller can see them and access them. Think of it like your house....
if your door is closed, no one from the outside can see/walk in correct? It's private right?
If your door is open, people can go in/access the house right? It's public!
It would help if you can explain further what you wish to achieve. This is how to create a module and hopefully you can follow the pattern :-)
all these so called useless bit of code have the function of testing my undertsanding, they are done so i can test and change things to see what happens, that will then satify me that i have really undertsood.
im sorry to be any trouble
what exactly are you having trouble understanding? Can you explain bit by bit/line by line?
as i said ahmed, i create these to test me.the next one i have is this.
Public
Class Form1Dim ans As StringPublic Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load'whatever is in here will be executed when you start the program'End SubPublic Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click'whatever is in here will be executed when you press the button 1'sayit()
ans = InputBox(
" thats it")End SubPublic Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ClickEnd SubEnd
ClassPublic
Module testDim ans As StringPublic Sub sayit()MsgBox(
" hello ")' this all works great'Form2()' this does not'
End SubEnd
ModulePublic
Class Form2
Public Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MsgBox(" now we are getting there")
End Sub
End Class
unless i know what is going on i wont learn.
ok well, Form2 doesnt exist in the module at all. You havent made a reference to it or even created an instance of it.
When you are trying to accessing a module or class, you need to have some form of reference to it. Either by perhaps the caller passing it in to your module/class or by creating an instance of the class/module. So in this case you need to declare Form2()/create an instance of it:
Dim theSecondForm as new Form2()
then show it, if you want to:
theSecondForm.Show()
so say if you had other classes in your program:
ClassA
ClassB
ClassC
in order to access any one of them from anywhere, you need to have a reference to them. You need to create/instantiate them and then can you access them. It's like when trying to get to your house, you need to get some directions or remember those directions so you can get there right? Similar thing happening here - you need to give it directions/references to where these classes are and how you can access them
Take a look at this also, perhaps this well help in some way:
http://www.vbip.com/books/1861004915/chapter_4915_03.asp
that all worked great, if i want to call newform form other modules or classes do i hever dim newform there as well or does 1 dimension work for all of the modules or classes.
the second link you sent was good. then cant i just use property instead of subs, sem though every one can see their vaules, and just call the property instaed of subs.
i have played with the program so far, and it works good and helps me get the idea of what is going on, but when i go around the 2nd time pressing the buttons on the forms, i get an error, objectdeposedexception was unhandled.
what did it not like
you can use properties yes, properties hold values, a method/function/sub is a procedure that does "work" - a job. Properties hold values and you can expose properties to external classes (get/set values).
can you explain what the error message was telling me please
well objectdisposedexception means that you are trying to access an object that no longer exists and has been disposed of. Where does this error happen and can you supply the entire code you currently have so we can see how to resolve this?
Public
Class FirstformDim ans As StringDim newscreen As New newformPublic Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load'whatever is in here will be executed when you start the program'End SubPublic Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click'whatever is in here will be executed when you press the button 1'sayit()
newscreen.Show()
End SubPublic Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnquit.ClickMe.Hide()MsgBox(
" thats it")EndEnd SubEnd
Class
Public
Class newformPublic Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load' this will show the msgbox when newform starts'MsgBox(
" now we are getting to newform")End SubPrivate Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label1.ClickEnd SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click' this will change the value of the label when you press the button'label1.Text =
"look its changed the text in the label"End SubPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnquit.ClickMsgBox(
" we are going to clodse newform and go back to firstform")Me.Close()Firstform.Show()
End SubEnd
ClassPublic
Module testDim ans As String' this is to tell the program where the newform prgram is''you will then call newscreen from your subs'Dim newscreen As New newformPublic Sub sayit()MsgBox(
" this is from your module ")End SubEnd
Modulethe second time i go around ans press the quit button on newform to start the program again, the error comes at;
newscreen.show on the firstform
firstly, there is no need to put in "end" in your code. It will end the application but not correctly in some cases. If you want your application to close/quit correctly, use Application.Exit()
Now to the main problem.
in the newform, when you press button2_click, you are closing the form (Me.Close()) which disposes the form. So in your first form, when you press that button again to show the newform, it cannot as the newform has been disposed of and no longer available.
You would need to either create a new instance of the newform in the button_click event on the first form where you are showing the newform:
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'whatever is in here will be executed when you press the button 1'
sayit()
if me.newscreen is nothing then
me.newscreen = new newform()
end if
newscreen.Show()
End Sub
or just don't do Me.Close() in the newform when you click the second button in newform, but just perhaps hide it instead (Me.Hide()) then in your button1_click event in the first form, just do a "bringToFront". Example:
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'whatever is in here will be executed when you press the button 1'sayit()
if me.newscreen is nothing then
me.newscreen = new newform()
end if
if me.newscreen.visible = false then
newscreen.Show()
end if
newscreen.BringToFront()
End Sub
newform:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnquit.Click
MsgBox(
" we are going to clodse newform and go back to firstform")Me.Hide()'rest of code here
does this make some sense? You are closing the newform when you press the button in newform, which means that the form is no longer valid/availible so next time you try to access it from the mainform (first form) it will throw the error since the object no longer exists (disposed of)
yes that maks sence in fact that is what i thought it was doing, trying to logically run it though my mind is hard,all i want to do is close one after i have operated on it and open the next one so i dont have a load os screens about, its just tyding up really.
i assume, when a sub or something is called, after execution it goes back to the point from where it was called, and then continues.
is that right
now if i call newscreen, from anywhere i get " expression is not a method"