Import/Call class vb.net from external .vb file - vb.net

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

Related

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.

Extending a class by namespace

I have a class within a library, with no root namespace, firstone.dll:
namespace first
public partial class one
public sub fun()
end sub
end class
end namespace
My second library, with no root namespace, firstonetwo.dll, references firstone.dll:
namespace first.one
public partial class two
public sub testfun()
first.one.fun() 'not recognized'
end sub
end class
end namespace
or
namespace first
public partial class one
public partial class two
public sub testfun()
first.one.fun() 'also not recognized'
end sub
end class
end class
end namespace
Is there a way to extend the class in a separate dll and still have access to the original class? I don't want to inherit the class just extend it.
The problem is that you have a namespace and a class with the same name and that causes a lot of confusion. Your first example is a class called one in the first namespace and your second example is a class called two in the first.one namespace. Just because they can be written the same, there's big difference between the two.
Do not name a class the same as its namespace, Part One
EDIT
Your tree looks something like this:
first (ns)
one (class)
fun (method)
one (ns)
two (class)
VB is having a problem walking the tree and determining which one to look at.
Take a look at "Extension Methods" http://msdn.microsoft.com/en-us/library/bb384936.aspx
This is the only form of 'extension' possible without inheriting.
This is untested
namespace first
public partial class one
public overridable sub fun()
end sub
end class
end namespace
Separate CLASS File
Imports first
namespace one
public partial class fun
public overrides sub fun()
end sub
end class
end namespace

VB.net (VS 2005) User Class access to public variable of maindisplay.master.vb

I am attempting to access a public member of the Master page class from a User Class.
In my master page, I have:
Partial Class MainDisplay
Inherits System.Web.UI.MasterPage
Public Shared m_test As Integer
...
In my User Class, I have:
Imports Microsoft.VisualBasic
Imports System.Web.UI.MasterPage
Public Class mytest
Public Function getValue() As Integer
Dim iRet As Integer = 0
iRet = Master.m_test ' how do i get access to the public member**
End Function
End Class
How do I get access to m_test from the user class?
thanks
My VB is a little rusty, but the first thing I notice is that the class with the shared member is called MainDisplay but you're referencing it as Master when you try to access the shared member. Does it work if you reference it as MainDisplay (note that you may need to fully-qualify the namespace or import the namespace in the mytest class file).

Is it possible to have forms in sub-namespaces of a VB.NET WinForms project?

If I create a new class library project in VB.NET, I can create subfolders (a la C#), add WinForm objects to these subfolders, and then specify a namespace:
Namespace Sub1.Sub2
Public Class SomeForm
Public Sub New()
InitializeComponent()
End Sub
End Class
End Namespace
This resolves as ProjectRootNamespace.Sub1.Sub2.SomeForm, which is good.
However, if I create a new WinForms project in VB.NET, and attempt the same thing, I get this error in the designer:
The class SomeForm can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try loading the designer again.
Is there a way to have forms in sub-namespaces of a VB.NET WinForms app instead of in the root namespace?
Are you renaming the namespaces in both the Form.vb as well as the Form.Designer.vb? you need to make sure that both files declare the same object.
Example Form.vb
Namespace X
Namespace Y
Public Class Form1
End Class
End Namespace
End Namespace
And Form.Designer.vb
Namespace X
Namespace Y
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
...
End Class
End Namespace
End Namespace

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.