How to reference function in shared function?

B

Brett

I'm trying to use the F1 function inside of F2 function below. I keep
getting the error posted below the code. If I remove the Shared
declaration from F2, it works fine. What exactly does the error mean?

Public Class myClass

Function F1(ByVal url As String) As Struct1
-- do something --
End Function

Public Shared Function F2(ByVal value2 As String) As String
F1(SomeValue) 'error references this line
-- do something --
End Function

End Class

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.

I also tried this below the class declaration:

Public F1_ As new myClass
F1_.F1()

That gives an error saying 'Declaration Expected' on F1_, anywhere I try to
use it. Any suggestions on how I can reference F1 in F2?

Thanks,
Brett
 
A

alejandro lapeyre

Because you need an instance of the class to call a non-shared function.

Shared functions are like a global function, you can call them like a
function you define in a form.

You can not call F1 directly without creating an instance of the class.
Neither can F2.

best regards,
Alejandro Lapeyre
 
G

Guest

Hi Brett,

you have to instance the class before.

Furthermore, if the F1 function is used inside of this class, I will put it
as private.

The code will be similar to:

Dim MClass As New Class1
MessageBox.Show(MClass.F2("sample"))


Public Class Class1

Private Function F1(ByVal url As String) As String
Return url + "lalala "
End Function

Public Shared Function F2(ByVal value2 As String) As String
Dim MyF1 As New Class1
Return MyF1.F1("something ") + value2
End Function

End Class


Kind Regards,

Jorge Serrano Pérez
MVP VB.NET
 
H

Herfried K. Wagner [MVP]

Brett said:
I'm trying to use the F1 function inside of F2 function below. I keep
getting the error posted below the code. If I remove the Shared
declaration from F2, it works fine. What exactly does the error mean?

Public Class myClass

In addition to the other replies: Note that 'MyClass' is a keyword and thus
cannot be used as an identifier. You can work around that by putting
'MyClass' into square brackets:

\\\
Public Class [MyClass]
...
///
 
B

Brett

If I define a variable in the class such as
Private DontHardCodeThisValue As String = "This is not hard coded"

Then, in the same class for the shared function, I'll need to create an
instance of the class correct?
Public Shared Function F2(ByVal value2 As String) As String
Dim MyVariable As New Class1
Return MyVariable .DontHardCodeThisValue
End Function

Now, in Form1 I also create an instance of Class1 to have a reference to F2.
Doing this will create two instances of Class1 correct? One in Form1 and
the one created in F2. Is this good practice? How else can it be done?

Thanks,
Brett

"Jorge Serrano [MVP VB]"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top