Hi
In below code i have created some functions. I want them to be in one place. What should i do
C#
public static bool IsAlphaNumericWithUnderscore(string input)
{
return Regex.IsMatch(input, "^[a-zA-Z0-9_]+$");
}
public static bool IsAllLetters(string s)
{
foreach (char c in s)
{
if (!Char.IsLetter(c))
return false;
}
return true;
}Thanks

Vishal JoshiRank #377 · 6.7K reputationPosted Aug 16, 2024, 1:08 AM EDT
Hello Ramco,
You can put all the common functions in one place based on the requirement or usage.
For example, if you have a common function for any specific page then you can create class inside business logic and put the common function there.
If functions are globally used then you can create a class at global level of application and use it.
If it's common between different application/projects you can create a class library and use them by adding a library referance.
As per you code you can create class and user the function like below sample code
CommanFunction.cs
public static class CommonFunction { public static bool IsAlphaNumericWithUnderscore(string input) { return Regex.IsMatch(input, "^[a-zA-Z0-9_]+$"); } public static bool IsAllLetters(string s) { foreach (char c in s) { if (!Char.IsLetter(c)) return false; } return true; } }YourClass.cs
using System; namespace YourApplication { class Program { static void Main(string[] args) { CommanFunction.IsAlphaNumericWithUnderscore("Input Parameter value"); } } }Thanks
Vishal Joshi
Vishal JoshiRank #377 · 6.7K reputationPosted Aug 23, 2024, 8:04 AM EDT
Hello Ramco,
Use of static keyword is a single copy of the variable is created and shared among all objects at the class level. Static variables are accessed with the name of the class, they do not require any object for access.
In simple words we can say no need to create an object to access static variables or methods.
Thanks
Vishal Joshi
Sam HobbsRank #107 · 20.5K reputationPosted Aug 16, 2024, 12:11 AM EDT
I am sorry, I do not understand. What do you mean by one place?
Ramco RamcoRank #462 · 5.4K reputationPosted Aug 16, 2024, 12:23 AM EDT
Hi
I want all functions at one place. Can it be done using class or some other process.
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Aug 16, 2024, 1:14 AM EDT
Hi Vishal Joshi
What is the importance of Static keyword.
Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted Aug 24, 2024, 7:35 AM EDT
Hello Ramco,
You can create a single static class containing all your common functions and use that class throughout your entire project.
using System; using System.Text.RegularExpressions; namespace YourNamespace { public static class Utility { public static bool IsAlphaNumericWithUnderscore(string input) { if (string.IsNullOrEmpty(input)) return false; return Regex.IsMatch(input, "^[a-zA-Z0-9_]+$"); } public static bool IsAllLetters(string s) { if (string.IsNullOrEmpty(s)) return false; foreach (char c in s) { if (!Char.IsLetter(c)) return false; } return true; } } }You can use the above class as below,
bool result1 = Utility.IsAlphaNumericWithUnderscore("test_123"); bool result2 = Utility.IsAllLetters("HelloWorld");Aman GuptaRank #193 · 12.4K reputationPosted Aug 24, 2024, 5:06 AM EDT
Hi Ramco,
To consolidate your functions into one place in C#, you can create a utility class specifically for string operations. This way, you keep related methods together, improving code organization and reusability.
Here’s how you can do it:
Step 1: Create a Utility ClassCreate a new class, typically named something like
StringUtilitiesorStringHelper, to house these methods.using System; using System.Text.RegularExpressions; public static class StringUtilities { public static bool IsAlphaNumericWithUnderscore(string input) { return Regex.IsMatch(input, "^[a-zA-Z0-9_]+$"); } public static bool IsAllLetters(string s) { foreach (char c in s) { if (!Char.IsLetter(c)) return false; } return true; } }Whenever you need to use these functions, you simply call them through the
StringUtilitiesclass.bool result1 = StringUtilities.IsAlphaNumericWithUnderscore("Example_123"); bool result2 = StringUtilities.IsAllLetters("Example");- Code Organization: Keeps related methods in one place, making it easier to manage and maintain.
- Reusability: Allows for reuse of the utility methods across different parts of your application.
- Encapsulation: Provides a single point of modification if the logic in these methods needs to be updated.
Additional Considerations:StringUtilitiesis in a namespace that is accessible where you need to use it. You might need to include ausingdirective if it’s in a different namespace.By following this approach, you centralize your string utility methods, making your codebase cleaner and more maintainable.
Chang Kook14 reputationPosted Oct 29, 2024, 12:33 AM EDT
simply update when b is true else do nothing