Resource reader broken. What have i done wrong please

C

Chukkalove

I originally created several "copy" resource reader classes which worked
great individually, but after I converted them into an parent class with
children they no longer find the resources. Each project contains its own
reader class and resource .resx file from where a string or image is read.

Can someone tell me what I need to do to get them to work please. I attach
code.

Im not sure if it's because the derived classes are in different name spaces
to the parent, or whether "myStringResourceManager = new
ResourceManager(myStringResourceName, Assembly.GetExecutingAssembly());" is
returning the ancestor's assembly rather than the derived class' or if it's
something else completely.


Module 1. Ancestor class
==================
using System;
using System.Globalization;
using System.Resources;
using System.Reflection;
using System.Threading;
using System.Drawing;
namespace P.BaseResourceHandler
{
public class BaseResourceHandler
{
protected string myStringResourceName = "";
protected string myImageResourceName = "";
private ResourceManager myStringResourceManager;
private ResourceManager myImageResourceManager;

private ResourceManager StringResourceManager
{
get
{
if (myStringResourceManager == null)
myStringResourceManager = new ResourceManager(myStringResourceName,
Assembly.GetExecutingAssembly());
return myStringResourceManager;
}
}
private ResourceManager ImageResourceManager
{
get
{
if (myImageResourceManager == null)
myImageResourceManager = new ResourceManager(myImageResourceName,
Assembly.GetExecutingAssembly());
return myImageResourceManager;
}
}

// Get the culture of the currently executing thread.
// The value of ci will determine the culture of
// the resources that the resource manager retrieves.
private CultureInfo cultureinfo = Thread.CurrentThread.CurrentCulture;

/// <summary>
/// Loads a string from the resources and returns it
/// </summary>
/// <param name="ID">ID of the string</param>
/// <returns>The resource string</returns>
public string LoadString(string ID)
{
try
{
// Retrieve the value of thes string resource named
// ID, localized for the culture specified by ci.
String str = StringResourceManager.GetString(ID, cultureinfo);
if (str == "")
throw new Exception("Resource '" + ID + "' is missing from the resource
file");
return str;
}
catch(Exception e)
{
return ID;
}
}// function
/// <summary>
/// Loads an image from the resources and returns it
/// </summary>
/// <param name="ID">ID of the image</param>
/// <returns>The image</returns>
public Image LoadImage(string ID)
{
string fnName = "LoadImage";
try
{
// Retrieve the value of thes image resource named
// ID, localized for the culture specified by ci.
Image image = (Image)ImageResourceManager.GetObject(ID, cultureinfo);
if (image == null)
throw new Exception("Image resource '" + ID + "' is missing from the
resource file");
return image;
}
catch(Exception e)
{
return new Bitmap(10,10);
}
}// function
}// class
}// namespace

Module 2. example child class
==================

using System;
using System.Globalization;
using System.Resources;
using System.Reflection;
using System.Threading;
using System.Drawing;
using P.BaseResourceHandler;
namespace P.SmartCentre
{
/// <summary>
/// ResourceHandler handles resources kept in the resource files
/// </summary>
public class ResourceHandler : BaseResourceHandler.BaseResourceHandler
{
private static ResourceHandler mResourceHandler;

public ResourceHandler()
{
myStringResourceName = "P.SmartCentre.StringResources";
myImageResourceName = "P.SmartCentre.ImageResources";
}
/// <summary>
/// Returns an instance of a ResourceHandler object if called
/// </summary>
public static ResourceHandler Resources
{
get
{
if (mResourceHandler == null)
mResourceHandler = new ResourceHandler();
return mResourceHandler;
}
}// function
}//class
}// Namespace
 
C

Chukkalove

Yes, the problem was with ResourceManager(myStringResourceName,
Assembly.GetExecutingAssembly()) which was returning the parent class
assembly rather than the derived class. Instead I stored the derived class
Assembly during the constructor and accessed that instead.
Thanks anyway
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top