I've created my own private NuGet server and hosted two packages written in VB, one with a single public class and one with a Module containing some extension methods. When I reference the packages in my application, I am able to create a new instance of the class from the package, but I am unable to use any methods declared in the module. I know that modules need to be contained withing the namespace, so I have a feeling I may need to reference it somewhere to make use of it. Does anyone know what I need to do? Thanks.
I've currently got the following:
Namespace TestHelperNamespace
Public Class TestHelper
Public Sub DoSomething()
End Sub
End Class
Public Module TestModule
Public Sub StringSub(s As String)
End Sub
End Module
End Namespace
Import the Module's namespace in your code
Imports NugetModuleNamespace
Here's a MCVE
Imports Namespace2
Namespace Namespace1
Module Module1
Private Sub foo()
Dim a = 1.23#
Dim b = a.Square() ' doesn't work without Imports
End Sub
End Module
End Namespace
Namespace Namespace2
Module Module2
<System.Runtime.CompilerServices.Extension>
Public Function Square(value As Double) As Double
Return value ^ 2
End Function
End Module
End Namespace
This applies to Modules in separate files as well.
Related
I have a pretty big VB.net code and I am trying to split it into different files. I want to create an external file containing different functions. I have read about partial class files but it is not working for me. Is there any option to call/import a vb.net file and do something as per below example?
Example
Form1.vb
' Imports Functions.vb (How can I call the file containing the class?)
Public Class Form1
Dim a,b,y As Double
Dim calculate As New MyFunctions
a=1
b=1
y=calculate.sum(a,b)
End Class
Functions.vb
Partial Class MyFunctions
Public Function sum(a As Double, b As Double) As Double
return a+b
End Function
End Class
If you want all the functions to be available to all your code, just create a module with the functions in them.
If you just want to split your form1 class into separate files, your form1 file should contain the class definition ..
Partial Public Class Form1
To create a new file for the bits you want to separate off, create a new class file and change the default definition to the above.
Please note that you might need to add Imports lines for each files as well.
In and old project of mine, I had a single form program but had the code split into several files such as ExcelFileHandling.vb, EmailHandling.vb etc. They were all actually partial definitions of Form1. Easy peasy :-)
You use the Imports statement when you don't want to fully qualify a namespace of a class. If the other class is in the same namespace as the class that is referencing it, there is no need to use Imports.
Note, that your example code has functionality that should exist in a method instead of in the body of the class.
'RootNamespace = Right click on project file and choose properties. You'll see it defined there.
Imports RootNamespace.SomeOtherNamespace
Namespace SomeNamespace
Public Class Form 1
Public Sub SomeMethod()
Dim objMyFunctions As New MyFunctions()
'If no Imports is used: As New SomeOtherNamespace.MyFunctions()
End Sub
End Class
End Namespace
Namespace SomeOtherNamespace
Public Class MyFunctions
End Class
End Namespace
Example if both classes are in the same Namespace:
Public Class MyFunctions
Public Sub SomeMethod()
'No need for Imports because they are in the same Namespace.
Dim objMyFunctions As New MyFunctions()
End Sub
End Class
Public Class MyFunctions
End Class
I'm not sure whether that's the best title but what I'm trying to do is set certain Subs and Functions to be only accessible from other functions by qualifying the exact location.
For example, I have a module called modShared.
In this module there is a function called LogForm which returns a Form.
In other areas of my code I have functions where the name begins with Log so I want the LogForm function to only be accessible and only appear on Intellisense when I type in modShared.LogForm and not just LogForm.
Is this possible because it would help me immensely?
Thanks
Just change your module to a static class and apply the Shared modifier to every method and you should be good to go.
Public NotInheritable Class modShared
Private Sub New() 'Prevent initialization.
End Sub
Public Shared Function LogForm() As Form
'Do stuff...
End Function
End Class
One way of doing this is placing your modules inside a Namespace
Namespace UtilityMethods
Module modShared
Public sub LogForm()
'Code Here
End sub
End Module
End Namespace
And you could use this code by either calling:
UtilityMethods.LogForm
or
Namespace UtilityMethods
Public Class MyClass
Public sub ClassMethod
LogForm()
end sub
End Class
End Namespace
How can I access public functions from a user control the same way I can access public functions from a form? The public functions are in a utility module.
Let's assume that you have a module named SpecialUtility which is in SomeProject.Utilities namespace.
' Without "Global." prefix the "SomeProject.Utilities" namespace
' would be placed under the project's default namespace.
' If your project's default namespace was "SomeProject" then
' you could write only "Namespace Utilities" in this case.
Namespace Global.SomeProject.Utilities
Module SpecialUtility
Public Sub DoSomething()
Console.WriteLine("Doing something")
End Sub
End Module
End Namespace
You should import the namespace of that utility module in the user control.
Imports SomeProject.Utilities
Public Class SomeUserControl
Private Sub SomeAction()
' Now you can call DoSomething() method from SpecialUtility module.
DoSomething()
End Sub
End Class
I was making a class library with some string and integer functions and I came across extension methods and wanted to build some in my library. After I try to use it in my other project through the library it doesn't recognize it.
Take this example:
<HideModuleName>
Public Module StringExtensions
<Extension()>
Public Sub Print(ByVal aString As String)
Console.WriteLine(aString)
End Sub
End Module
This sub takes a string and writes it(just an example) but I cannot use it in another project by reference. Any help is greatly appreciated
Please see the answer to the following question: Can't specify the 'async' modifier on the 'Main' method of a console app
I am trying to do this with a VB.NET program. I have added the package using NUGET and I have ensured that the Reference is added. Please see the code below:
Imports Nito.AsyncEx
Public Class ScheduledTasks
Private Shared Async Sub MainAsync(args As String())
Dim bs As New Bootstrapper()
Dim list As VariantType = Await bs.GetList()
End Sub
End Class
The error is: Type BootStrapper is not found. I have used Intellisense to look at the types contained in Nito.AsyncEx and Bootstapper is not there? How do I create an asynchronous main method using VB.NET?
Bootstrapper is not a part of AsyncEx. AsyncContext is:
Imports Nito.AsyncEx
Public Class Program
Private Shared Sub Main(args As String())
AsyncContext.Run(Function() MainAsync(args))
End Sub
Private Shared Function MainAsync(args As String()) As Task
...
End Function
End Class