Can anyone explain me in VB i need to use Public Shared Sub so it can be accessed from another form.
But what this "Public" and "Shared" means?
Who is public?
With who is shared?
If it is public shared does this means some other software or "some hacker app" can easier have access to this sub and it's values?
In VB.NET, Shared is equivalent to static in C# - meaning the member belongs to the class, not an instance of it. You might think that this member is 'Shared' among all instances, but this is not technically correct, even though VB.NET will resolve a Shared member though an instance invocation.
public class1
public shared something as string
public somethingelse as string
end class
The following code illustrates how VB.Net allows you to access these:
...
class1.something = "something" 'belongs to the class, no instance needed
dim x as new class1() with {.somethingelse = "something else"}
Console.WriteLine(x.somethingelse) 'prints "something else"
Console.Writeline(class1.something) 'prints "something" <- this is the correct way to access it
Console.Writeline(x.something) 'prints "something" but this is not recommended!
...
Public means any linking assembly can see and use this member.
The Public accessor keyword simply means that the method, property, etc. is visible and callable from outside of the DLL or Assembly that defined it.
The Shared keyword means that the method, etc. is not "instanced". That is, it is part of the Class definition only, and not part of the objects that are created ("instanced") from that Class definition. This has two principal effects:
The Shared method can be called at anytime, without actually having an object/instance of that Class. and,
Shared methods cannot access any of the non-Shared parts of the Class definition (unless an object instance is passed to it). They can only directly access the other Shared parts of the Class definition.
Related
I'm having trouble inheriting a (public) variable, let's say
Public Var As ClassThatIsIndependent
The declaration above generates no trouble for itself, however, if i inherit the class that holds it
Implements BaseClass
I get the error "object module needs to implement variable for interface". I've tried these options (both inside ChildClass)
Public Var As ClassThatIsIndependent
and
Public BaseClass_Var As ClassThatIsIndependent
But none of them solves the problem. Any alternative? I'm open to possible Set/Get solutions, however, i'd prefer to maintain Var as a public variable.
Per the Visual Basic 6.0 Programmer's Guide, Polymorphism, Implementing Properties section:
Suppose we give the Animal class an Age property, by adding a Public variable to the Declarations section:
Option Explicit
Public Age As Double
The Procedure drop downs in the code modules for the Tyrannosaur and Flea classes now contain property procedures for implementing the Age property,
…
Using a public variable to implement a property is strictly a convenience for the programmer. Behind the scenes, Visual Basic implements the property as a pair of property procedures.
You must implement both procedures. The property procedures are easily implemented by storing the value in a private data member, as shown here:
Private mdblAge As Double
Private Property Get Animal_Age() As Double
Animal_Age = mdblAge
End Property
Private Property Let Animal_Age(ByVal RHS As Double)
mdblAge = RHS
End Property
The private data member is an implementation detail, so you have to add it yourself.
That is, the "public interface" is exactly the same whether you use a Public variable or define them with Property Get/Let. And to implement a property in an interface, you can't use the Public variable approach and need to use the Property Get/Let syntax and handle the data storage for it in your own private variables.
Using asp.net/vb.net. Created a solution with 2 projects, "MainProject" and "MyCommonStuff". The 2nd project ("MyCommonStuff") is really a common utilities class, the resulting dll I hope to use for other projects as well.
MyCommonStuff is defined very simply....
Public Class MyCommonStuff Stuff
Public Shared Function GetInfo() as string
:
:
End Function
Public Shared Sub Test
:
:
End Sub
:
End Class
In MainProject I set a reference to this MyCommonStuff project.
I want to access some of the MyCommonStuff methods in my code. But for some reason the methods are not being recognized.
For example, in a button in the MainProject I tried this....
dim m as new MyCommonStuff
x = m.GetInfo()
Intellisense doesn't pick up any of the subs/functions for m. What am I doing wrong? Thanks!
The thing is that you've made your methods static ("Shared" in VB). You need to either remove the Shared keywords:
Public Function GetInfo() As String
':
':
End Function
Public Sub Test()
':
':
End Sub
or keep the Shared keywords and use it like this:
x = MyCommonStuff.GetInfo()
Here is some information about Shared members. Most notably:
Specifies that one or more declared programming elements are
associated with a class or structure at large, and not with a specific
instance of the class or structure.
In other words, if you want to use your methods from an instance of your MyCommonStuff class, e.g. m in m.GetInfo(), you need to leave the Shared keyword off. If, on the other hand, you have a method that is common across all instances of your class or for which you don't even need an instance, you would use the Shared keyword and access the method like I said above, e.g. MyCommonStuff.GetInfo().
Make sure you're importing you common stuff's namespace.
I'm having a problem because I changed my code from vb6 to .net and I cant seem to sort out this issue I am having. Please assist.
The error message I'm getting:
Error 5 Reference to a non-shared member requires an object reference.
This is happening quite a few places in the code. This is my code. The problem is where it says FrmInvItem.Inv.`
Option Strict Off
Option Explicit On
Public Class ClsInv
Public Function RunProcess(ByVal ConnectStr As String, ByRef Parstr As String) As Integer
Dim frmInvIt As frmInvItem
RunProcess = frmInvItem.Inv(ConnectStr, Parstr)
frmInvIt.Close()
End Function
I assume Inv is not shared, but you are calling it without an instance of the class in which it is. So you either have to make it shared or create an instance of frmInvItem:
Dim frmInvIt As New frmInvItem() ' create instance
RunProcess = frmInvIt.Inv(ConnectStr, Parstr) ' use it on this instance
frmInvItem is the class, you can call a method via classname only if the method is shared.
Shared procedures are class methods that are not associated with a
specific instance of a class. For example, the Cos method defined
within the Math class is a shared method. You can call a shared
procedure as a method of an object or directly from the class.
You are running a method called Inv on a the frmInvIt without creating an instance.
You should create an instance to run your method against:
http://msdn.microsoft.com/en-us/library/77s47661.aspx
I am a C# programmer but I have to work with some VB.Net code and I came across a situation where I have two methods on an interface with the same name but different method parameters. When I attempt to implement this interface in a class, VB.Net requires explicitly declaring "Implements MethodName" after the method signature. Since both method names are identical, this is confusing the compiler. Is there a way to get around this sort of problem? I suspect this must be a common occurrence. Any thoughts?
N.B. This was more a case of the programmer not verifying that the interface in question had not changed from underneath him.
How is this confusing the compiler?
The compiler expects to find an implementation for every method signature, and distinguishes the implementations by their signatures.
If the signatures are identical/undistinguishable (in most cases it means that the arguments are of the same types in the same order) you'll get a design-time error related to the interface, saying that the two methods cannot overload eachother as they have the same signature.
So, in any case, the compiler should not be confused.
Should you need further assistance, please attach a code sample - these things are relatively easy to resolve.
Tip: When writing the implementation, as soon as you write down "implements MyInterface" and hit Enter - Visual Studio will create a "skeleton" code of the implementation, which saves you writing the method signatures and correlating them to the interface.
Example code of having two methods with the same name and everythign working well:
Interface MyInterface
Sub MySub(ByVal arg0 As DateTime)
Sub MySub(ByVal arg0 As ULong)
End Interface
Class MyImplementation
Implements MyInterface
Public Sub MySub(ByVal arg0 As Date) Implements MyInterface.MySub
...
End Sub
Public Sub MySub(ByVal arg0 As ULong) Implements MyInterface.MySub
...
End Sub
End Class
You can make the method private and give it another name.
Like:
Private Sub SaveImpl(ByVal someEntity As IEntity) Implements IRepository.Save
this will look to the outside like: someRepository.Save
Here is a VB.NET code snippet
Public Class OOPDemo
Private _strtString as String
Public Function Func(obj as OOPDemo) as boolean
obj._strString = "I can set value to private member using a object"
End Function
End Class
I thought we cannot access the private members using the object, but perhaps CLR allows us to do that. So that means that access modifiers are based on the type and not on the instance of that type. I have also heard that c++ also allows that..
Any guesses what could be the reason for this?
Edit:
I think this line from the msdn link given by RoBorg explains this behaviour
"Code in the type that declares a private element, including code within contained types, can access the element "
Your question is quite confusing but I think I've understood it as:
"Why can I access another instance (of my class)'s private variables?"
And you're right - in all OOP languages I've used you can access private variables from other instances, precisely because access permissions are based on where the code is, rather than to which object instance it 'belongs'.
It might be hard to implement copy constructors or equality operators otherwise.
Here's the section about access levels in MSDN.