Reputation: 2832
I have a small doubt:
What is the difference between str
and str_obj
; cls
and cls_obj
.
Are all of them are objects..??
(Got this doubt as they are declared in different syntaxes).
Can someone please elaborate ?
Thanks in advance.
Public Class Form1
Public Structure dayplan_str
Public a As Integer
Public b As String
Public c As Boolean
End Structure
Public Class dayplan_cls
Public a As Integer
Public b As String
Public c As Boolean
End Class
Public str As dayplan_str 'structure
Public cls As dayplan_cls 'class
Dim str_obj As dayplan_str = New dayplan_str 'structure
Dim cls_obj As dayplan_cls = New dayplan_cls 'class
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
str.a = 5
cls.a = 6
str_obj.a = 7
cls_obj.a = 8
End Sub
End Class
Upvotes: 1
Views: 1165
Reputation: 244991
Public str As dayplan_str 'structure
This declares a variable named str
that can hold a reference to an object of type dayplan_str
. dayplan-str
happens to be a structure, but it could also be a class.
The variable str
has not been initialized. It currently does not hold a reference to any particular object. If you try to use the variable str
in an expression, you will get a NullReferenceException
.
Public str_obj As dayplan_str = New dayplan_str 'structure
This declares and initializes a variable named str_obj
.
Like the previous declaration, it also declares a variable that can hold a reference to an object of type dayplan_str
, but the second part of the statement (the part after the equal sign) initializes the variable by calling the dayplan_str
structure's constructor and creating a new object.
If you use the variable str_obj
in an expression, it will work fine (no NullReferenceException
) because it has been initialized to point to a valid dayplan_str
object.
Presumably, this is where the name comes from. The suffix _obj
suggests that str_obj
refers to an actual object, whereas str
does not.
However, you can easily initialize str
so that it does point to a valid object:
str = New dayplan_str
This has the same effect as the second form, it just spreads the declaration and initialization across two lines, instead of keeping it all in a single line.
Generally, you want to initialize variables at the point of their declaration, so the second (combined) form is to be preferred. You would only use this separated form if you had a particular reason that the variable needed to be declared before it could be meaningfully initialized.
There is another difference between the two declarations in your code, though, and that is one of scope. More specifically, it is a difference in the accessibility of the variables.
When you declare a class-level variable as Public
, it is accessible from both inside of the class and outside of the class.
When you declare a class-level variable as Private
, it is only accessible from inside of the class.
Variables in a class can also be declared Friend
, Protected
, and Protected Friend
. Consult the documentation for more details on what those access specifiers mean.
The Dim
keyword is very old, coming into VB.NET from the classical BASIC programming language. It just means "dimension" and is a generic way of declaring a variable. Because of its history, it does not have any intrinsic access specification. The accessibility of a variable declared with Dim
in VB.NET depends on the context in which that variable is declared. When it is declared at class scope, as in your case, its accessibility will be private. So in that sense:
Dim str_obj As dayplan_str = New dayplan_str 'structure
is equivalent to:
Private str_obj As dayplan_str = New dayplan_str 'structure
which is another way that it is different from:
Public str_obj As dayplan_str = New dayplan_str 'structure
Upvotes: 1
Reputation: 7352
Public str As dayplan_str 'structure
This is a declaration of a dayplan_str Object called str
Dim str_obj As dayplan_str = New dayplan_str 'structure
This is a declaration and initialization of a dayplan_str Object called str_obj
Upvotes: 1
Reputation: 43595
The difference between "cls" and "cls_obj" is in the way of declaration. They are all objects, but in "new cls_obj" the new object is also initialized. The difference is like in the following:
Dim k as long
Dim i as long
i = 0
In your case cls_obj is i.
Edit: Concerning the public and dim
- the value declared with dim
is only visible from the module, where it is declared. The value with public
is visible in all modules.
Upvotes: 1