I declare an enum in a Module:
friend Module M
Enum E
Elem1
Elem2
end Enum
end Module
and use it in another class:
Class C
console.writeline(M.E.Elem1) ' this works
End Class
But I would like to simplify the writing of M.E to MyE so I tried:
Class C
private myE as M.E ' and variations of this using getype
console.writeline(myE.Elem1)
end Class
Is there a way to do this? That is, declare myE as M.E.
I tried variations using getype but no success.
You can use import alias to simplify the name of any type or namespace. In your case:
Imports myE = M.E
Class C
Public Sub Test()
Console.WriteLine(myE.Elem1)
End Sub
End Class
Just don't forget that Imports statements must be placed at the beginning of the file.
Related
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.
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
Public Class A
Public Class B ... End Class
Public Class C ... End Class
End Class
I'm writing some utility code that makes heavy use of B and C, so up at the top I did Imports ThisProject.A. Great, until I want to use that file in ThatProject. I normally link to code like this, so changing it breaks the other project.
I thought Namespace was the fix, but that adds another layer to the space, so the project name is still there. Did I do this wrong?
Alternately, is there syntax for "this project" in Imports?
apple.vb
Namespace ThisProject.Foo
Public Class Apple
' stuff
End Class
End Namespace
orange.vb
Namespace ThisProject.Foo
Public Class Orange
' other stuff
End Class
End Namespace
banana.vb
Namespace ThisProject.Foo
Public Class Banana
' yet more stuff
End Class
End Namespace
(ThisProject)someotherclass.vb
Imports Foo
' other stuff in the class
Private Sub Blah()
Dim x as new Banana()
End Sub
(ThatProject)yetanotherclass.vb
Imports ThisProject.Foo
' other functions, etc
Private Sub Cool()
Dim y as new Orange()
End Sub
I'm stuck when trying to create the simplest partial class in order to access a table property.
I have a LINQ auto generated DataContext with:
Namespace VuBridgeDB
<System.Data.Linq.Mapping.DatabaseAttribute(Name:="C:\Users\Didier\Documents\Visual Studio 2010\Projects\VuBridge1\VuBridge1\Data\VuBridgeDB.sdf")> _
Partial Public Class myClassDataContext
Inherits System.Data.Linq.DataContext
Private Shared mappingSource As System.Data.Linq.Mapping.MappingSource _
= New System.Data.Linq.Mapping.AttributeMappingSource()
Partial Private Sub InsertCompetitions(ByVal instance As Competitions)
End Sub
End Class
<Table(Name:="Competitions")> _
Partial Public Class Competitions
Partial Private Sub OnC_TitleChanged()
End Sub
End Class
Now I try to add my own business logic in a class of mine:
Public Class myClassDataContext
Private Sub InsertCompetitions(ByVal instance As Competitions)
End Sub
End Class
Public Class Competitions
Private Sub onC_SiteChanged()
Me.
End Sub
End Class
Problem:
VB.NET refuses the class name myClassDataContext saying it already exists.
I was expecting the C_Site property to be available in my own class (as well as other Competitions columns), but when I type "Me.", IntelliSense doesn't give me any of the Competitions properties (ie data columns).
I've tried all sorts of Partial Public, adding namespace the same as the one used in the auto-generated... Nothing works.
Can someone provide with a working sample please?
You need to make your other declaration of myClassDataContext partial too:
Public Partial Class myClassDataContext
Private Sub InsertCompetitions(ByVal instance As Competitions)
...
End Sub
...
End Class
Otherwise the VB compiler thinks you're trying to declare another "standalone" class which happens to have the same name.
This will fix both of your problems - the other properties etc currently aren't present in your "extra" class code for exactly the same reason.
Ok, thanks guys... I finally get that stuff working, by adding the same Namespace declaration, like this:
Namespace VuBridgeDB
Partial Public Class VubridgeDB
Private Sub InsertCompetitions(ByVal instance As Competitions)
MsgBox("Inserting " & instance.C_Title, vbInformation)
End Sub
End Class
End Namespace
Once this is done, Intellisense fully recognizes the instance parameter.
The class declaration that works with us is simply Partial Class myClassDataContext in a separate file, nothing more. This should be in the same assembly (dll or exe) and namespace of the original class.