473,387 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to reference function in shared function?

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
Nov 21 '05 #1
4 15822
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

"Brett" <no@spam.net> escribió en el mensaje
news:OY**************@tk2msftngp13.phx.gbl...
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

Nov 21 '05 #2
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

Nov 21 '05 #3
"Brett" <no@spam.net> schrieb:
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]
...
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
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]"
<NO*******************@NOQUIEROSPAMportalvbNOSPAM. com.NOQUIEROSPAM> wrote in
message news:31**********************************@microsof t.com...
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

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chandy | last post by:
Hi, I have a public shared function in a utility class full of other public shared functions. It does not get hit that often but when it does the hit will be long-ish and involve potentially...
8
by: ABC | last post by:
In VB, we can write the share function as Public Shared Function FnXXX() .... .... End Function How about on C#?
4
by: Matt | last post by:
I'm trying to write a shared function that will convert all my phone numbers from format 1234567890 to (123) 456-7890. Here is my function that I wrote: Public Shared Function...
4
by: hansiman | last post by:
isn't it possible to call a public shared function that resides in another file directly from the datagrid html code?: <asp:TemplateColumn HeaderText="Password"> <ItemTemplate> <asp:Label...
1
by: Edward | last post by:
I have trouble with some of the concepts of OOP, and am struggling currently with Private Shared Functions. I think I understand Private (not available outside the class). I think I understand...
2
by: Hayrob | last post by:
I have extracted the code fragment below. The variables do not appear to be set, which is not what I expected. I can see it must be a problem with a Shared method and an instance object, but I...
4
by: Chris | last post by:
Hello, I'm just getting started with VB and am new to the group, so please excuse what may seem to be a rudimentary question. I've been writing basic programs and have noticed that the...
2
by: Aaron Cutlip | last post by:
I have been looking all over and have seen many possible ways to create a Syncronized Shared Function in VB.NET, but I would like some advice that will make my life easier. Given the following...
1
by: =?Utf-8?B?am9uZWZlcg==?= | last post by:
Is it possible to use Server.MapPath in a Public Shared Function? Using the following code in this context - shows "Sever" as not declared - Public Shared Function Back_End_State() As Integer...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.