I have noticed that I cant use the variable percentage in one module and a different variable called percentage in another module how can you do that without a conflict occurring?
Modules add variables to the namespace they appear in. Which is usually the global namespace so a collision between variable names is common. VB.NET provides the Namespace keyword to solve this. You'll have to write it like this:
Namespace Something
Module whatever
Dim Percentage As Double
End Module
End Namespace
And modify the code that uses that variable but is not inside the namespace, if any, by writing Something.Percentage
Related
I have a VB net Class which will use very very large string constants...
I want to define these constants in a Module in a different file as to make the code easier to read and update.
I would like the Module to be Private to everything else except the Class that uses it.
Is this possible?
Thanks
Tony
I've got a situation where I can't change a CLI header that declares several symbols in the global scope, and then those symbols are then used by a function which IS inside a namespace, and that function is exported in an assembly I need to make use of.
Below is a bit of puedo code to help illustrate the Visual Studio setup. Also, I'm using the 2008 toolchain which is pre C++11 making the 'enum class' a managed object. Also note that it also doesn't have the access specifier (I did not write this) which makes it private.
ExportingAssembly.vcxproj
|->SomeHeader.h
| |->enum class SomeSymbol
|->SomeSource.c
|->SomeNamespace
|->void SomeFunction(SomeSymbol arg)
ImportingAssembly.vcxproj
|->Ref ExportingAssembly
|->ImportingSource.c
|->Using SomeNamespace;
|->void MyFu(){ SomeSumbol a = 0; SomeFunction(a); }
The problem is that those global symbols are obviously not visible to the importing assembly and if I include the header for those symbols it results in an incompatibility between my assembly and the one I'm importing
error C2664: 'ExportAssemblyNamespace::SomeFunction(SomeSymbol arg)': can't
convert parameter 1 from 'SomeSymbol" to 'SomeSymbol'
Now I CAN change the exported assembly (the project file and/or the source file) so I just hope I can somehow make those global symbols part of the exported assemblies' namespace so the importing assembly will see them. Or failing that, somehow locate the global symbols in the exported assembly (I can see the global symbols are in the PE MetaData Tables).
Any ideas. Or is a symbol without an access specifier private and impossible to make use of via referencing the assembly?
Or is a symbol without an access specifier private and impossible to make use of via referencing the assembly?
Yes, a symbol without an access specifier is private. It's not quite "impossible" to make use of; the only way is via reflection.
The fact that you have some things in a namespace, and some not, doesn't matter here. But you should have your function as part of a class, since global functions aren't part of the CLR. (C++/CLI creates a class to hold the global functions, but it's not a public class, and I think it has an invalid name anyway.)
Add the keyword public to your enum, and enclose your function in a public ref class.
As it says on msdn:
Both classes and modules are reference types that encapsulate the items defined within, but they differ in how items are accessed from other procedures.
How is it possible to use a Module inside a Class? How would I access its members and use them?
EDIT #1
I tried to access this module in all possible ways,
Dim memman as MemoryModule
but it gives me an error, Module 'MemoryModule' cannot be used as a type.
From your comment on the question...
Dim memman as MemoryModule
This is incorrect. Modules aren't classes, they can't be instantiated as objects. You can essentially think of a module as being a collection of Shared helper functions. And you'd access those like any other Shared function:
MemoryModule.SomeFunction()
So, for example, if your module looks like this:
Module MemoryModule
Sub PerformAnOperation()
' some function logic
End Sub
End Module
Then any class which can see that module can invoke that function:
MemoryModule.PerformAnOperation()
If logically your "memory module" should be an object capable of separate instances, then it shouldn't be a Module. Instead, you'd want to make it a Class and implement it with instance members instead of Shared members. It's important to structure your code according to the logic and concepts it represents.
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.
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
I am following this tutorial
and like many other tutorials, it is very vague as to where to insert code. Which vb.net file would I insert the host class to begin adding the using statement?
I have a file called crawler.aspx...this accepts html and the like. I also have crawler.aspx.vb...tried adding the code there and received an error saying that the statement could not be used outside of the method/body lambda.
The code example is in C#. The equivalent VB.NET directive is Imports. It tells the compiler that you will be using classes and methods and such from the specified assembly.
As long as you have a reference to the assemblies in your project (which if you installed this via NuGet you should), you can simply add the following lines to the top of your file containing the code of the host class, like this:
Imports Abot.Crawler
Imports Abot.Poco
This should not be confused with the Using statement, which is something different entirely.