RE: How to reference function in shared function?

From: Jorge Serrano [MVP VB] (NOQUIEROSPAMwebmaster_at_NOQUIEROSPAMportalvbNOSPAM.com.NOQUIEROSPAM)
Date: 02/12/05


Date: Sat, 12 Feb 2005 00:05:02 -0800

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

"Brett" wrote:

> 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
>
>
>



Relevant Pages

  • Re: How to reference function in shared function?
    ... Shared functions are like a global function, you can call them like a ... > Public Class myClass ... > or shared member initializer without an explicit instance of the class. ... > I also tried this below the class declaration: ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Shared Subs?
    ... Your class definition needs to go OUTSIDE the first public class declaration ... > Sub Application_Start ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: question relates to instance variable initialization
    ... I am unclear about the following two ways to initialize instance ... public class MyClass ...
    (comp.lang.java.programmer)
  • Re: XML Serializer
    ... CDATA to escape special characters, ... semantically, the meaning of XML remains exactly the same, and, unless ... public class MyClass ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Initializing variable style question
    ... public class MyClass ...     public MyClass ... To initialize to a non-zero-like value, ...
    (comp.lang.java.programmer)