469,085 Members | 1,034 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,085 developers. It's quick & easy.

Resource reader broken. What have i done wrong please

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
Jul 26 '06 #1
1 1925
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
Jul 26 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Code Guy | last post: by
7 posts views Thread by Christofer Dutz | last post: by
3 posts views Thread by J.Marsch | last post: by
12 posts views Thread by bj7lewis | last post: by
reply views Thread by Fabrice | last post: by
9 posts views Thread by Alf P. Steinbach | last post: by
5 posts views Thread by =?Utf-8?B?QXhlbCBEYWhtZW4=?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.