Question [Public Sub / Public Function] equals [Sub / Function] declaring a function or sub?

poncianux

Member
Joined
Jun 12, 2011
Messages
5
Location
Coyoacán, Distrito Federal, Mexico, Mexico
Programming Experience
1-3
Like the title, I want to know what implies to declare a function or a sub using public or directly Sub / Function i.e.:

Public Function Whatever()
' Process
End Function

versus.

Function Whatever()
' Process
End Function

Thanks.!
 

Flippedbeyond

Well-known member
Joined
Dec 19, 2008
Messages
198
Programming Experience
1-3
Public Function Whatever() 'explicitly

versus.

Function Whatever() 'implicitly, public is the default

Access Levels in Visual Basic
Public (Visual Basic)

from link above "Public access is the normal level for a programming element when you do not need to limit access to it. Note that the access level of an element declared within an interface, module, class, or structure defaults to Public if you do not declare it otherwise."
 

poncianux

Member
Joined
Jun 12, 2011
Messages
5
Location
Coyoacán, Distrito Federal, Mexico, Mexico
Programming Experience
1-3
Thanks for the answer.

Now, I know that, the thing I want to know if it has a real affectation to the program, I mean in which case it's better to use explicit or implicit, I think both are simply equals and you can use them both anyway, but the point it's to think about memory usage, the application size, the response speed, polymorphism (does explicit and implicit are both able to use polymorphism?), etc.

I know this question could be kinda tedious or kiddish, but I must know this to optimize every code I do and I did.
Thanks.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,412
Location
Norway
Programming Experience
10+
You should set access levels to your elements explicitly because it more readable, it is very time consuming to read code and try to remember all the time what the default access level is. Default access level mean that all elements will get one access level or another by the compiler if you don't write it, which access level that is depends on what kind of element it is and in which context it is used.
 
Top Bottom