Question calling a method in Class2 from Class1?

mahmood

Member
Joined
Feb 5, 2007
Messages
20
Programming Experience
Beginner
Hi guys

Lets say I have a application with these three items:

A startup form called form1
A class lets say Class1
A class lets say Class2

How can I access a method in class2 from whitin class1?

For example in Class2 I have a method call AddToDatabase(sql as string) and in a method is Class1 I have a SQL statement that I want to implement.

Solution that didn't work:
  1. Initializing an instance of Class2 in form1 called myClass2 and trying to reach it using: form1.myClass2
  2. Initializing an instance of Class2 in New subrotine of Class1 and trying to call it through: myClass2
 
When you make the instance you call the sub like this:
VB.NET:
Dim cls As New Class1
cls.AddToDatabase(<sql>)
Also the sub has to public.

If the sub is declared Shared then you can call it like:
VB.NET:
Class1.AddToDatabase(<sql>)
 
If declaring the method as Shared solved it then it is no different that creating a new instance to call the instance method. There is nothing wrong in declaring shared utility methods though, when they are instance independent.
When the problem is interacting with a particular instance of a class you just have to provide access to the reference to that instance, this can be done through a property or a method parameter.
 
I have a question relating to the use of public shared subs and functions.

I am creating a user control for a third party product, using that product's API. I have designed the interface for the user control and I have coded the subs and functions that are directly related to the user control.

I have also created a series of classes that will allow the user control to perform various tasks, and I used public shared subs and functions in those classes. The reason I went this route is because when I had the code in just the one class for the user control, the code itself became very large and hard to manage.

So in your opinion, is separating the code using public shared subs and functions the correct way to go to help better manage your code?
 
If you managed to separate code logic then good for you :) What I meant by instance dependency can be illustrated by this:
VB.NET:
Class Utils
Shared Sub CalcA(instance As ClassX)
Shared Sub CalcB(instance As ClassX)
VB.NET:
Dim x As New ClassX
Utils.CalcA(x)
Utils.CalcB(x)
In this case it would be better to include these methods in the ClassX:
VB.NET:
ClassX
Sub CalcA()
Sub CalcB()
VB.NET:
Dim x As New ClassX
x.CalcA()
x.CalcB()
Other ways to organize code for a single class is to use #Region and Partial class files.

Shared members may also logically belong to specific classes, if you for example look to the VB data types they all have a shared Parse method. Integer.Parse(String), Boolean.Parse(String). If these were collected in something like a Utils class either there would have to be different names, ParseInteger/ParseBoolean, or added a parameter to set target type, ie Utils.Parse(String, Type).

Then you have Extension methods that extend specific types as instance methods, but is declared shared outside the type definition. Kind of a special case, that one.
 
Other ways to organize code for a single class is to use #Region and Partial class files.

Thanks JohnH!

Until you had mentioned it, I had not even considered using partial classes. You can extend the class of the WinForm or User Control because the designer code and programmer's code are themselves partial right?

If that is true and you add another partial class to the form or user control, then the subs and functions that you can write can be private instead of public shared, true?
 
You can extend the class of the WinForm or User Control because the designer code and programmer's code are themselves partial right?
That is true.
If that is true and you add another partial class to the form or user control, then the subs and functions that you can write can be private instead of public shared, true?
Dividing a class up in several parts makes no difference to what you can do when writing a class, it is still just a single class. Partial (Visual Basic)
 
Back
Top