coding help

PublicClass Form1

Dim ansAsString

PublicSub Form1_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load

'whatever is in here will be executed when you start the program'

EndSub

PublicSub 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")

EndSub

PublicSub Button2_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button2.Click

EndSub

EndClass

PublicModule test

Dim ansAsString

msgbox(" hello ")

' this gives me declaration expected'

EndModule

the 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]
# 1

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 :-)

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

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

chaza at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3
what exactly are you having trouble understanding? Can you explain bit by bit/line by line?
ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4

as i said ahmed, i create these to test me.the next one i have is this.

Public Class Form1

Dim ans As String

Public 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 Sub

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()

ans = InputBox(" thats it")

End Sub

Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

End Sub

End Class

Public Module test

Dim ans As String

Public Sub sayit()

MsgBox(" hello ")' this all works great'

Form2()' this does not'

End Sub

End Module

Public 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.

chaza at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5

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

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6
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.
chaza at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 7

you would have to declare it in the other modules and classes also, unless you pass a reference to it. Here is a link which shows how to pass objects to other classes (modules will be the same too)

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=760400&SiteID=1

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=700821&SiteID=1

Passing a reference to that object means you can use the SAME object, meaning it has its state as is instead of making a new instance of the object in question, like we did with the form here

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 8

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

chaza at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 9

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).

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 10
can you explain what the error message was telling me please
chaza at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 11
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?
ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 12

Public Class Firstform

Dim ans As String

Dim newscreen As New newform

Public 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 Sub

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()

newscreen.Show()

End Sub

Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnquit.Click

Me.Hide()

MsgBox(" thats it")

End

End Sub

End Class

Public Class newform

Public 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 Sub

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label1.Click

End Sub

Private 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 Sub

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.Close()

Firstform.Show()

End Sub

End Class

Public Module test

Dim 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 newform

Public Sub sayit()

MsgBox(" this is from your module ")

End Sub

End Module

the 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

chaza at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 13

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)

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 14

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"

chaza at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...