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.
Related
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
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
is it possible to extend different classes with the same generic class?
I tried something like this:
Public Class A
Public Sub TestA()
Debug.Print("Test A")
End Sub
End Class
Public Class B(Of T)
Public Sub TestB()
Debug.Print("Test B")
End Sub
End Class
Public Class C
Inherits B(Of A)
Public Sub TestC()
TestA() '**<-- Thows error 'is not declared'**
TestB()
Debug.Print("Test C")
End Sub
End Class
I basicly have some usercontrols, which derive from Combobox or Textbox and i'd like both to implement some functions(and interfaces) that are defined in a base class. In C++ i'd do it with multi inheritance.
is it possible to extend different classes with the same generic class?
Generics isn't some kind of "workaround" for a lack of multiple inheritance, no. Your class C doesn't derive from A - it just means that the T in B(Of T) would be A in the context of C.
Which instance of A would you expect TestA() to be called on? Creating an instance of C certainly doesn't create an instance of A...
The fact that B(Of T) doesn't use T anywhere should be a warning signal - types which are generic but never use their generic type parameters are generally problematic.
It's hard to know exactly how to help you solve your real problem without more details, but you can't add a common base class in like this, when you also need to derive from other types which aren't under your control.
Perhaps extension methods would help?
You could make both your Combobox and your Textbox classes implement the same interface.
Then you could define extension methods on that interface class.
Thanks to your hint i got this working with extentions
Public Class Form1
Public Interface IA
Property val As String
End Interface
Public Class A
Public Sub test()
Debug.Print("test")
End Sub
End Class
Public Class C
Inherits A
Implements IA
Public Property val As String Implements IA.val
Public Sub TestC()
val = "testxxx"
TestA()
test()
End Sub
End Class
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim ct As New C
ct.TestC()
End Sub
End Class
Module TestModule
<Extension()>
Public Sub TestA(ByVal pvIA As IA)
Debug.Print(pvIA.val)
End Sub
End Module
This way every class can implement it's own 'parent' (like A here) and i don't need to implement the function TestA for every class.
thank you
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
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.