Force Full Path For Method - vb.net

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

Related

How to prevent the execution of the same Sub called in NEW() constructor a lot of times while objects are inherited?- vb.net

First of all: I have 3 interfaces, each has some class holding required assembly & license data,the first two interfaces are implemented by the next one(s): ISolution --> IExtension --> IExportPlugin, and their objects are inherited from the base class - Solution.
ISolution can be implemented by any application, at least to provide licensing features.
IExtension is a base for the IExport, IImport, IService etc - I planned a lot of derivatives, if it will be neccessary, the license checking is required for every type of extension (plugin).
Each derivative class initiates the calling LicenseCheck sub in New() of base class, which shows frmActivate dialog if the license not found.
So, in inheritance, it shows ACTIVATE three times!!!.
Say, please, can I prevent this window showing, except the last time, when IExportPlugin created?
Yes, I can add optional parameter 'OmitLicenseChecking', but it`s not the best idea due to security reasons.
Every program (exe or dll) must incapsulate it without any possibility to avoid checking. E.g. Checking is a must and must be hidden.
Thanks, friends.
And sorry for my English))
A guess based on the information provided
Public MustInherit Class Solution
Public Shared Lock As New Object
Public Shared Shown As Boolean
Public DoShow As Boolean
Public Sub New()
SyncLock Lock
If Not Shown Then
Shown = True
Me.DoShow = True
Else
Me.DoShow = False
End If
End SyncLock
End Sub
End Class
Public Class SolutionA : Inherits Solution
Public Sub New()
If Me.DoShow Then
Stop
End If
End Sub
End Class
Public Class SolutionB : Inherits Solution
Public Sub New()
If Me.DoShow Then
Stop
End If
End Sub
End Class
Public Class SolutionC : Inherits Solution
Public Sub New()
If Me.DoShow Then
Stop
End If
End Sub
End Class
To test, where the Stop represents the dialog,
Dim foo3 As New SolutionC
Dim foo2 As New SolutionB
Dim foo1 As New SolutionA

How do I reference a Module in VB from a NuGet package

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.

In vb.net, how do I access a public function in a module from within a user control?

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

Expression is not a method from within a class

I am getting an error when trying to call a form from within a method:
Expression is not a method
My code structure looks like this:
Public Class frmMain
Class Server
Private Shared Sub StringMessageReceived()
Call frmMM()
End Sub
End Class
End Class
How can I call the windows form within the class?
You create an instance of the form, and then call its Show (or ShowDialog) method:
Public Class frmMain
Class Server
Private Shared Sub StringMessageReceived()
Call New frmMM().Show()
End Sub
End Class
End Class

VB Partial class... stuck!

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.