Author Topic: [VB.Net] - Calling a procedure from another class  (Read 25256 times)

0 Members and 1 Guest are viewing this topic.

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
[VB.Net] - Calling a procedure from another class
« 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).
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

fixo

  • Guest
Re: [VB.Net] - Calling a procedure from another class
« Reply #1 on: November 10, 2011, 02:44:38 PM »

DogBone

  • Guest
Re: [VB.Net] - Calling a procedure from another class
« Reply #2 on: November 10, 2011, 02:51:15 PM »
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


RMS

  • Guest
Re: [VB.Net] - Calling a procedure from another class
« Reply #3 on: November 10, 2011, 03:14:00 PM »
Here is a sample of how I do it:

Code: [Select]
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


Jeff H

  • Needs a day job
  • Posts: 6144
Re: [VB.Net] - Calling a procedure from another class
« Reply #4 on: November 10, 2011, 03:41:12 PM »
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
 
Code: [Select]
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
 
Code: [Select]

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
 
Code: [Select]
mclass.MattInstanceMethod()

Also there are Shared members which are called from the class level.
 
Code: [Select]
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.
 
Code: [Select]
    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
 
Code: [Select]
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
 
Code: [Select]
        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.
 

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: [VB.Net] - Calling a procedure from another class
« Reply #5 on: November 10, 2011, 08:04:21 PM »
If your code is inside a namespace you would have to call the namespace also?  such as:

namespace.class.procedure?
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: [VB.Net] - Calling a procedure from another class
« Reply #6 on: December 08, 2011, 10:28:29 AM »
I'm finally getting back to this.  (let's hope I can get this done before the new year)


Thanks for the info everyone!!
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io