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.
Related
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.
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.
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).
A little confused about class modifiers in VB.NET
In my project i have a BI layer with multiple classes. In each class i have some public methods \ functions that i expose via the public shared modifier.
However in different classes in the BI layer, i need to access methods in a different class, but the same namespace (in the BI layer) that i do not want exposed to the UI project \ layer.
I thought the Friend modifier will expose the method to classes in the same namespace, but it gives me an error saying reference to a non-shared member requires and object reference
Friend modifier (internal in C#) exposes members to classes in the same assembly.
The error in your case is not related to friend modifier. From the error message it seems that you are trying to access instance (i.e. non shared) method as if it was shared method. You need an instance of the class in order to call such methods.
Code sample would be helpful, because it would help to easier tell what should be changed.
Take the following examples.
Instance method
Friend Class Foo
Friend Sub Fubar()
'do something
End Sub
End Class
Usage:
Dim fu As New Foo
fu.Fubar()
Shared method
Friend Class Foo
Friend Shared Sub Fubar()
'do something
End Sub
End Class
Usage:
Foo.Fubar()
I was reading about private constructor and found a few points that I couldn't understand. It said, if you declare a constructor as private:
That class cannot be explicitly instantiated from another class
That class cannot be inherited
Should be used in classes containing only static utility methods
My first question: Point 2 says the class cannot be inherited. Well, if you declare a class private then it would still satisfy this property. Is it because, if a class is private, it can still be explicitly instantiated from outside by another class?
My second question: I don't understand point 3. If I have a helper class which is full of static methods, I would never have to instantiate that class to use the methods. So, what is the purpose of a constructor in that class which you are never going to instantiate?
Answer for Java
Question 1 You're confusing a private class, with a class that has a private constructor. Private constructors are used mainly for static classes that are not meant to be instatiated (i.e. they just have a bunch of static methods on them).
Question 2 Exactly there is no need for a constructor so you have to explicitly create a private constructor so that it does not get a default constructer that the JVM will provide if none is defined
An empty class with no methods defined will always be given a no argument constructor by the JVM by default
I take java and c++ as an examples (not the best OO languages known, but very popular) - since you are not defining which languge do you mean.
Ad.2. In these languages you must either call superclass constructor explicitly or it is implicitly called for you. From a subclass you cannot call private methods (only public and protected) - this rule applies to constructors as well. This means if the class has only private constructors, there is no way to call one in subclass constructor. So you cannot subclass such class.
Ad. 3. It is just to avoid confusion - since this class is only a container for utility methods, there is no point in instantiating it. This way you can enforce this rule at compile time.