|
-
Dec 13th, 2022, 06:12 PM
#1
Thread Starter
Junior Member
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
-
Dec 13th, 2022, 07:51 PM
#2
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
-
Dec 13th, 2022, 09:19 PM
#3
Thread Starter
Junior Member
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.
-
Dec 13th, 2022, 09:29 PM
#4
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."
-
Dec 13th, 2022, 09:31 PM
#5
Thread Starter
Junior Member
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!
-
Dec 14th, 2022, 09:39 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|