How do I extend My.Resources

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using My.Resources (VS2005) to build all messages that my application
displays to the user. Often I like to embed a new line character in the
middle of the string to make it more readable. Currently I do this by typing
a place holder in the string value when I type it into the resource editor
(e.g. <NL>), and then when I use the string I replace the place holder like
this:

MsgBox(My.Resources.SomeCustomMessage.Replace("<NL>", vbNewLine)

This works, but of course I need to remember to do the Replace each time. I
could wrap all my calls to My.Resources in a function like this:

MsgBox(ReplaceNL(My.Resources.SomeCustomMessage))

but this appears very clusy to me. Is there some better way to handle this
that you .Net gurus can see that I can't.
 
Peter,

Thanks for the response, that certainly helps solve the new line problem,
but there's actually a bit more to this problem that I left out so as not to
overly complicate my question. I have also spent a bit more time on it so
here is an update.

As well as the new line placeholders, some of my resource strings also
include placeholders that need to be replaced with a value at run time. For
example:

"The file '<filename>' already exists. Do you want to replace it?"

At run time I would then call:

MsgBox(My.Resources.FileExistsPrompt.Replace("<filename>", FileNameVariable))

I thought the best way to solve this problem would be to create my own class
that inherits from My.Resources and then provide an override to any resource
items that included replaceable parameters. Then I would be prompted to
provide the parameter whenever I used that resource item. Eg.

Public Overrides ReadOnly Property FileExistsPrompt(Byval FileName as
String) As String
Get
Return FileExistsPrompt.Replace("<filename>", FileName)
End Get
End Property
....

Then I would simply call:

MsgBox(My.Resources.FileExistsPrompt(FileNameVariable))

However, I am not able to inherit from My.Resources because it has been
implemented as a module. Is there any way around this?

BTW, I am relatively new to .Net and often find myself stuck in the old
(procedural) way of doing things. If there is a simpler approach to this
issue a pointer in the right direction would be greatly appreciated.
 
Hi again, I haven't got vs2005 sot I can't test with the my namespace but I
would use a general function something like this

Private Function ReplaceInString ( yourMessage as String, parameter as
String, myValue as string) as String
return replace(yourMessage, parameter, myValue)
End Function

this way you only have got one Function, but as you said before you don't
need to forget to call it, but again I haven't got VS2005 so I can't tell
you if there is a better solution in 2005

Greetz Peter
 
David,
In addition to the other comments:

Instead of:

| "The file '<filename>' already exists. Do you want to replace it?"

Consider:

| "The file '{0}' already exists. Do you want to replace it?"

Then you can use:

| MsgBox(String.Format(My.Resources.FileExistsPrompt, FileNameVariable))


Some advantages of String.Format over Replace include but are not limited
to:

- String.Format is built into the Framework & more importantly leveraged by
formatable types such as Integer & Decimal.

For example, you can format currency types:

Dim format As String = "Total: {0:C}"

Dim value As String = String.Format(format, 123.3456D)

- Other types, such as Console.Write & StringBuilder.AppendFormat also
understand the formatting.

Console.Write(format, 123.3456D)

Dim sb As New StringBuilder
sb.AppendFormat(format, 123.3456D)


- Multiple place holders are supported in a single pass:

Dim format As String = "Total: {0:C}, {1}, {2}"

Dim value As String = String.Format(format, 123.3456D, "a", "b")


For details on formatting in .NET see:

http://msdn2.microsoft.com/en-us/library/fbxft59x(VS.80).aspx

For composite formatting (the {0} syntax above) see:

http://msdn2.microsoft.com/en-us/library/txafckwd(VS.80).aspx


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Peter,
|
| Thanks for the response, that certainly helps solve the new line problem,
| but there's actually a bit more to this problem that I left out so as not
to
| overly complicate my question. I have also spent a bit more time on it so
| here is an update.
|
| As well as the new line placeholders, some of my resource strings also
| include placeholders that need to be replaced with a value at run time.
For
| example:
|
| "The file '<filename>' already exists. Do you want to replace it?"
|
| At run time I would then call:
|
| MsgBox(My.Resources.FileExistsPrompt.Replace("<filename>",
FileNameVariable))
|
| I thought the best way to solve this problem would be to create my own
class
| that inherits from My.Resources and then provide an override to any
resource
| items that included replaceable parameters. Then I would be prompted to
| provide the parameter whenever I used that resource item. Eg.
|
| Public Overrides ReadOnly Property FileExistsPrompt(Byval FileName as
| String) As String
| Get
| Return FileExistsPrompt.Replace("<filename>", FileName)
| End Get
| End Property
| ...
|
| Then I would simply call:
|
| MsgBox(My.Resources.FileExistsPrompt(FileNameVariable))
|
| However, I am not able to inherit from My.Resources because it has been
| implemented as a module. Is there any way around this?
|
| BTW, I am relatively new to .Net and often find myself stuck in the old
| (procedural) way of doing things. If there is a simpler approach to this
| issue a pointer in the right direction would be greatly appreciated.
|
|
| "Peter Proost" wrote:
|
| > You can use shift+enter to enter a NewLine,
| >
| > Hope this helps,
| >
| > Greetz Peter
| >
| > --
| > Programming today is a race between software engineers striving to build
| > bigger and better idiot-proof programs, and the Universe trying to
produce
| > bigger and better idiots. So far, the Universe is winning. (Rich Cook)
| >
| > "David" <[email protected]> schreef in bericht
| > | > > I am using My.Resources (VS2005) to build all messages that my
application
| > > displays to the user. Often I like to embed a new line character in
the
| > > middle of the string to make it more readable. Currently I do this by
| > typing
| > > a place holder in the string value when I type it into the resource
editor
| > > (e.g. <NL>), and then when I use the string I replace the place holder
| > like
| > > this:
| > >
| > > MsgBox(My.Resources.SomeCustomMessage.Replace("<NL>", vbNewLine)
| > >
| > > This works, but of course I need to remember to do the Replace each
time.
| > I
| > > could wrap all my calls to My.Resources in a function like this:
| > >
| > > MsgBox(ReplaceNL(My.Resources.SomeCustomMessage))
| > >
| > > but this appears very clusy to me. Is there some better way to handle
this
| > > that you .Net gurus can see that I can't.
| > >
| > >
| >
| >
| >
 
Back
Top