October 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

.NET Remoting and Event Handling in VB .NET, Part 3

  • March 7, 2005
  • By Paul Kimmel
  • Send Email »
  • More Articles »

Finally, notice that one overloaded version of Listen uses an interface called IFormatter. (Listing 7 shows the definition and some examples of IFormatter.) This interface has a single method named ApplyFormatting. Using an interface enables any class that implements IFormatter to be passed to the Listen method. The result is that the message could be formatted as HTML, XML, plain text, or any future style of formatted text without ever changing the client. This is what extensible and loosely coupled code means.

Listing 7: The IFormatter Interface and Two Formatters

Public Interface IFormatter
    Function ApplyFormatting(ByVal text As String) As String
End Interface

Public Class GenericFormatter
    Implements IFormatter

    Public Function ApplyFormatting(ByVal text As String) As String _
           Implements IFormatter.ApplyFormatting
        Return text
    End Function
End Class


Public Class TimeStampedFormatter
    Implements IFormatter

    Public Function ApplyFormatting(ByVal text As String) _
           As String Implements IFormatter.ApplyFormatting
        Return String.Format("{0}: {1}", DateTime.Now, text)
    End Function
End Class

Internationalizing with the ResourceManager

The Help command from earlier in the article reads help information from an external resource file using the ResourceManager (see the Help command in Listing 1).

.NET makes externalizing resources like text and graphics easy. Simply add an Assembly Resource File to your project and define the resources. The example used text, which can easily be added in the resource editor (see Figure 1).

The next step is to load the resources using the ResourceManager defined in System.Resources. You can load the resources by referring to the assembly name followed by the resource file name. (In the sample, the assembly is Client and the resource file is Help, so "Client.Help " is used to construct the resource manager.)



Click here for a larger image.

Figure 1: Using an Assembly Resource File to Externalize Resources Like Text

Invoke ResourceManager.GetString to read a string resource. (For more ResourceManager methods refer to the .NET help.)

To internationalize resources, create additional resource files with the two-character international code for the country between the resource file name and extension. For example, Help.de.resx, would represent the resource file for Germany. If your computer's language is German, that resource file would be loaded instead of the default resource file, which is the one you defined.

There isn't much more to internationalization than that in .NET. You can also create satellite assemblies post-deployment. You could add resources after you deploy your .NET application without re-deploying the whole application.

Note: The ease with which applications can support multiple languages is just one of the compelling reasons to switch to Visual Basic .NET, and I have it on good authority that 2005 is going to be even better.

Finding Goofy OOP Behavior

I read somewhere that the acronym OOP is POO in French. I won't make any O'Reilly-esque political commentary here, but OOP can be POO sometimes.

Have you ever noticed that some applications behave a bit oddly? For example, the chat application had a quirky behavior: it would place the blinking cursor on the line following the 'chat>' prompt. The reason for this is that—in a very OOP way—I reused code from the Broadcaster to display all of the text, including the prompt. For some text, I needed a new line, but the flashing cursor needed to stay on the same line as the prompt. This meant that everything used Console.WriteLine except the 'chat>' prompt, which needed to use Console.Write (no new line).

Because I originally used the Listen method to display all feedback to the client, the new line was used to write the prompt. The result was the cursor wound up in the wrong place. The problem was fixed in the implementation, but I had to add the second Listen method to fix it. Quirky behavior can happen in OOP when you try to reuse code that doesn't fit perfectly.

As a general rule, reuse code and write good OOP. When doing so results in quirky behavior, write and use something else from scratch—even if it means duplicating some behavior. Trying to find perfect inheritance, encapsulation, overloading, or realization relationships often falls into the category of diminishing returns. Sometimes it is just easier to write a one-off version of something to resolve quirky behavior.

Investigate Patterns

More frequently, I am finding that many problems are already solved and documented as patterns. I also find that patterns (and refactoring) can elevate the quality of a solution. Whether you are working on a team or as a one-person show, investigate the patterns in this article and the dozens more published in Design Patterns.

If you have a chance, pick up a copy of one of my upcoming books, UML Demystified or Expert One on One Visual Studio 2005. You will learn more about the practical design and implementation practices related to using design patterns and refactoring.

Download the Code

To download the complete code listing for this series, click here.

About the Author

Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object oriented programming and .NET. Check out his book Visual Basic .NET Power Coding from Addison-Wesley and his upcoming book UML DeMystified from McGraw-Hill/Osborne (Spring 2005). Paul is also the founder and chief architect for Software Conceptions, Inc, founded 1990. He is available to help design and build software worldwide. You may contact him for consulting opportunities or technology questions at pkimmel@softconcepts.com.

If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org.

Copyright © 2005 by Paul Kimmel. All Rights Reserved.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel