Help: I'm new to C#: I want to make a global function
-
Hi As you may be able to tell I am a bit new to C# and I may be attacking this from the wrong angle. I would like to create global function for the program I am writing. I would like to group these functions together for ease i.e. Customer related functions, general function but enable them to be used by other bits of code. I can do this fine in VB.NET but cannot get them to work in C#. For Example I would like a function that returns a string that is stripped of all punctuation and is in upper case. This would be a general function and I would like it in its own code file so I can group it together with other ones. I've had a look in my book and cannot find anything that helps. Is this more of a VB thing and is done differently in C# or am I missing something. ps I'm using C# 2005 Express Edition. Many Thanks
The FoZ
-
Hi As you may be able to tell I am a bit new to C# and I may be attacking this from the wrong angle. I would like to create global function for the program I am writing. I would like to group these functions together for ease i.e. Customer related functions, general function but enable them to be used by other bits of code. I can do this fine in VB.NET but cannot get them to work in C#. For Example I would like a function that returns a string that is stripped of all punctuation and is in upper case. This would be a general function and I would like it in its own code file so I can group it together with other ones. I've had a look in my book and cannot find anything that helps. Is this more of a VB thing and is done differently in C# or am I missing something. ps I'm using C# 2005 Express Edition. Many Thanks
The FoZ
Customer-related functions should be instance methods on the Customer class. For things that don't require an instance, make them static. For example,
public class MyGlobalFuncs
{
public static void Foo() { }
}That can be invoked like this:
MyGlobalFuncs.Foo()
Life, family, faith: Give me a visit. From my latest post: "A lot of Christians struggle, perhaps at a subconscious level, about the phrase "God of Israel". After all, Israel's God is the God of Judaism, is He not? And the God of Christianity is not the God of Judaism, right?" Judah Himango
-
Customer-related functions should be instance methods on the Customer class. For things that don't require an instance, make them static. For example,
public class MyGlobalFuncs
{
public static void Foo() { }
}That can be invoked like this:
MyGlobalFuncs.Foo()
Life, family, faith: Give me a visit. From my latest post: "A lot of Christians struggle, perhaps at a subconscious level, about the phrase "God of Israel". After all, Israel's God is the God of Judaism, is He not? And the God of Christianity is not the God of Judaism, right?" Judah Himango
-
Hi As you may be able to tell I am a bit new to C# and I may be attacking this from the wrong angle. I would like to create global function for the program I am writing. I would like to group these functions together for ease i.e. Customer related functions, general function but enable them to be used by other bits of code. I can do this fine in VB.NET but cannot get them to work in C#. For Example I would like a function that returns a string that is stripped of all punctuation and is in upper case. This would be a general function and I would like it in its own code file so I can group it together with other ones. I've had a look in my book and cannot find anything that helps. Is this more of a VB thing and is done differently in C# or am I missing something. ps I'm using C# 2005 Express Edition. Many Thanks
The FoZ
TheFoZ wrote:
I would like to create global function
That is not possible. You can use static methods, as Judah suggested, but they are not global.
TheFoZ wrote:
I can do this fine in VB.NET
No, you can't. VB.NET doesn't have any global functions either. If you mean a module, that is just a class where all members automatically become static.
Despite everything, the person most likely to be fooling you next is yourself.