הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Tuesday, May 31, 2011 11:40 PM
Any source to explain me the difference between them?
I'm new to visual basic, and I need to know the very basic things in Visual Basic allowing me to become a professional User!!
I appreciate any help !
All replies (8)
Tuesday, May 31, 2011 11:59 PM ✅Answered
On 5/31/2011 7:40 PM, CrowdedThoughts wrote:
> Any source to explain me the difference between them?
>
> I'm new to visual basic, and I need to know the very basic things in
> Visual Basic allowing me to become a professional User!!
>
> I appreciate any help !
>
http://visualbasic.about.com/od/usingvbnet/a/blinheritancea.htm
Public means another class can see the public method in the class the
has the public class.
Private means that only the class that the private method was declared
in can see and use the method.
You have a Public class and you have a Private class which holds the
same meaning as above and how the class can be used.
Functions with Public and Private hold the same meaning.
<http://www.developerfusion.com/article/1047/new-objectoriented-capabilities-in-vbnet/2/>
<http://www.developerfusion.com/article/1047/new-objectoriented-capabilities-in-vbnet/5/>
.NET is about this with any .NET language.
<http://en.wikipedia.org/wiki/Object-oriented_programming>
Wednesday, June 1, 2011 12:23 AM ✅Answered | 3 votes
Excellent question and a great reply by darnold! May I make a suggestion though? If you're just getting your feet wet with all this, it can all be quite confusing - we've all been there, so don't feel alone!
I'd suggest that for now, you focus more on "scope" - it matters where you put the statement, and that, at first, is a difficult concept to fully embrace, but it's a good thing that it does matter and in time, you'll see why that is also.
http://msdn.microsoft.com/en-us/library/1t0wsc67.aspx
Have a look at that article and from the link above and there's a link at the bottom for "lifetime". Read that also.
If you're like the rest of us, you'll leave more confused than you started out as! Ha! But that's how it works - don't give up, don't give in, keep going! You'll be amazed at how much you'll know a year from now compared to today.
Try some things - experiment. What's the worst that can happen? Your computer catches fire? Obviously not, so don't be afraid to try it, and read read read and then some. You'll get it - you'll see! :)
Wednesday, June 1, 2011 12:26 AM ✅Answered
Hi darnold. A good selection of links to help explain the concept of scope. As usual with a broad topic like this there are always the outlier scenarios that turn a quick explanation into something that invites correction, and I just want to clarify a few comments you made for the sake of the OP or anyone else who comes across this.
> Public means another class can see the public method in the class the has the public class.
*
*
* *
This is true. More accurately, for a class Public has a scope that allows all classes access including those from another assembly that references the class's assembly. this becomes significant when you consider that Friend (internal) scope is restricted to only classes within the assembly.
* *
> Private means that only the class that the private method was declared in can see and use the method.
Well, not entirely. You can have another nested class within that class that can still access that private member. True, not a common scenario but worth mentioning. Here's an example.
Public Class Foo
Private Hi As String
Private Class Bar
Sub ScopeTest()
Dim f As New Foo
f.Hi = "Gotcha!"
End Sub
End Class
End Class
There's obviously much more to the topic... that I believe your links explain very well.
Wednesday, June 1, 2011 3:25 AM ✅Answered
Hi,
Here is the section on access modifiers:>>
http://msdn.microsoft.com/en-us/library/dd460654.aspx#AccessModifiers
As no one has mentioned the SHARED part then here goes.
The SHARED keyword means that SUB or FUNCTION ( method ) or indeed it can also be applied to a PROPERTY
can be accessed without declaring an instance of the class.
If you create one or more instances of a CLASS then the SHARED item is common i.e. it is said to be SHARED between them.
So if you change something via an instance it also changes in all of the others.
From my experience with code in these forums, SHARED is rarely used, but handy to know about. ;-)
PASTE ALL of this code into your Form1 code window with one Button on your Form to try this please:>>
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim instance1 As New ExampleClass
Dim instance2 As New ExampleClass
'You may get a warning on these two lines as it is accessing a SHARED property
'The code should still run however. :-)
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
MessageBox.Show(instance1.Name, "Instance1 name =")
MessageBox.Show(instance2.Name, "Instance2 name =")
Dim result As Double = 0
'This is showing that a SHARED Function does not require an instance.
'Dim example1 As New ExampleClass would give you an instance accessed via the OBJECT 'example1'
result = ExampleClass.CubeRoot(27)
MessageBox.Show("Cube root of 27 = " & result.ToString)
'As above the SHARED SUB within ExampleClass does not require an instance.
Call ExampleClass.Greeting()
'or simply:>>
ExampleClass.Greeting()
End Sub
End Class
Public Class ExampleClass
Shared Sub Greeting()
MessageBox.Show("Hi there!!")
End Sub
Shared Function CubeRoot(ByVal aDouble As Double) As Double
Return Math.Pow(aDouble, 1 / 3)
End Function
Private Shared mName As String = "ExampleClass"
Public Shared ReadOnly Property Name() As String
Get
Return mName
End Get
End Property
End Class
P.S: I've just remembered something else which can be confusing for new users of VB.NET
but that is not mentioned in this question. It is to do with ReadOnly properties.
Regards, John
Click this link to see how to insert a picture into a forum post.
Wednesday, June 1, 2011 6:33 AM ✅Answered
Hello,
As I see the part of the question about shared is not yet answers.
Shared in VB is another name for a module in VB (with slight differences), in far past a program was always in the same static area in the memory of a computer.
Because everything is now relocatable, that static is already for a long time not anymore like that, but some old program languages still use that word to tell that a piece of code (can also be fields) is constantly in memory.
The classic name for that in VB is module which acts the same.
Members (methods and properties but also fields) which are declared shared are in fact not working in an OOP way were objects are constantly released from memory; but with that classic way of designing a kind of fixed storage and program area.
Of course because they are constantly in memory, they can always be used shared by all methods. In past this was the common way, but this shared goes wrong as it is about re-usability, with the dynamical object way your memory is better managed, which also means that it is not loaded direct completely.
Success
Cor
Wednesday, June 1, 2011 11:47 AM ✅Answered
You have put many questions all in one question. However I’ll try to answer all your questions point by point.
Difference between various scopes:
Public – Accessible from anywhere. This the maximum scope you may give to a class member (method/property/field).
Private – Accessible from inside the class only. This the minimum scope you may give to a class member.
These are the others to complete the list:
Protected – Accessible from inside the class or inherited class.
Friend – Accessible from inside the class or anywhere inside the same project/assembly.
Protected Friend – Accessible from inside the class or inherited class or anywhere inside the same project/assembly.
Public Class Class1
Public PublicVar As Integer 'can be assessed from anywhere.
Public Shared PublicSharedVar As Integer 'can be assessed from anywhere even without class instance
Private PrivateVar As Integer 'can be accessed from within this class only.
Private Shared PrivateSharedVar As Integer 'can be accessed from within this class only even without class instance.
End Class
Difference between Sub and Function:
The only difference between a sub and a function is that Sub can’t return any values, while a function can return a value.
For Example:
Public Class Class1
Public Sub Sub1()
'this doesn't return anything.
End Sub
Public Function Function1() As Integer
' this returns something of type integer
Return 100
End Function
Public Sub Sample()
Sub1()
Dim x As Integer = Function1()
MsgBox(x)
End Sub
End Class
Difference between Shared and Non-Shared:
When you declare a class member as shared, then it doesn’t reside inside the object instance but inside the class itself. This means that all object instances point towards the same member. For properties and fields, If any one of the objects modify the value, the change is visible in all instances.
As such, you will call it by ClassName.MemberName which you can’t do otherwise. This makes perfect sense. However, calling it by ObjectInstance.MemberName isn’t illegal either though you will get a compiler warning if you try to do so.
You don’t need a object instance to get/set shared member value since it resides inside the class. You an access it directly using the ClassName.MemberName notation.
Here is an example:
Public Class SharedMemberDemo
Public Shared MySharedVariable As String = ""
Public MyNonSharedVariable As String = ""
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj1, obj2 As SharedMemberDemo
SharedMemberDemo.MySharedVariable = "some value"
MsgBox(SharedMemberDemo.MySharedVariable)
obj1 = New SharedMemberDemo
obj2 = New SharedMemberDemo
MsgBox(obj1.MySharedVariable)
MsgBox(obj2.MySharedVariable)
End Sub
Here are the things you should notice:
- We didn’t need to create a class instance to access the shared member.
- All objects have same value of the shared member variable. This will be evident from the message boxes that popup.
- You will get a compiler warning at the last two msgbox lines, since calling it this way is not illegal but not something that is recommended or should be done.
Pradeep, Microsoft MVP (Visual Basic)
http://pradeep1210.wordpress.com
Wednesday, June 1, 2011 1:31 PM ✅Answered
Difference between Sub and Function:
The only difference between a sub and a function is that Sub can’t return any values, while a function can return a value.
Hi ALL,
One difference or exception to the above is if you choose to change and return one or more value(s) or object(s) ByRef with a Sub.
:-) ;-) :-D
In other words you can make changes to passed values and objects. :-D
You can of course do the same with a Function as well.
If you pass an array or a LIST you can make those contain more or less results too.
However, strictly speaking, a Sub doesn't return any value(s) or object(s) otherwise, that is you can not assign the Sub
to the right hand side of a value or object reference.
Try the code below and read my code comments with one Button on a Form if you wish please.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim result As Integer = 0
'Assignment to 'result' from the function DoubleIt >>
result = DoubleIt(7)
MessageBox.Show("7 doubled = " & result.ToString)
'
'You can not do this with a SUB:>>
' result = TripleIt(12)
' result = TenTimesTheValue(8)
'
'You can however do this:>>
Dim secondResult As Integer
secondResult = 8
TenTimesTheValue(secondResult)
MessageBox.Show("Ten times 8 = " & secondResult.ToString)
End Sub
Function DoubleIt(ByVal anInteger As Integer) As Integer
Return anInteger * 2
End Function
Sub TripleIt(ByVal anInteger As Integer)
anInteger *= 3
End Sub
Sub TenTimesTheValue(ByRef anInteger As Integer)
anInteger *= 10
End Sub
End Class
Regards, John
Click this link to see how to insert a picture into a forum post.
Wednesday, June 1, 2011 5:53 AM
Hello,
here the section: Variable and Method Scope in Microsoft NET
regards Ellen
Ich benutze/ I'm using VB2008 & VB2010