Private and Public Class at Class Library
I'm posting this there is a relationship with my previous post
http://www.vbforums.com/showthread.p...ighlight=group
I have two projects, namely:
1. Project1 (Windows Application)
2. Project2 (Class Library)
in Project2 there are several classes:
* frmLogin.vb
* frmCustomer.vb
* clsGlobals.vb
in my case, I want to frmLogin.vb and frmCustomer.vb not called in Project1 and I can only call is clsGlobals.vb
how for this case?
thank you
Re: Private and Public Class at Class Library
Hmmm.
Public subs and functions can be called by anything referencing your dll or exe
Private subs and functions can only be called from within the class
Friend subs and functions can only be called from with the program.
Of course if you load an assembly via reflection you can make private stuff public pretty easy.
I'm not sure exactly what you're trying to do, but in many cases you would use a public function or sub that in turn calls private functions and sub and sets private variables you that you can better control the business logic of the function.
Re: Private and Public Class at Class Library
What you want is the Friend access modifier. When a class or member is Friend it can only be accessed from within the same project. So if you make your two frm__ classes Friend (instead of Public) you can still access them from within Project2, but not from within Project1.
Re: Private and Public Class at Class Library
hello, thanks for the responses.
I will try to clarify again what I want.
I have 2 project (windows & class library)
the class library:
VB.NET Code:
Public Class Class1
Public Sub ShowLogin ()
Dim As New frmLogin fLogin
fLogin.ShowDialog ()
End Sub
End Class
then project into two (windows):
VB.NET Code:
Imports ClassLibrary1
Public Class Form1
Dim c As ClassLibrary1.Class1
Dim c2 As ClassLibrary1.frmLogin
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cLogin As New Class1
cLogin.ShowLogin()
End Sub
End Class
of the code above I want to call Class1 from the Class Library project into the windows. that where I just want to Class1 are recognizable by the project 2. but if we look at the code above:
VB.NET Code:
Dim c2 As ClassLibrary1.frmLogin
frmLogin can be called, while I want frmLogin can only be called in Class1 in the Class Library Project.
how to do this?
thank you
Re: Private and Public Class at Class Library
You're repeating a question that was answered over two days ago. Did you read post #3? Post #2 mentions the same solution, although not quite as clearly as post #3. There's not a lot of point posting a question if you ignore the answers provided.
Re: Private and Public Class at Class Library
thanks for the reply. :)
sorry, because I still do not understand the explanation of the posts 2 and 3.
Re: Private and Public Class at Class Library
You might not have understood that Friend is another access modifier, next to Public and Private. Instead of "Public Class <name>", you can also write "Friend Class <name>". If you do that, your class will only be visible from within the same project (ClassLibrary1).
What you can also do is make your frmLogin class a nested class inside Class1. If only Class1 uses frmLogin, then you can make the nested class Private to achieve the same effect.
So in summary your options are:
1.
Code:
Friend Class frmLogin
'...
End Class
2.
Code:
Public Class Class1
'...
Private Class frmLogin
'...
End Class
End Class
Re: Private and Public Class at Class Library
The first sentence of post #3 says:
Quote:
What you want is the Friend access modifier.
What effort did you make to find out what the Friend access modifier is? Presumably none, because information is easy to find.
http://www.google.com.au/search?q=fr...ient=firefox-a
Besides that, post #3 also says:
Quote:
make your ... classes Friend (instead of Public)
It doesn't get much clearer than that. This:
Code:
Public Class Class1
becomes this:
Code:
Friend Class Class1
for whatever classes you don't want accessible outside the assembly. Exactly as post #3 says, Friend instead of Public.
Re: Private and Public Class at Class Library
thank you, as this is what I want. and I think my questions were answered :)