TheSwamp
Code Red => .NET => Topic started by: Matt__W on November 10, 2011, 02:22:50 PM
-
How do you call a procedure from another class in VB.Net? In VB/VBA I would create a PUBLIC SUB and could call it from anywhere in the project. Do you typically create separate classes to keep similar commands grouped together? I'm trying to recreate an old AutoCAD VBA app that shows a dialog box, allows the user to select multiple drawing files then counts block attributes via DBX. I've got the dialog box part... the multi-selection of DWG files... just not sure how to call a command from another class (or would it make sense to put EVERYTHING in the form).
-
Take a look at this article :
http://drive-cad-with-code.blogspot.com/2010/09/command-method-static-or-not-static.html
it may helps
~'J'~
-
I use Class.Proceedure ()
Public Class A
Public Sub A1
End Sub
Public Sub A2
End Sub
End Class
Public Class B
Public Sub B1
A.A1()
A.A2()
End Sub
Public Sub B2
End Sub
End Class
-
Here is a sample of how I do it:
Public Class Rectangle
Dim COUNT As Counter ' Counter is a Class
Private Sub Rectangle_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
COUNT = New Counter() ' On form load populate known data
COUNT.getCount() 'call to sub getCount in Counter Class
End Sub
End Class
-
A simple quick overview
When a member is private it can be called by other members in the same class.
When a member is public it can be called anywhere a instance of the class is visible
also there is protected which is like private but allows derived classes to call it, and sealed etc......
When you call a member of a class you call it through a object of that class
So you have
Public Class MattClass
Sub New()
Console.WriteLine("Created")
End Sub
Public Sub MattInstanceMethod()
Console.WriteLine("Instance")
End Sub
Public Shared Sub MattSharedMethod()
Console.WriteLine("Shared")
End Sub
End Class
So
Dim mclass as New MattClass
Assigns mclass as a new instance of MattClass and calls its constructor which in VB is the Sub New
Which would print 'created' because of the constructor
Now you can call it through the object
mclass.MattInstanceMethod()
Also there are Shared members which are called from the class level.
MattClass.MattSharedMethod()
A shared method does not belong to any instance of that class.
To create a counter of how many instances of a class is created a shared field could be incremented in its constructor.
Sub Main()
MattClass.MattSharedMethod()
Dim mclass As New MattClass()
mclass.MattInstanceMethod()
Console.ReadKey()
End Sub
....
.....
....
From a form perspective all logic for counting attributes should be kept out of it.
To reuse that would mean creating a new instance of the form and calling the method.
For a basic and simple example
I am not familiar with DBX but you could create class that takes filenames and returns the number of atts using the DBX logic
Public Class DBXAttCounter
Public Function NumberOfAtts(ByVal fileName As String) As Integer
''''logic for getting atts
End Function
End Class
then from the form you have something like
Dim dbxcounter As New DBXAttCounter()
Dim i As Integer = dbxcounter.NumberOfAtts("FileName")
That way all complexity is handle by the class and any changed is handled there, so it will not affect any form, class that calls it.
-
If your code is inside a namespace you would have to call the namespace also? such as:
namespace.class.procedure?
-
I'm finally getting back to this. (let's hope I can get this done before the new year)
Thanks for the info everyone!!