How to make sure that certain class can only be accessed by another class but not others? - vb.net

Say in my vb.net program I already have too many classes and module.
I want to normalize things a bit. So I want to create a new class called FolderStats and I want folderstats to be accessible only by another class folderStatsuser.
I do not want any other class to know that FolderStats even exist
What should I do?

If only FolderStatsUser should have any knowledge of it, then it is an implementation detail of FolderStatsUser. Having a private class might be useful
public class FolderStatsUser {
private class FolderStats {}
}

#Jim - here is the vb.net version...
Public Class FolderStatsUser
Private Class FolderStats
End Class
End Class
You can read more about nested classes at https://msdn.microsoft.com/en-us/library/twwxww86(v=vs.71).aspx

You could use an assembly and use the Friend access modifier so that only classes in the assembly can access your class. Classes that shouldn't access it would need to be in another assembly.

Related

Is there a solution to "Cannot access '<init>': it is private in XYZ?

I included a library I'd like to use, but in accessing to one of its classes I get the error message,
"Cannot access '<init>': it is private in [class name]
Is there something I can do to rectify this on my side, or am I just stuck to not use the package?
The error means the constructor is private. Given your comment, I'm assuming you're using a library. If this is the case, you'll have to find a different way to initialize it. Some libraries have factories or builders for classes, so look up any applicable documentation (if it is a library or framework). Others also use the singleton pattern, or other forms of initialization where you, the developer, don't use the constructor directly.
If, however, it is your code, remove private from the constructor(s). If it's internal and you're trying to access it outside the module, remove internal. Remember, the default accessibility is public. Alternatively, you can use the builder pattern, factory pattern, or anything similar yourself if you want to keep the constructor private or internal.
I came across this issue when trying to extend a sealed class in another file. Without seeing the library code it is hard to know if that is also what you are attempting to do.
The sealed classes have the following unique features:
A sealed class can have subclasses, but all of them must be declared in the same file as the sealed class itself.
A sealed class is abstract by itself, it cannot be instantiated directly and can have abstract members.
Sealed classes are not allowed to have non-private constructors (their constructors are private by default).
Classes that extend subclasses of a sealed class (indirect inheritors) can be placed anywhere, not necessarily in the same file.
For more info, have a read at https://www.ericdecanini.com/2019/10/14/kotlins-sealed-class-enums-on-steroids/
Hopefully, this will help others new to Kotlin who are also encountering this issue.
Class constructors are package-private by default. Just add the public keyword before declaring the constructor.
By default constructor is public so need to remove internal keyword.

VB.NET how to allow people implement a class without letting people see or changing the code?

How do you allow people to implement a class without letting them see or change the code?
Implementation implies Interface. You can just define an Interface in a .NET assembly (.dll) which you provide to said people. There is no implementation in an interface. No code to change other than the property and method names and types.
Public Interface IFoo
Sub Bar()
End Interface
If this code is given to people in a code file, they can change the definition of the Interface.
There is also an abstract class, in VB it is called MustInherit. Other classes can inherit or derive from it (not implement, as your question said, but you might mean this).
Public MustInherit Class Foo
Sub Bar()
Console.WriteLine("Bar")
End Sub
MustOverride Sub Barr()
End Class
If you give this to people in a code file, they can change what happens in Bar() or really do anything they want to it. Again, putting this in a .NET assembly and giving the .dll to people would prevent this from happening. In most cases this is enough to keep people from changing your code.
You can do this by making the output type of your project a class library. This will compile into a .dll file.
Build the project. In the output directory will be a .dll file (if compilation was a success).
You, or other people, could then add a reference to the .dll file created by your class library, in a new project. You could declare a class which implements the public interface defined in it
Public Class MyIFoo
Implements WindowsApplication1.IFoo
Public Sub Bar() Implements WindowsApplication1.IFoo.Bar
End Sub
End Class
And/or make a class which inherits the abstract class you made in the .dll
Public Class MyFoo
Inherits WindowsApplication1.Foo
Public Overrides Sub Barr()
End Sub
End Class
In your new project, you cannot see or change the code of the Interface IFoo or abstract class Foo.
You should use the private keyword to prevent a client of the class from accessing or modifying fields directly. You expose the methods and properties you want the client to access via the public or protected keyword (for inheritance).

mustInherit class shared function

Hi got a class MustInherit and many instance.
I need a function shared function declared once (normally in the template).
But as a mustInherit, I cant call it. I need to use one of my instance of the class.
Is there another way?
Public MustInherit MyBaseClass
...
public shared function UnknowBaseFunction () as object
....
x = InheritsClassByMyBaseClass.UnknowBaseFunction()
There is no way to inherit a shared member so that it also is available through the derived class name. Obviously, by removing the Shared keyword, it will be available in all derived types, but only via instantiated objects, not as a shared method. If it doesn't make sense to have everything call the method via the base class name, then I would recommend breaking it out into a separate class altogether which is neither in the base class nor the derived classes.

vb.net: multiple inheritance in an interface

I'm facing a problem regarding multiple inheritance in VB.net:
As far as I know VB.net does not support multiple inheritance in general but you can reach a kind of multiple inheritance by working with interfaces (using “Implements” instead of “Inherits”):
Public Class ClassName
Implements BaseInterface1, BaseInterface2
End Class
That works fine for classes but I’d like to have an interface inheriting some base interfaces. Something like that:
Public Interface InterfaceName
Implements BaseInterface1, BaseInterface2
End Interface
But the “Implements” keyword is not allowed for interfaces (what makes sense, of course). I tried to use a kind of abstract class which I know from Java:
Public MustInherit Class InterfaceName
Implements BaseInterface1, BaseInterface2
End Class
But now I need to implement the defined methods from BaseInterface1 and BaseInterface2 within the InterfaceName class. But as InterfaceName should be an interface, too, I don’t want to have to implement these methods within that class.
In C# you can do that quite easy:
public interface InterfaceName: BaseInterface1, BaseInterface2 {}
Do you know if I can do something similar in VB.net?
Similar to Java, in VB.NET interfaces "extend" other interfaces. That means they "inherit" their functionality. They do not implement it.
Public Interface InterfaceName
Inherits BaseInterface1, BaseInterface2
End Interface
Try
Public Interface InterfaceName
Inherits BaseInterface1
Inherits BaseInterface2
End Interface
A workaround is to have the abstract class (mustinherit) pass on the job of defining each item in the interface it does not want to implement with mustoverride. Try to predefine each one in a general sense if possible and make it overridable.
I would be careful when inheriting interfaces.
While it works, I have found that if you bind a BindingList(Of InterfaceName) to a BindingSource and the BindingSource to a DataGridView, then properties in Interface1 and Interface2 are not visible to the Visual Studio DataGridView designer for allocating as columns to the DataGridView.

VB.NET Creating Classes, What is Public Class MyClass(Of Type)?

I'm still learning ASP.NET and I often see code like this throughout parts of our framework:
Public MustInherit Class DBFileManager(Of F As IDBFile, FC As IDBFileContent, FT As IDBFileThumb)
Can anybody tell me what this means? Much thanks!
Its a generic. That means a DBFileManager can be created that acts on 3 classes that implement the 3 named Interfaces
see http://msdn.microsoft.com/en-us/library/w256ka79(VS.80).aspx for more information
To build on what #Jimmy said: It is also an Abstract Class, which means it acts as a base class - you can't use it directly, you must sub class it to use. That subclass must implement the 3 types in the class header.