What am I doing wrong?

R

Robert Dufour

I have two classes , class1 and class2

In class1 I have

Public Overload function Fct1(paramlist) as string
some code
end function

Public overloads Function Fct1(otherparamlist) as string
some code
end function

In class2 I have

Private sub use fct1
Dim str as string
str = class1.fct1(paramlist)
end sub

In the IDE fct1 in class two shows an error reference to a non-shared member
requires on object reference


What am I doing wrong? Can anyone point me in the right direction?

Thanks for any help.
Bob
 
M

Mr. Arnold

Robert Dufour said:
I have two classes , class1 and class2

In class1 I have

Public Overload function Fct1(paramlist) as string
some code
end function

Public overloads Function Fct1(otherparamlist) as string
some code
end function

In class2 I have

Private sub use fct1
Dim str as string
str = class1.fct1(paramlist)
end sub

In the IDE fct1 in class two shows an > error reference to a non-shared
member requires on object reference

http://msdn2.microsoft.com/en-us/library/zwwhc0d0(vs.80).aspx
http://www.developerfusion.co.uk/show/1047/5/

You use the Shared keyword. You can further look it up use Google.

where you can do str = class1.fct1(paramlist)

Or

You have to instantiate and object to set reference.

dim cls1 = new class1

str = cls1.fct1(paramlist)
 
R

Robert Dufour

I am using 2005, the code worked in 2003 but now it fails when imported in
2005.
I guess they "improved" some some fundamentals again <GGGG>

Bob
 
P

Phill W.

Robert said:
In class1 I have

Public Overload function Fct1(paramlist) as string
some code
end function

Public overloads Function Fct1(otherparamlist) as string
some code
end function

In class2 I have

Private sub use fct1
Dim str as string
str = class1.fct1(paramlist)
end sub

In the IDE fct1 in class two shows an error reference to a non-shared member
requires on object reference
What am I doing wrong? Can anyone point me in the right direction?

Both Fct1()'s are /Instance/ methods - you need to create an instance of
the class that defines them before you can use the methods.

Private Sub Use_fct1()
Dim c1 As New Class1
Dim s2 As String _
= c1.Fct1(...
End Sub

Compare this to the IndexOf() on the String class - in order to search
one string for another, you have to start with the string you want to
search.

Dim s1 As String = "abcdefghijklmnop"
Dim i1 As Integer = s1.IndexOf( "d" )

To use the syntax you started out with, you have to make the methods
"Shared" (you'll see "Static" in C# examples).

Class Class1
Public Shared Sub fct1( ...

then you can do this:

Private Sub Use_fct1()
Dim s2 As String _
= Class1.fct1(...
End Sub

Compare this to the Join method on the String class. You can use
any-old string to join a string array using a delimiter you don't need a
/specific/ string, so /any/ of these will do:

Dim s1() As String = ...
Dim s2 As String

' Use the class name directly
s2 = [String].Join( vbCrLf, s1 )

' Instances get (inherit) all the Shared methods as well
s2 = s2.Join( vbCrLf, s1)

' And, even more strangely ... :)
s2 = "fred".Join( vbCrLf, s1)

HTH,
Phill W.
 

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