problem with module

A

André

Hi,

I wrote this module in file "module.vb" and put it in App_Code of my asp.net
application.

Imports Microsoft.VisualBasic
Public Module Mymodule
Sub Main()
MyFunction()
End Sub

Function MyFunction()
Return "function in module"
End Function
End Module

From code-behind, i do:
----------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Label1.Text = Mymodule.MyFunction
Label1.Text = Mymodule.Main
End Sub

The first line gives the correct output, but the second line produces error
"Expression does not produce a value."

I thought: sub Main() starts function Myfunction which renders the string
"function in module" ...
By the way, is it possible to place the module in code-behind?

Thanks for help
André
 
B

Branco Medeiros

André wrote:
Sub Main()
MyFunction()
End Sub
Label1.Text = Mymodule.Main
<snip>

You can't assign the result of a Sub to anything, because a Sub
doesn't return values. Only functions and properties do. Thus the
compilation error.

(in the case of your call to MyFunction inside Main, its returned
value is silently discarded)

HTH.

Regards,

Branco.
 
A

André

Hi, thanks for replying.
So, the Sub Main() is completely useless?
And is it possible to put the code in code-behind, or it must be put, like a
class, in a .vb file?

Thanks again
 
B

Branco Medeiros

André wrote:
So, the Sub Main() is completely useless?

Nope. I mean, it *is not* useless.

See, Subs, in general, are methods that do things, from which you
don't expect a result. Functions, on the other side, usually calculate
things and return a result, although most languages (including VB)
allow you to implicitly discard the Function's result and call the
Function as if it was a Sub -- for what we call "the function's side-
effects" (notice, however, that a Sub *can't* be used as a Function.
Could you guess why?). Properties allow you access to the internal
state of an object. A Property is like the mixing of a Sub to *set*
the value with a function to *get* it back (that's why properties are
usually called, in some languages, getters and setters).

Now, Sub Main has, for historical reasons, the job of marking the
entry point of the "application". It's the first method to be called
by the framework when it is about to execute your code.

To be used like this, the Sub Main has to be a Shared method in a
class, or it must be declared in a Module. And, of course, you must
indicate to the compiler which Sub Main is *the* main sub (usually in
the application properties, if you're using the MS IDEs).
And is it possible to put the code in code-behind, or it must be put, like a
class, in a .vb file?
<snip>

Unfortunately I have no idea of what you're asking. Maybe you can
state your question more clearly...

HTH.

regards,

Branco.
 
A

André

Thanks again,

I suppressed the Main() sub and it works.
I don't know whether it's relevant, but i use VB.net in the ASP.Net context,
so i don't think that Sub Main() is relevant here .... I remember that is
was indeed used in VB language.

About putting the code in code-behind or in a .vb file question, again it's
related to ASP.Net where all files containing classes must be placed in
subdir APP_Code. Code-behind is the VB.Net part of an aspx file ...




"Branco Medeiros" <[email protected]> schreef in bericht
André wrote:
So, the Sub Main() is completely useless?

Nope. I mean, it *is not* useless.

See, Subs, in general, are methods that do things, from which you
don't expect a result. Functions, on the other side, usually calculate
things and return a result, although most languages (including VB)
allow you to implicitly discard the Function's result and call the
Function as if it was a Sub -- for what we call "the function's side-
effects" (notice, however, that a Sub *can't* be used as a Function.
Could you guess why?). Properties allow you access to the internal
state of an object. A Property is like the mixing of a Sub to *set*
the value with a function to *get* it back (that's why properties are
usually called, in some languages, getters and setters).

Now, Sub Main has, for historical reasons, the job of marking the
entry point of the "application". It's the first method to be called
by the framework when it is about to execute your code.

To be used like this, the Sub Main has to be a Shared method in a
class, or it must be declared in a Module. And, of course, you must
indicate to the compiler which Sub Main is *the* main sub (usually in
the application properties, if you're using the MS IDEs).
And is it possible to put the code in code-behind, or it must be put, like
a
class, in a .vb file?
<snip>

Unfortunately I have no idea of what you're asking. Maybe you can
state your question more clearly...

HTH.

regards,

Branco.


"Branco Medeiros" <[email protected]> schreef in bericht
André wrote:
So, the Sub Main() is completely useless?

Nope. I mean, it *is not* useless.

See, Subs, in general, are methods that do things, from which you
don't expect a result. Functions, on the other side, usually calculate
things and return a result, although most languages (including VB)
allow you to implicitly discard the Function's result and call the
Function as if it was a Sub -- for what we call "the function's side-
effects" (notice, however, that a Sub *can't* be used as a Function.
Could you guess why?). Properties allow you access to the internal
state of an object. A Property is like the mixing of a Sub to *set*
the value with a function to *get* it back (that's why properties are
usually called, in some languages, getters and setters).

Now, Sub Main has, for historical reasons, the job of marking the
entry point of the "application". It's the first method to be called
by the framework when it is about to execute your code.

To be used like this, the Sub Main has to be a Shared method in a
class, or it must be declared in a Module. And, of course, you must
indicate to the compiler which Sub Main is *the* main sub (usually in
the application properties, if you're using the MS IDEs).
And is it possible to put the code in code-behind, or it must be put, like
a
class, in a .vb file?
<snip>

Unfortunately I have no idea of what you're asking. Maybe you can
state your question more clearly...

HTH.

regards,

Branco.
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top