Extending a class by namespace - vb.net

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

Related

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

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

Force Full Path For Method

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

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.

One Class with two different Namespaces?

Is something like this possible?
Namespace Transaction, Document
Class Signer
Public Sub New()
'Do Work
End Sub
End Class
End Namespace
I basically want to be able to instantiate the Signer class from either Namespace. The reason is that I mistakenly set it up in the Transaction class and need to migrate it over to the Document class without breaking existing legacy code. I'd prefer to not have the same Signer class duplicated in both Namespaces if possible.
I don't think you can do that. However, you CAN define the object in one namespace and then make a class of the same name in the other namespace that simply inherits the first class, like so:
Namespace Transaction
Class Signer
' Signer class implementation
End Class
End Namespace
Namespace Document
Class Signer
Inherits Transaction.Signer
End Class
End Namespace
What you need to do is create the class in separate namespaces so that you actually have two different classes declared. Mark the one in the Transaction namespace as obsolete and have it act as a proxy to the real class that way you do not duplicate the implementation.
Namespace Transaction
<Obsolete> _
Public Class Signer
Private m_Implementation As Document.Signer
Public Sub New()
m_Implementation = new Document.Signer()
End
Public Sub DoSomething()
m_Implementation.DoSomething()
End Sub
End Class
End Namespace
Namespace Document
Public Class Signer
Public Sub New()
End
Public Sub DoSomething()
End Sub
End Class
End Namespace
A class can only belong to one namespace. The only other thing you can do is duplicate that class in another namespace. You should be able to refactor that code and change the namespace, visual studio will propogate those changes throughout your code.

Can I inherit from a generic class without specifying a type?

I have the following sample code in a VB.NET console application. It compiles and works, but feels like a hack. Is there a way to define EmptyChild so that it inherits from Intermediate(Of T As Class) without using the dummy EmptyClass?
Module Module1
Sub Main()
Dim Child1 = New RealChild()
Child1.Content = New RealClass()
Dim Child2 = New EmptyChild()
Console.WriteLine("RealChild says: " & Child1.Test)
Console.WriteLine("EmptyChild says: " & Child2.Test)
Console.ReadLine()
End Sub
Public Class EmptyClass
End Class
Public Class RealClass
Public Overrides Function ToString() As String
Return "This is the RealClass"
End Function
End Class
Public MustInherit Class Base(Of T As Class)
Private _content As T = Nothing
Public Property Content() As T
Get
Return _content
End Get
Set(ByVal value As T)
_content = value
End Set
End Property
Public Overridable Function Test() As String
If Me._content IsNot Nothing Then
Return Me._content.ToString
Else
Return "Content not initialized."
End If
End Function
End Class
Public MustInherit Class Intermediate(Of T As Class)
Inherits Base(Of T)
'some methods/properties here needed by Child classes
End Class
Public Class RealChild
Inherits Intermediate(Of RealClass)
'This class needs all functionality from Intermediate.
End Class
Public Class EmptyChild
Inherits Intermediate(Of EmptyClass)
'This class needs some functionality from Intermediate,
' but not the Content as T property.
Public Overrides Function Test() As String
Return "We don't care about Content property or Type T here."
End Function
End Class
End Module
The other way to do this would be to move the generic code out of the Base class and then create 2 Intermediate classes like this:
Public MustInherit Class Intermediate
Inherits Base
'some methods/properties here needed by Child classes
End Class
Public MustInherit Class Intermediate(Of T As Class)
Inherits Intermediate
'implement generic Content property here
End Class
Then RealChild would inherit from the generic Intermediate and EmptyChild would inherit from the non-generic Intermediate. My problem with that solution is that the Base class is in a separate assembly and I need to keep the code that handles the generic type in that assembly. And there is functionality in the Intermediate class that does not belong in the assembly with the Base class.
Yes, you need to specify a type parameter when you inherit, or your EmptyChild must be generic as well. But, you don't have to dummy up a EmptyClass - just use Object as your type parameter:
Public Class EmptyClass
Inherits Intermediate(Of Object)
End Class