Results 1 to 4 of 4

Thread: Where to put general procedures in C#

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2010
    Posts
    772

    Where to put general procedures in C#

    I have some C# procedures that are needed in several forms in the project.
    So I cannot put them in the code of a specific form.
    Where do I put them?

    In VB6, I could easily add these procedures in a bas module.
    But, what is the equivalent of that in C#?
    And how do I add it to the project?
    And where (in what file) do I save it?

    Also, what if there are some procedures that are so generic that all projects may need to call them?
    Where do I put those ones?

    Please advise.
    Thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,468

    Re: Where to put general procedures in C#

    Basically everything in .NET is a class. Forms are classes, just like everything else. Just create one or more classes and put your methods there. You already use classes like that from the framework, e.g. the System.IO.File class for operations relating to files. If it makes sense to do so, you can declare you class and its members static and then you can use it without having to create an instance, exactly as you do with the System.IO.File and various other classes. For the record, VB.NET has modules that are implemented to work in basically the same way that modules do in VB6 but they compile to the same things as C# static classes.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,803

    Re: Where to put general procedures in C#

    For example, let's assume in your VB6 application you had a module named Utilities that had a slugify method in it that turned plain text values into slugs. That might look something like this:
    Code:
    Module Utilities
        Function Slugify(ByVal strInput As String) As String
            Dim strSlug As String
            Dim i As Integer
            Dim chrCurrent As String
    
            strSlug = LCase$(strInput)
    
            For i = 1 To Len(strSlug)
                chrCurrent = Mid$(strSlug, i, 1)
    
                If (chrCurrent >= "a" And chrCurrent <= "z") Or (chrCurrent >= "0" And chrCurrent <= "9") Then
                    strSlug = strSlug & chrCurrent
                ElseIf chrCurrent = " " Then
                    strSlug = strSlug & "-"
                End If
            Next i
    
            Slugify = strSlug
        End Function
    End Module
    Now whenever you needed to reference the code, you'd execute the following:
    Code:
    Dim slug As String
    slug = Slugify("Hello World")
    The C# equivalent would be to create a class and make your method static which allows you to reference the member by using the class name itself:
    Code:
    public partial class Utilities
    {
        [GeneratedRegex(@"[^a-z0-9\s-]")]
        private static partial Regex RegexSlugifyMatchNonValidCharacters();
    
        [GeneratedRegex(@"\s+")]
        private static partial Regex RegexSlufigyMatchDuplicateSpaces();
    
        [GeneratedRegex(@"\s")]
        private static partial Regex RegexSlugifyMatchSingleSpaces();
    
        [GeneratedRegex(@"-+")]
        private static partial Regex RegexSlugifyMatchDuplicateHyphens();
    
        public static string Slugify(string text)
        {
            var slug = text.ToLowerInvariant();
            slug = RegexSlugifyMatchNonValidCharacters().Replace(slug, "");
            slug = RegexSlufigyMatchDuplicateSpaces().Replace(slug, " ").Trim();
            slug = RegexSlugifyMatchSingleSpaces().Replace(slug, "-");
            slug = RegexSlugifyMatchDuplicateHyphens().Replace(slug, "-");
            slug = slug.Trim('-');
    
            return slug;
        }
    
    }

    Now whenever you needed to reference the code, it'd look like this:
    Code:
    var slug = Utilities.Slugify("Hello World");
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    652

    Re: Where to put general procedures in C#

    I think jmcilhinney and dday9 have give good usfull answers but I thought I share a link for you that may also help while moving from VB6 to c# hope it may have you

    https://www.vbmigration.com/white-pa...ation-partner/

Posting Permissions

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



Click Here to Expand Forum to Full Width