Hi
In below code i have created some functions. I want them to be in one place. What should i do
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 JoshiPosted Aug 16, 2024, 5:08 AM
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
YourClass.cs
Thanks
Vishal Joshi
Chang KookPosted Oct 29, 2024, 4:33 AM
simply update when b is true else do nothing
Jignesh KumarPosted Aug 24, 2024, 11:35 AM
Hello Ramco,
You can create a single static class containing all your common functions and use that class throughout your entire project.
You can use the above class as below,
Aman GuptaPosted Aug 24, 2024, 9:06 AM
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
Step 2: Use the Utility ClassStringUtilitiesorStringHelper, to house these methods.Whenever you need to use these functions, you simply call them through the
Benefits:StringUtilitiesclass.- 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.
Vishal JoshiPosted Aug 23, 2024, 12:04 PM
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
Ramco RamcoPosted Aug 16, 2024, 5:14 AM
Hi Vishal Joshi
What is the importance of Static keyword.
Thanks
Ramco RamcoPosted Aug 16, 2024, 4:23 AM
Hi
I want all functions at one place. Can it be done using class or some other process.
Thanks
Sam HobbsPosted Aug 16, 2024, 4:11 AM
I am sorry, I do not understand. What do you mean by one place?