Calling other project from onclick event

pettrer

Well-known member
Joined
Sep 5, 2008
Messages
92
Programming Experience
10+
Hi all,

I have a question regarding VB.Net and Object-oriented programming.

I have a solution consisting of two projects.

One project is dedicated to the logic of the application, and one is only to show the Winforms form, and to reflect changes made in the logics project. In other words, when an array filled with various attributes residing in the logics project gets changed, then these changes should be reflected on the form (not that strange in theory).

Here comes the - for me - really tricky part:

I have created user controls, and these are residing in the forms project and placed on the aplication's form. When I click on any one of them, their respective onclick sub gets called okey, but how can I send a message to the logics project from their onclick subs? Basically, I'm not sure on what should be private or not, or possibly how to call the other project correctly. In any case, I can't get it to work.

To sum up: What I need is a line of code calling a class in another project, and someone telling me what should be private, static etc.

Thanks a lot in advance!

P
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+
There isn't really any difference between classes in your assembly and in other assemblies. If the assembly is referenced you can use it, which is exactly what you are doing with the .Net Framework class library assemblies. To create an instance you use the New keyword and specify the type (eg class), the type must be qualified by namespace if the namespace has not been imported. Take for example the Button framework class, it belongs to System.Windows.Forms namespace. If you haven't imported that namespace you create an instance like this: New System.Windows.Forms.Button()
Read: Namespaces in Visual Basic
The default namespace is your own project has same name as the project name.

Here you can read about Access Levels in Visual Basic, Public/Private is the simplest levels which determine if something can or can not be accessed between assemblies. The baseline is if you have something that needs to be accessed from one assembly to another is must be Public, if not protect it from usage with Private level.

Shared, as opposed to instance, means for example a method can be called without depending on anything declared in the class outside the method. A shared method is not called using an instance of a class, but instead qualified only by type. Read about it here: Shared Members in Visual Basic
 

pettrer

Well-known member
Joined
Sep 5, 2008
Messages
92
Programming Experience
10+
Thanks a lot. The problem now is that I can't get this to work both ways (I get circular references).

I have a solution called AccessingMethodsBetweenProjects.

In this solution I have two projects:
AccessingMethodsBetweenProjects (unfortunately, but anyway this is where the form and graphics are) and Logic (this is where the logic is going to be).

I started out doing Add reference and added Logic to AccessingMethodsBetweenProjects, and so I got access to the Logic classes and methods.

So this is my code for the form in AccessingMethodsBetweenProjects, having two usercontrols, and when I click one of those, the other project's method gets called correctly):

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports Logic

Namespace AccessingMethodsBetweenProjects
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub

Public Sub ruta1_Click(sender As Object, e As EventArgs)
Dim lk As New logikklassen()
lk.minmetod(1)
End Sub

Private Sub ruta2_Click(sender As Object, e As EventArgs)
label1.Text = "jkl" 'works
Dim lk As New logikklassen()
lk.minmetod(2)
End Sub
End Class
End Namespace


However I don't know how to set the form's label1 text from the other project:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text


Namespace Logic
Public Class logikklassen
Public Sub minmetod(nr As Integer)
Dim okey As String = "yes!"
okey += nr
'AccessingMethodsBetweenProjects.Form1.label1.Text = okey DOES NOT WORK
End Sub
End Class
End Namespace

So how would i accomplish this task as well?

Thanks a lot!

P
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,583
Location
Norway
Programming Experience
10+
You can't have circular references, so you have to arrange your stuff in a provider-consumer fashion. Logically if A needs B to do its job, and B needs A to do its job, then neither can do their job. Typically libraries provide functionality and often return results of that. That is also the school of thought in OOP, avoid dependencies. For example you should avoid what you do in logikklassen, where you hard code setting a specific label text to some result. Instead typically you return results that a consumer can use (obvious choice for 'minmetod'), or if something needs to be set by method provide that as input parameter (using a type not defined by consumer).
 
Top Bottom