Declare variable for shared procedure inside procedure instead Imports statement - vb.net

I'm using an imports statement to access shared constants and shared procedures just to shorten things a bit.
Imports vList = Helper.Stores.Departments.FruitList
But what I'd really like to do is declare it inside the procedure:
Dim vList as Helper.Stores.Departments.FruitList
Of course I get the warning: Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated. when I try to use it like that.
Is there a way to do something like this inside the procedure without using the imports statement?

As far as I know you can only create an alias of this kind with an import statement as you already done.
The thing that you can do inside a procedure is creating variables using the fully qualified name for the class.
Take a look at the MSDN documentation about Namespaces for more information.

Related

How to add attribute "CallingConvention" to Declare statement when calling dll?

In VB.NET, to import a method from DLL file can be written in:
DllImport statement - new method introduced for .net
Declare statement - old method since VB6
For the first one, the code looks like:
Imports System.Runtime.InteropServices
<DllImport("library_name.dll", EntryPoint:="entry_point", CallingConvention:=CallingConvention.Cdecl)> Public Function method_name(...) AS ...
End Function
And the above code works fine, I just wonder how to convert the code into the older way using Declare keyword as following:
Declare Function method_name Lib "library_name.dll" Alias "entry_point" (...) As ...
To make the question more specific, where can I add the CallingConvention attribute into the Declare statement?
The legavy Declare syntax is limited to stdcall. Use p/invoke.

How to use the variable declared in one module to be used in other module function access vba

Is there any way that we can use variable declared in one module and use that variable in a function defined in other module?
Yes. Declare a variable at module level like this:
Public myvar As String
It can then be accessed from other modules.
Note, however, that global variables like this are almost never a good idea.

Declaration of Variables in Vb 2010

I am creating a kind of a "naive" search engine.
What I have done is distributed words in files where each file corresponds to the starting letter of the words (i.e. if its English, then 26 files are there)
Then when the search system loads I am loading all the words in hash tables (26 hash tables corresponding to 26 alpha-characters) for which I am using an array of structures.
The problem is after declaring the variables in a Form load subroutine, the variables are not able to use those values in other subroutines....
How do I properly declare them so their values persist?
Declare them above all routines but inside the main Class then they will be available elsewhere too. For example:
Public Class yourClassName
' declare your variables here
When you declare variables inside a subroutine, they will be available only inside that subroutine, that's why you need to declare them at class-level for them to be available in other subroutines too.
Fore more info, see the docs (Thanks to #Drise)
http://msdn.microsoft.com/en-us/library/1t0wsc67.aspx

Enforce Type Alias in VB.NET

How might one go about aliasing a type in VB.NET or C# such that when the alias is used as an argument type in a function, accidentally using the not aliased type is an error?
i.e.
Imports AccessSpecifier = System.String
Module Accessors
Delegate Function IoOper(ByRef strm As System.IO.Stream) As Action
Public Function accessMethod(ByRef spec As AccessSpecifier) As IoOper
' implementation
' ...
End Function
End Module
Module Main
Public Sub Main()
Dim spec As AccessSpecifier = New AccessSpecifier(CType("READ_WRITE", Char()))
Dim val = Accessors.accessMethod(spec)
System.Console.WriteLine(val.GetType())
Dim shouldFail = Accessors.accessMethod("FAIL_ME")
System.Console.WriteLine(shouldFail.GetType())
End Sub
End Module
Or perhaps is there a better way to go about this?
Overall, I'm wanting the IDE to force me to know what I'm doing if I'm throwing Ints around to mean Flags, or States and Strings around to mean Names, Propertys and Records.
Help?
I've never liked Type aliasing in .NET. It makes for imprecise code and it is not immediately clear what is happening. As in your example, when an individual went looking for the AccessSpecifier.vb file (or class) they would not find it.
Also, Aliasing only works within YOUR project and only within a single code file. So you would have to define that alias in all the various code files where it was to be used.
A better (as in easier to read, easier to maintain, and more clear of intent) option is to create a class and overload the type conversion operators for automatic conversion to/from String. In this manner you retain your ability to use your new class as if it were a String, but you gain your strict type checking.

Use same name for local variable and function

In VBA, I want to use a name for a local variable that I'd also like to use for a function name. The problem I'd that the function name formatting always changes to the local variable formatting.
Any way to prevent that?
I highly recommend not using the same name for disambiguation purposes. Also, if VBA is not case sensitive, it may not know whether you are referring to the function or the variable and thus give a runtime error (I don't think it is compiled per se, but it goes to a proprietary p-code intermediate.)
Often when you'd like the names to be similar, it can be useful to prepend an underscore to one, such as a local variable. Thus I recommend you name the function FunctionName and the variable _FunctionName if you want to go that route.
If you want to try having the same name for each, you will likely need to edit the code outside of the IDE that is reformatting your code. In an editor that doesn't try to auto-format, you may be able to force it. Then whether it compiles or not is the question.
In Visual Basic, each function already has a variable named after the function. Assigning a value to this variable is the only way to set a return value for this function.
Therefore, declaring yet another variable of the same name creates ambiguity. You cannot do so. It results in the Duplicate declaration in current scope error.
And if by "local" variable you meant a module-level variable, then you cannot do it either: Ambiguous name detected: %s.
And if you are asking about a situation when a function and a variable of the same name belong to different scopes, then VBA will use the case of the line that was edited last. So if you declare a function and then declare a variable in another module, the function name will change case. But if you then return to the function, change its casing back and press Enter, the variable, in its turn, will change its casing to match the function name.