class from a class

is it possible to call a class from a class.

i have created two forms

PublicClass MAINVIEW

Dim ANSAsString

PublicSub BTNSEARCH_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles BTNSEARCH.Click

If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OKThen

MsgBox(OpenFileDialog1.FileName)

CONTROL()

EndIf

EndSub

PublicSub BTNQUIT_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles BTNQUIT.Click

If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OKThen

MsgBox(SaveFileDialog1.FileName)

EndIf

EndSub

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

'TODO: This line of code loads data into the 'MYRECSDataSet.SONGFIELDS' table. You can move, or remove it, as needed.

Me.SONGFIELDSTableAdapter.Fill(Me.MYRECSDataSet.SONGFIELDS)

EndSub

EndClass

PublicClass CONTROL

PublicSub add(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnadd.Click

Dim artAsString

art = InputBox(" enter artist name")

End

EndSub

PrivateSub search(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button2.Click

EndSub

PrivateSub quit(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button3.Click

ExitSub

EndSub

EndClass

which automatically create a class to enter code. each class has button events in them. can i, amd how do i call a class procedure from a button event.

i haved tried

control()

but that just tells me its not a method

all the classes are set topublic and that never helped, all my books dont tell you about this, which is the point about having books( i thought).

they are not finished, i need to know waht is going on as i do it.

any help please

[13609 byte] By [chaza] at [2007-12-25]
# 1

Make a complete reference....

Public Class MyClass1

Dim MC2 as New MyClass2

Public Sub Test

MC2.DoTest

end Sub

End CLass

Public Class MyClass2

Public Sub DoTest

MessageBox.Show("Hello World")

End Sub

End Class

This would be the same with calling methods across forms...you just need to make sure you make a complete reference to the correct instance

DMan1 at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2
i dont understand waht your saying, do i have to dim another variable called newclass
chaza at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

To answer you question

Is it possible to call an object from an object ? Yes, of course it is and this is probably what you are trying to do.

Here's some simple code (the fact that Form1 is a form shouldnt matter as its just a class with a Visual Interface). In this code when you click on the button on the instance of Form1 - it will create an Object (which is an instance of Classfoo) and then call the method bar on this object.

So in essence you have called another objects method from an object. Objects are just instances of classes - so in essence you have probably achieved what you try to do.

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As New CLassfoo
x.Bar()
End Sub
End Class


Class Classfoo
Sub Bar()
MsgBox("xyz")
End Sub
End Class

If you dont want to have to instantiate an object to call method bar - then you have two choices - define the method as a shared method which means you can refer to it with having to instantiate an object - or put the method in a module which is really a VB concept but it is conceptually a class with all methods in it being implicitly shared.

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

thank spotty

please remrmber im new to this, so let me recap,

as i understand what you have sent is;

you have a form with one button, in that button sub you have dimensioned a new class or module call classfoo which has in it a sub called bar which has a msgbox which contains "xyz"

so when i call x.bar()

i will get the msgbox(xyz)

if this is right can i call x.bar() anywhere in any of my classes or modules or subs

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

You can call

Dim x As New CLassfoo
x.Bar()

anywhere in you code and it will create an instance and call the method. But this is based upon the accessibility of the variable and the fact that the method is an instance method and therefore will require an object to be instanced to use it.

If you make the method bar a shared method by adding shared keyword as part of the method declaration then you can call using the line

Classfoo.bar

as its shared you dont have to instance an object to use it you can call it using the class name.

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