Results 1 to 9 of 9

Thread: Private and Public Class at Class Library

  1. #1

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    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

  2. #2
    Addicted Member
    Join Date
    Oct 2009
    Posts
    212

    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.
    Have you tried Google?

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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.

  4. #4

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    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:
    1. Public Class Class1
    2.  
    3.      Public Sub ShowLogin ()
    4.          Dim As New frmLogin fLogin
    5.          fLogin.ShowDialog ()
    6.      End Sub
    7. End Class

    then project into two (windows):

    VB.NET Code:
    1. Imports ClassLibrary1
    2.  
    3. Public Class Form1
    4.  
    5.     Dim c As ClassLibrary1.Class1
    6.     Dim c2 As ClassLibrary1.frmLogin
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.         Dim cLogin As New Class1
    10.         cLogin.ShowLogin()
    11.  
    12.     End Sub
    13. 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:
    1. 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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    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.

  7. #7
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Private and Public Class at Class Library

    The first sentence of post #3 says:
    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:
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Private and Public Class at Class Library

    thank you, as this is what I want. and I think my questions were answered

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width