New OOTS products from CafePress
New OOTS t-shirts, ornaments, mugs, bags, and more
Results 1 to 6 of 6
  1. - Top - End - #1
    Ogre in the Playground
    Join Date
    Aug 2009

    Default Programming question: function library in C#

    Yes, I know, C# doesn't do function libraries.
    Yes, I also know that you can emulate a function library by creating a static class with the functions as members, like this:
    Code:
    public static class Utils {
        public static int MyIntFunction(int coolParam)
        {
             //do stuff
        }
    }
    This issue I have with this approach is that I always have to type out the name of the static class:
    Code:
    [...]
         int myInt = Utils.MyIntFunction(42);
    [...]
    Is there any way so that I don't have to type out Utils all the time?

  2. - Top - End - #2
    Surgebinder in the Playground Moderator
     
    Douglas's Avatar

    Join Date
    Aug 2005
    Location
    Mountain View, CA
    Gender
    Male

    Default Re: Programming question: function library in C#

    Would it make semantic sense, for whatever the name and purpose of your function actually is, to call it like this?
    Code:
    int myInt = 42.MyIntFunction();
    If so, you could define it as an extension method. All it would take is adding "this" in front of the function's first parameter, so your example would become
    Code:
    public static class Utils {
        public static int MyIntFunction(this int coolParam)
        {
             //do stuff
        }
    }
    Last edited by Douglas; 2017-10-04 at 06:16 PM.
    Like 4X (aka Civilization-like) gaming? Know programming? Interested in game development? Take a look.

    Avatar by Ceika.

    Archives:
    Spoiler
    Show
    Saberhagen's Twelve Swords, some homebrew artifacts for 3.5 (please comment)
    Isstinen Tonche for ECL 74 playtesting.
    Team Solars: Powergaming beyond your wildest imagining, without infinite loops or epic. Yes, the DM asked for it.
    Arcane Swordsage: Making it actually work (homebrew)

  3. - Top - End - #3
    Ogre in the Playground
    Join Date
    Aug 2009

    Default Re: Programming question: function library in C#

    I'm not sure if I understand.

    In other languages like C++ or Delphi you can create namespaces/units/whatever that simply define methods. Then by declaring that you use the namespace you can just call the method. While in C#, if you want to call, say, Mathf.Max you have to do it like this:

    Code:
    public class myClass {
       public int myFunc(int param)
       {
          return Mathf.Max(10, param);
       }
    }
    My question is if it is possible to arrive at something like this:
    Code:
    using Mathf //I know this doesn't work
    
    public class myClass {
       public int myFunc(int param)
       {
          return Max(10, param); //calls Mathf.Max
       }
    }

  4. - Top - End - #4
    Surgebinder in the Playground Moderator
     
    Douglas's Avatar

    Join Date
    Aug 2005
    Location
    Mountain View, CA
    Gender
    Male

    Default Re: Programming question: function library in C#

    I was suggesting a possible alternative. In C# if you define a function like this:
    Code:
    public static class Utils {
        public static int Add(this int i, int j) {
            return i + j;
        }
    }
    Then you can call it like this:
    Code:
    int sum = 10.Add(12); // calls Utils.Add(10, 12)
    I just did a little bit of searching, though, and exactly what you asked is actually possible.

    Code:
    using static Mathf; // This does work
    
    public class myClass {
       public int myFunc(int param)
       {
          return Max(10, param); //calls Mathf.Max
       }
    }
    Like 4X (aka Civilization-like) gaming? Know programming? Interested in game development? Take a look.

    Avatar by Ceika.

    Archives:
    Spoiler
    Show
    Saberhagen's Twelve Swords, some homebrew artifacts for 3.5 (please comment)
    Isstinen Tonche for ECL 74 playtesting.
    Team Solars: Powergaming beyond your wildest imagining, without infinite loops or epic. Yes, the DM asked for it.
    Arcane Swordsage: Making it actually work (homebrew)

  5. - Top - End - #5
    Colossus in the Playground
     
    BlackDragon

    Join Date
    Feb 2007
    Location
    Manchester, UK
    Gender
    Male

    Default Re: Programming question: function library in C#

    Yeah, I was about to suggest the "using" directive--you can specify any class with that and it will allow you to use functions from that class without qualification. It's usually used for system classes in the .NET framework but you can use it for your own as well.

  6. - Top - End - #6
    Barbarian in the Playground
    Join Date
    Apr 2008
    Location
    The Forest Moon of Ohio
    Gender
    Male

    Default Re: Programming question: function library in C#

    The "using" statement will work for bringing in references like that. The thing to watch out for is overlap with other namespaces that you might want to use in the project, depending on the environment you are coding in you may get a warning that about an ambiguous namespace or reference if you have another reference that has a function with the same name.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •