Results 1 to 9 of 9

Thread: [RESOLVED] How to Access Resource File from a Class Library ?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    16

    Resolved [RESOLVED] How to Access Resource File from a Class Library ?

    Hello,

    I'm building a generic Class Library that I will use it in all my projects.

    However, I want to know how can I access Resource file from a Class Library ?

    I will appreciate if you can send me a code sample!

    Thank you for your help

    Paul
    Last edited by paulnamroud; Jun 3rd, 2011 at 04:04 PM.

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: How to Access Resource File from a Class Library ?

    You mean you want to allow consumers of your library to access a resource in the library? Or you want to pass in a resource from say a .exe that your library can use?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    16

    Re: How to Access Resource File from a Class Library ?

    Yes, I want consumer to access Resource file in Library.

    Example:
    I have my Project Class Library called LibPublic under C:\Projects\LibPublic.

    And I should the following files:
    C:\Projects\LibPublic\CConnection.cs
    C:\Projects\LibPublic\CConnection.resx (or. resources)
    C:\Projects\LibPublic\CUser.cs
    C:\Projects\LibPublic\CUser.resx (or. resources)
    ...

    So, for each class I want to have a resource file and access it when i need to return a specific message
    Last edited by paulnamroud; Jun 8th, 2011 at 08:36 AM.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    16

    Re: How to Access Resource File from a Class Library ?

    I have some help for this issue ... So now, I can do the following where I have only one Resource file Resources.resx: LibPublic.Properties.Resources.ResourceManager.GetString("RecordType_Missing").ToString() where LibPublic is the name of my Class Library.

    I have already worked on a Website Project where we can create our classes under App_Code Folder (e.g. App_Code/CCustomer.cs), and then we can create all related Resouces File under App_GlobalResources (e.g. App_GlobalResources/CCustmomers.resx or App_GlobalResources/CCustmomers.fr.resx ... ) . And we can acces the resouces files by doing the following: Resources.CCustomers.FirstName_Missing.ToString()


    So I would like to know if there's a way to have a resource file for each class inside my Class Library or something similar to what we have in a Website Project ?

    Thank you

  5. #5
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: How to Access Resource File from a Class Library ?

    It's easy to create one, just create a new resource with the same name as the class. You will get a warning every time you try to edit it however, as it believe you should be using the forms/project designer tool. I have just done it here to test, but have no idea what it can break. There is a warning for a reason . As long as you don't try to convert it to a form later on ...

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: How to Access Resource File from a Class Library ?

    If you were using VS2008 or higher you can change the modifier for the resources from Friend to Public which makes the resources accessible to a project which uses the class library.

    In VS2005 you can do it as follows but understand that if you add new data to your resources after the fact the following will have to be done over again.

    Under Project menu select Show all files
    Open Resources.Designer.vb

    Change Friend to Public
    Code:
    Friend Module Resources
    Code:
    Public Module Resources
    For each string item change from Friend to Public

    Do the same if you have added a resource file to the project.

    So in the attached VS2005 project there is a library with a stub Class1 class and for resources

    My.Resources.UserAccount

    In Class1Resources.resx
    My.Resources.Class1Resources.FirstNameMissing

    The Windows form project uses the class library and accesses the resources
    Code:
    Public Class Form1
       Private Sub Form1_Load( _
          ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
    
          Me.Text = ClassLibrary1.My.Resources.UserAccount
          TextBox1.Text = ClassLibrary1 _
             .My.Resources.Class1Resources.FirstNameMissing
    
       End Sub
    This may not be acceptable but is an alternative
    Attached Files Attached Files
    Last edited by kareninstructor; Jul 20th, 2011 at 01:00 PM.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2006
    Posts
    16

    Re: How to Access Resource File from a Class Library ?

    Thank you Kevin for your answer!
    It works fine since I'm using VS 2005 so i have to do the chnages manually evry time i need to add/modify a resources.

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: How to Access Resource File from a Class Library ?

    Quote Originally Posted by paulnamroud View Post
    Thank you Kevin for your answer!
    It works fine since I'm using VS 2005 so i have to do the chnages manually evry time i need to add/modify a resources.
    Yes, in VS2005 you must do the changes manually. Of course if you add all resources at one time you do the manual change one time but life is never that way is it.

  9. #9
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: [RESOLVED] How to Access Resource File from a Class Library ?

    Or, declare a public property that returns the local resource, then you never have to change it.

Tags for this Thread

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