Answered by:
Problem for Using Class in VB.NET

Question
-
I tried to create a class called MyAppClass.vb , but when i try to use one of the class functions , variables or subs , i see an error.
the code is :
' Class codes : Public Class MyAppClass Public A as integer = 100 End Class ' Main Application code : Public Class MyApp Public Sub Button1_click(sender As Object, e As EventArgs) han_ _dles Button1.Click msgbox(A) End Sub End Class
(All the functions , subs , variables and the class are Public.)
- Edited by Arshia Aghaei Wednesday, November 12, 2014 8:45 PM
Wednesday, November 12, 2014 8:38 PM
Answers
-
Arshia,
As Acamar pointed out, you have the class set up with instance members so you can only operate with a given instance of it. Another way to do it though, would be to set up with shared members:
Public NotInheritable Class MyAppClass Private Shared _a As Integer = 100 Private Sub New() End Sub Public ReadOnly Property A() As Integer Get Return _a End Get End Property Public Shared Function GetA() As Integer Return _a End Function End Class
Still lost in code, just at a little higher level.
:-)- Marked as answer by Arshia Aghaei Thursday, November 13, 2014 8:20 AM
Wednesday, November 12, 2014 9:34 PM
All replies
-
I tried to create a class called MyAppClass.vb , but when i try to use one of the class functions , variables or subs , i see this error :
You have to create an instance of the class before you can refer to any of its members (functions, subs or properties). For example:
Dim MAC As New MyAppClass
Dim Y As Integer = MAC.Calculate(X)Or, it might be that your class should have been a module. See:
http://msdn.microsoft.com/en-us/library/7825002w(v=vs.90).aspx
- Proposed as answer by David M. Nichols Wednesday, November 12, 2014 9:09 PM
Wednesday, November 12, 2014 8:45 PM -
Arshia,
As Acamar pointed out, you have the class set up with instance members so you can only operate with a given instance of it. Another way to do it though, would be to set up with shared members:
Public NotInheritable Class MyAppClass Private Shared _a As Integer = 100 Private Sub New() End Sub Public ReadOnly Property A() As Integer Get Return _a End Get End Property Public Shared Function GetA() As Integer Return _a End Function End Class
Still lost in code, just at a little higher level.
:-)- Marked as answer by Arshia Aghaei Thursday, November 13, 2014 8:20 AM
Wednesday, November 12, 2014 9:34 PM -
For that matter, you don't need even that. Typed manually but something like this would be fine:
Public Shared ReadOnly A As Integer = 100
Still lost in code, just at a little higher level.
:-)Wednesday, November 12, 2014 9:46 PM -
https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/
It doran't matter if it's Java or .NET, becuase both development platforms are Object Oriened Platforms.
1) class
2) object
3) instance of an object with memory allocated for it -- the object lives.
Wednesday, November 12, 2014 9:49 PM -
As Acamar said, you need to create an instance.
Seeing your code I wonder how much experience you have coding in VB.NET. If not much then welcome to VB.NET! We've all been there. I hope you find the following useful.
One reason to use a class is to separate and organize your routines. The class gets separated from the app's routines which means that the class should not use any resources that are designated to the main program. This allows you to take the class and pop it into another project without getting lots of errors because you are using undeclared variables in the class.
A class also allows you to encapsulate data by using properties, in such a way that you do not access the actual class variable directly, but rather go through a property. Your code:
' Class codes : Public Class MyAppClass Public A as integer = 100 End Class
Technically this is correct, but notice how you are accessing the 'A' variable directly. If your class is going to be this simple then it might be better to just use a module. The better way is to use properties.
I created a new form project and added a button. Plus I added your class after the main class. The code:
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim cMyAppClass As New MyAppClass MessageBox.Show(cMyAppClass.A) End Sub End Class ' Class codes : Public Class MyAppClass Public A As Integer = 100 End Class
This will work. Now suppose that A represents an angle and you want to set it to angles between 0 and 360; however, you notice that in some cases you have negative angles (I am making this stuff up) that you should convert to a positive number. You can do something like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim cMyAppClass As New MyAppClass Dim AngleX As Integer AngleX = -45 'Ooops, negative angle! cMyAppClass.A = Math.Abs(AngleX) MessageBox.Show(cMyAppClass.A) End Sub
But perhaps you want the class to take care of it, or the logic is more complicated. One solution is to add a property to the class where you can fine tune the value being stored in your class variable. The new code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim cMyAppClass As New MyAppClass Dim AngleX As Integer AngleX = -45 'Ooops, negative angle! cMyAppClass.Angle = AngleX MessageBox.Show(cMyAppClass.Angle) End Sub End Class ' Class codes : Public Class MyAppClass Private A As Integer = 100 Property Angle() Get Angle = Math.Abs(A) End Get Set(ByVal value) A = value End Set End Property End Class
First, changes to your class:
- I made the A variable Private. Since I will be accessing A through Angle. In fact, I do not want the caller directly modifying the class' A variable.
- I added the Angle property that I want the caller to use to access the A variable. In addition, the property allows me to take the absolute value of whatever the caller passes, in case it is a negative angle.
And changes to the caller procedure:
- I no longer access A directly. I use the Angle property
- I removed the Math.Abs function call. This is now the class' responsibility
Use properties, they'll be handy and are the recommended way to access class variable.
Also, a bit off topic. I noticed that you used Msgbox. Try to avoid using function and code that comes from VB6. I just through debugging a problem in one of my apps where I inadvertently left in a VB6 function and it was returning an invalid result. The fix was as easy as using the VB.NET equivalent. Good luck! Saga
Insanity is the prelude to discovery
- Edited by SagaV9 Wednesday, November 12, 2014 10:05 PM
Wednesday, November 12, 2014 10:01 PM -
I tried to create a class called MyAppClass.vb , but when i try to use one of the class functions , variables or subs , i see this error :
You have to create an instance of the class before you can refer to any of its members (functions, subs or properties). For example:
Dim MAC As New MyAppClass
Dim Y As Integer = MAC.Calculate(X)Or, it might be that your class should have been a module. See:
http://msdn.microsoft.com/en-us/library/7825002w(v=vs.90).aspx
Can you explain better ?
Thursday, November 13, 2014 8:18 AM -
Can you explain better ?
A class is a deifition or template - nothing actually exists until you create an instance of that class. So attempting to reference a member of that class before you have instantiated it will create the error you are seeing. You instantitate it using the New keyword, and you assign the new object you create to a variable so that you can refer to it.
Dim MAC As New MyAppClass
Once you have an instance that you can refer to using the variable, the methods are accessible using the dot format - variable.methodname. So to execute the Calculate function, passing a value X and getting the result into the variable Y you would use
Dim Y As Integer = MAC.Calculate(X)
That's the format you would use if your class was designed to represent a specific case of that object - a particular instance with its own property values created for a particular purpose.
If you are creating that class so that you can establish some variables that exist as a single value accessible from anywhere in your code, or generic methods that you can call from any part of your code, then you are talking shared properties and methods, and that's what a module does for you.
See: http://msdn.microsoft.com/en-us/library/7825002w(v=vs.90).aspxThursday, November 13, 2014 9:27 AM