9

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?

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
osdTech
  • 147
  • 1
  • 2
  • 10
  • @pst dear, i don't understand Microsoft bunch of text. I would prefer someone experienced professional to tell me yes or no reather than waste time on reading Microsoft unorganized bible to find out answer after waisting too much time. And i am sure this would help others in future 2. After all isn't that what this website is about? Thank you! – osdTech Oct 20 '12 at 19:05

2 Answers2

9

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.

dwerner
  • 6,462
  • 4
  • 30
  • 44
  • so basically if you... dllinject os something similar to my software you could use my public shared function and find out what it returns. right? – osdTech Oct 20 '12 at 19:06
  • Not familiar with dllinject, but there's no built-in security there if the member is public. Shared doesn't really affect the access level here, it's a bit of a VB.NET misnomer I believe. – dwerner Oct 20 '12 at 19:12
  • Private methods are still accessible to Hackers, etc. – RBarryYoung Oct 20 '12 at 19:17
4

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:

  1. The Shared method can be called at anytime, without actually having an object/instance of that Class. and,
  2. 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.
RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
  • When it comes to hacking, cracking the software which is safer? private or public? and how can i make the sub or function private inside of a class but still access it from my form? thank you – osdTech Oct 20 '12 at 19:09
  • In .Net, hacking/cracking and **Public** have nothing to do with each other. Its a nonsense question. – RBarryYoung Oct 20 '12 at 19:10
  • Depends what you're trying to do. If you are trying to access functionality in an assembly, and that code is not intended to be used externally, the developers should not have left the assembly wide open. – dwerner Oct 20 '12 at 19:14
  • 1
    Doesn't matter to a Hacker in .Net. Public/Private only controls *legitimate* access. Illegitimate access can bypass it easily because of JIT and the IL, etc. See the Reflector tool for more info: http://www.reflector.net/ – RBarryYoung Oct 20 '12 at 19:17
  • assembly will be obfuscated at the end. but i just want to know if private is safer than public – osdTech Oct 20 '12 at 19:17
  • 1
    Only if you consider obfuscated to be safer. Most folks do not, and obfuscation is mostly about preventing effective disassembly/code scraping. – RBarryYoung Oct 20 '12 at 19:19