Results 1 to 6 of 6

Thread: VB.Net - Tips on creating a function library - Shared function? No instancing!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2022
    Posts
    26

    VB.Net - Tips on creating a function library - Shared function? No instancing!

    Hi,

    I am simply trying to create a library of common functions I've written. I used the 'Class Library' template project.

    But I'm running into issues with member accessibility. Because I DON'T want to instance this library, as its purely a namespace, I was making class members (functions) 'SHARED' for the time being (for testing).
    But this can't be the way a namespace is written. Now I have problems accessing private members in the class from within a shared function.

    What is the way to approach this?

    Code:
    Namespace CustomFunctions
    
        Public Class Str  'there are mutliple classes within this namespace to organize the entire library
    
            Shared Function HasAlpha(ByVal Str As String) As Boolean
                'check for alphabetic characters
                For i = 0 To Str.Length - 1
                    If Char.IsLetter(Str.Chars(i)) Then
                        Return True
                    End If
                Next
                Return False
            End Function
            Shared Function IsNumeric(ByVal Str As String) As Boolean
                'check if only numbers
                For i = 0 To Str.Length - 1
                    If Not Char.IsNumber(Str.Chars(i)) Then
                        Return False
                    End If
                Next
                Return True
            End Function
    
            Shared Function CountChar(Str As String, Character As String) As Integer
                'count characters in string
                Dim count As Long
                For Each c As Char In Str
                    If c = Character Then
                        count = count + 1
                    End If
                Next
                Return count
            End Function
    
        End Class
    End Namespace

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VB.Net - Tips on creating a function library - Shared function? No instancing!

    Namespaces are simply a logical organization tool for classes. You don't "write a namespace"...

    As for the shared functions .... yep, that's the way you do it. You create a class and put in functions... private, public, or shared. Sounds like shared is what you want.
    As for accessing private variables, you shouldn't have problems. Except that you swerved when comes to your parameters... you gave them the same name as your class. So when you do Str.Char ... it doesn't know what Str you're referring to. You'e ocnfusedit.

    Give your functios meaningful names ... StringToCount, CharCount, CharToCount, SringToCheck. StrToCheck, Str2Chk...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2022
    Posts
    26

    Re: VB.Net - Tips on creating a function library - Shared function? No instancing!

    Hi Thanks for the tips.

    I didn't catch that my class name conflicted with my input parameters. Fixed.
    As for semantics: you don't write classes either, they're just a logical organization tool for code.

    Nevertheless, I am still having issues trying to access a private member from a shared function without instantiating the object. As described in the code below:

    Code:
    Namespace CustomFunctions
    
        Public Class StringFunctions
            Shared Function HasAlpha(ByVal Str As String) As Boolean
                'check for alphabetic characters
                For i = 0 To Str.Length - 1
                    If Char.IsLetter(Str.Chars(i)) Then
                        Return True
                    End If
                Next
                Return False
            End Function
            Shared Function IsNumeric(ByVal Str As String) As Boolean
                'check if only numbers
                For i = 0 To Str.Length - 1
                    If Not Char.IsNumber(Str.Chars(i)) Then
                        Return False
                    End If
                Next
                Return True
    
            End Function
    
            Shared Function CountChar(Str As String, Character As String) As Integer
                'count characters in string
                Dim count As Long
                For Each c As Char In Str
                    If c = Character Then
                        count = count + 1
                    End If
                Next
                Return count
            End Function
    
            Shared Function Test_The_Test() As String
                Return Test("Hello World")  'not allowed without an instance of the class?
            End Function
    
    
            Private Function Test(InputString As String) As String
                Return InputString
            End Function
    
        End Class
    End Namespace
    Exception:

    Code:
    Error	BC30369	Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: VB.Net - Tips on creating a function library - Shared function? No instancing!

    Should be:

    Code:
    Private Shared Function Test
    I believe pretty much everything accessed as a non-instance needs to be "Shared", which doesn't imply that it is externally visible.

    From https://learn.microsoft.com/en-us/do...difiers/shared :

    "Sharing does not alter the access level of a member. For example, a class member can be shared and private (accessible only from within the class), or non-shared and public."

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2022
    Posts
    26

    Re: VB.Net - Tips on creating a function library - Shared function? No instancing!

    Hi,

    Yes. I just realized this. I wasn't aware a function could be shared and private. Great!

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: VB.Net - Tips on creating a function library - Shared function? No instancing!

    If this is all in a class library, you might want to use Friend rather than Private. Friend is public...but only in the same assembly, which means just in the class library, in this case.
    My usual boring signature: Nothing

Tags for this Thread

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