# Extension methods: Adding methods to classes without deriving (with a simple StringTheory example)

**URL:** <https://clarionhub.com/t/extension-methods-adding-methods-to-classes-without-deriving-with-a-simple-stringtheory-example/7389>\
**Category:** code\
**Tags:** class, stringtheory\
**Created:** [August 11, 2024, 8:38pm UTC](https://clarionhub.com/t/extension-methods-adding-methods-to-classes-without-deriving-with-a-simple-stringtheory-example/7389 "2024-08-11T20:38:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![CarlosGtrz](https://clarionhub.com/user_avatar/clarionhub.com/carlosgtrz/32/5870_2.png) [@CarlosGtrz](https://clarionhub.com/u/CarlosGtrz)\
**Post date:** [August 11, 2024, 8:38pm UTC](https://clarionhub.com/t/extension-methods-adding-methods-to-classes-without-deriving-with-a-simple-stringtheory-example/7389/1 "2024-08-11T20:38:00Z")

</div>

While coding a recent project I wrote something like this to call [StringTheory](https://www.capesoft.com/accessories/StringTheorysp.htm) to [Append](https://www.capesoft.com/docs/StringTheory3/StringTheory.htm#stAppend) a Value with a `' '` Space Separator before the Value:

```auto
st.Append(ValueOne,st:clip,' ')
st.Append(ValueTwo,st:clip,' ')
st.Append(ValueThree,st:clip,' ')

```

It works perfectly fine, but it adds some repetitive code that hurts a little the code readability.

Trying to simplify this, I wrote some tests, and after getting a compiler error I recalled that for the compiler it's the same to call `SomeProc(object)` or `object.SomeProc()`.

This means we can declare global procedures that can add methods to classes without creating a derived class, like [C# extension methods](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods).

For example, the above code can be written as:

```auto
st.AppSp(ValueOne) !Append with clip and space separator before
st.AppSp(ValueTwo)
st.AppSp(ValueThree)

```

by globally declaring something like:

```auto
  MAP
AppSp PROCEDURE(StringTheory pSt,STRING pText) !Append with clip and space separator before
  .

AppSp PROCEDURE(StringTheory pSt,STRING pText)
  CODE 
  pSt.Append(pText,st:clip,' ')

```

and the new method will be available for all instances of `StringTheory`. Here's a [test project on github](https://github.com/CarlosGtrz/ExtendStringTheory).

Of course, this is only trivial syntactic sugar, but it may be used as a simple way to add custom functionality to third party or ABC classes.

Hope you find it interesting.

Carlos

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [August 11, 2024, 9:46pm UTC](https://clarionhub.com/t/extension-methods-adding-methods-to-classes-without-deriving-with-a-simple-stringtheory-example/7389/2 "2024-08-11T21:46:33Z")

</div>

You can also use this to create private methods that don’t get published in the inc file.

> <https://github.com/jslarve/CSVParseClass/blob/f88bb0047ebee45ca9f0354fb707c3feff0447c9/JSCSVParseClass.clw#L50>
