Shared functions

W

wally.anchor

Hi.

I'm getting myself rather confused here, so hopefully somebody can
help me.

I want to put a fairly general function somewhere I can access it from
any class in my project. It's not something that depends on a
particular instance of any object and so can just be called to return
a value/object.

When I have my function within the class I'm calling it from it works
fine.
However I need to be able to call it from other classes too. Since it
doesn't depend on the class I'm working with I thought I could put it
in it's own class (named say GeneralClass), make it a "shared"
function (GetData()), and I should be able to call like
GeneralClass.GetData()

It doesn't work though. I get an compile time error: Expression does
not produce a value.
I know it does produce a value as it works fine when it's sitting in
the same class that I'm calling it from.

Have I just got my idea of shared functions all messed up? I know
these lookup/general functions should not need to be tied to a
particular class so I must have something wrong.

A (very simplified) example:

Public Class Form1
Public Sub New()
Dim dt as DataTable
dt = GetData()
End Sub

Public Function GetData() As DataTable
Dim table As DataTable = New DataTable()
'build table
Return table
End Function
End Class

This works fine. dt gets populated with data.

However if I break it into two classes, and declare GetData() as
Shared:

Public Class Form1
Public Sub New()
Dim dt as DataTable
dt = GeneralClass.GetData()
End Sub
End Class

Public Class GeneralClass
Public Shared Function GetData() As DataTable
Dim table As DataTable = New DataTable()
'build table
Return table
End Function
End Class

......this doesn't work. At compile time I get an error on the
dt = GeneralClass.GetData()
line, for GeneralClass saying "Expression does not produce a value"


What am I missing here? Should I be using something other than a
Shared Function?

Cheers
Wally
 
W

wally.anchor

Duh!
Ignore my post.

It seems the class name I used coincided with a Sub that I could also
access, hence the "Expression does not produce a value" error.

Changed the name of the class and now all works as expected.

:)

Cheers
Wally
 

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