Results 1 to 6 of 6
-
2017-10-04, 05:34 PM (ISO 8601)
- Join Date
- Aug 2009
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 } }
Code:[...] int myInt = Utils.MyIntFunction(42); [...]
-
2017-10-04, 06:16 PM (ISO 8601)
- Join Date
- Aug 2005
- Location
- Mountain View, CA
- Gender
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();
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:
SpoilerSaberhagen'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)
-
2017-10-04, 06:35 PM (ISO 8601)
- Join Date
- Aug 2009
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); } }
Code:using Mathf //I know this doesn't work public class myClass { public int myFunc(int param) { return Max(10, param); //calls Mathf.Max } }
-
2017-10-04, 07:26 PM (ISO 8601)
- Join Date
- Aug 2005
- Location
- Mountain View, CA
- Gender
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; } }
Code:int sum = 10.Add(12); // calls Utils.Add(10, 12)
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:
SpoilerSaberhagen'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)
-
2017-10-05, 03:44 AM (ISO 8601)
- Join Date
- Feb 2007
- Location
- Manchester, UK
- Gender
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.
-
2017-10-05, 10:13 PM (ISO 8601)
- Join Date
- Apr 2008
- Location
- The Forest Moon of Ohio
- Gender
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.