1

So, I'm writing a small program in VB.net and I was wandering if there's a replacement for the include statement I'm used to in C/C++? That's instead of making all my classes and functions "public shared".

Are their security concerns, or is it bad practice to keep declaring all the classes or functions I wish to use in other .vb files as public shared?

For example let's say I write a hashing function for passwords within passwords.vb and I want to use it within a UI file login.vb, to do this I would declare my hashing function as a public shared funtion within a public class (unless I'm missing another way). That way I can access it by using something along the lines of password.Hash(STRING) where password. is a reference to my password.vb file.

Now in C/C++ I would add an include statement to the header file that would define everything I needed it too. I know header files don't exist but there's not other way to include a separate .vb file, other than making everything public? It just seems odd to me and it feels like having possibly hundreds of public classes/variables/functions etc... is somehow the wrong way to do it?

I'm aware of .dll referencing as an alternative, but I don't really want to have 20 .dll files associated with a program if I can help it, or unless it should be like that?

Thanks for your help and I hope that what I'm asking makes sense.

Samuel Nicholson
  • 3,587
  • 3
  • 20
  • 37
  • I don't understand, don't "include" file only has the body of the class? It how nothing to do with naming. It's the same here, use namespace/class/static (or in vb shared). – the_lotus May 25 '15 at 13:45

1 Answers1

2

Well you need to do some extra effort if you wish this separation.

Namespaces are not really enforced inside VB.net, but you could use them to arrange your code in such a way that you can use the Imports Namespace statement.

Lets say we have this dummy class, this would now be publicly accessible in your code without any extra effort

Module PasswordHandler
    Public Function Hash(ByVal password As String) As String
        Return password
    End Function
End Module

However, you can change the namespace of the Module if you want to

Namespace Security

    Module PasswordHandler
        Public Function Hash(ByVal password As String) As String
            Return password
        End Function
    End Module

End Namespace

And this would now "hide" the Module from your program, until you use the Imports ProgramName.Security statement on the top of your code, as the following:

Imports IncludeSampleVB.Security

Module Module1

    Sub Main()
        Dim salt As String = PasswordHandler.Hash("dummy")
    End Sub

End Module

This way you could structure your code in a way that might seem more familiar to you :) Btw, for static functions, you can also simply use Modules, they don't have to be shared classes, as you could see here: Classes vs. Modules in VB.NET

Community
  • 1
  • 1
Icepickle
  • 12,689
  • 3
  • 34
  • 48