CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2018
    Posts
    15

    Creating Basic Wrapper Class in C#

    Creating a Basic Wrapper.
    These three patterns, proxy, decorator, and adapter are all implemented identically. They are all wrappers at the basic level. The functionality they provide is how we can make the distinction between them. Let's first look at a template for a basic wrapper and then at how to use a wrapper to implement each of these patterns can help us in our quest for the bizarre.
    Before we begin to understand how to perform super-feats, we need to understand the basic super-power all C# developers are born with - our secret ability to wrap (not to be confused with rap). Here is how a wrapper works:
    Let's say we have a Thing with a ThingString property and a method PrintThingString() as below:

    Code:
    class Thing
    {
              private string m_string;
              public string ThingString
              {
                        get { return m_string; }
                        set { m_string = value; }
              }
              public void PrintThingString()
              {
                        Console.WriteLine(m_string);
              }
    }
    Here is how we create a basic ThingWrapper to wrap the Thing. It contains a reference to the Thing being wrapped and has the same signature. The ThingWrapper just passes requests into the wrapped Thing.

    Code:
    class ThingWrapper
    {      
              private Thing m_thing;
              public ThingWrapper(Thing pThing)
              {
                        m_thing = pThing;
              }
              public string ThingString
              {
                        get { return m_thing.ThingString; }
                        set { m_thing.ThingString = value; }
              }
              public void PrintThingString()
              {
                        m_thing.PrintThingString();
              }
    }
    Because the ThingWrapper has the same signature as the Thing, it is really no different dealing with the Thing or the ThingWrapper. We can work with either the Thing or the ThingWrapper access our internal string m_string, or perform the base functionality which is to print a line to the console.

    Code:
    Thing t = new Thing();
    t.ThingString = "This is the ThingString";
    t.PrintThingString();
     
    ThingWrapper tWrap = new ThingWrapper(t);
    tWrap.PrintThingString();
    tWrap.ThingString = "This is still the ThingString";
    tWrap.PrintThingString();
    We can wrap any class this way. Now that we understand this "wrapping power", we should only use it for good (or to do some cool stuff). Just remember, things can get pretty complicated if we have wrappers that are really not needed. Sometimes, however, we just can't live without them. Starting in the next article, let's next look at some of the super powers we can manifest using wrappers.
    Last edited by 2kaud; January 4th, 2019 at 07:26 AM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Creating Basic Wrapper Class in C#

    This is a forum for asking questions (and hopefully receiving answers) regarding c#. It's not a place for articles regarding aspects of a language nor is it a place for advertising.

    Cheers!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

Featured