Newbie question

  • Thread starter Thread starter David Sawyer
  • Start date Start date
D

David Sawyer

Being new to VB I have a question.
I have written a program but now I want to split up some
of the sub routines as modules.
How would I, for example, call a module named module1 that
it's only function is to build a datagrid table. Can
someone give me a code example?

Thanks
 
Hi David,

A Module is a Class for which you don't need use the name when calling its
routines. But you may if you wish..

==================================
Here's an example where the Module name is not used.

DataGridtable.vb
Public Module Whatever
Public Sub BuiildDataGridTable (...)
: : :
End Sub
End Module

SomewhereElse.vb
Public Sub Foo (...)
: : :
BuildDataGridTable (...)
: : :
End Sub

==================================
Here's the same example where the Module name is used.

DataGridtable.vb
Public Module DataGridTable
Public Sub Buiild (...)
: : :
End Sub
End Module

SomewhereElse.vb
Public Sub Foo (...)
: : :
DataGridTable.Build (...)
: : :
End Sub

Strictly speaking you wouldn't need to say DataGridTable.Build(). You
could simply use Build() on its own. But if you use "Build" in another module,
then you'd need to qualify it with the module name too.

==================================
Here's almost the same example using a Class.

DataGridtable.vb
Public Shared Class DataGridTable
Public Shared Sub Buiild(...)
: : :
End Sub
End Class

SomewhereElse.vb
Public Sub Foo (...)
: : :
BuildDataGridTable (...)
: : :
End Sub

This time you <must> use the class name.

==================================
As you can see there's not a huge deal of difference. They all behave the
same way as used here. You can also see how what starts as a Module can become
a Class if it becomes useful to do so.

Regards,
Fergus
 
David Sawyer said:
Being new to VB I have a question.
I have written a program but now I want to split up some
of the sub routines as modules.
How would I, for example, call a module named module1 that
it's only function is to build a datagrid table. Can
someone give me a code example?

Maybe deriving your own class from DataGrid is the better approach.
 
Fergus Cooney said:
So show him, Armin. He's new!!

Do I have to? No. If he asks, I will - probably. Writing dozens of
paragraphs that might turn out to be superfluous is not my style.
 
Hello,

David Sawyer said:
Being new to VB I have a question.
I have written a program but now I want to split up some
of the sub routines as modules.
How would I, for example, call a module named module1 that
it's only function is to build a datagrid table. Can
someone give me a code example?

What's exactly the problem?

\\\
Public Module Foo
Public Sub Bar(ByVal d As DataGrid)

' Modify d here.
End Sub
End Module
///

In the form:

\\\
Foo.Bar(Me.DataGrid1)
///

Notice that it's maybe better to inherit from 'DataGrid' (or something
like that). The document linked below will show you the basics of OOP
in VB.NET:

Classes: Blueprints for Objects (Visual Basic Language Concepts)
http://msdn.microsoft.com/library/e...modulesputtingdatatypesprocedurestogether.asp
 
Thanks Fergus.
I have a main program called APRSDeCode and currently I
have a sub routine called setupAFDC
I would like to put setupAFDC as a module.
I add the module, name it to setupAFDC
When I attempt to set Public Module setupAFDC in my main
program (APRSDeCode) or even in the Module I get an
error "Expected: end of statement"
So what am I missing?
 
Hello,

Armin Zingler said:
Maybe deriving your own class from DataGrid is the better approach.

A really good answer. On the one hand it's really easy to write a
solution with a module ("bad" style), on the other hand it's hard for a
beginner to understand OOP concepts and write "good" code.

;-)
 
Hello,

David Sawyer said:
I have a main program called APRSDeCode and currently I
have a sub routine called setupAFDC
I would like to put setupAFDC as a module.
I add the module, name it to setupAFDC
When I attempt to set Public Module setupAFDC in my main
program (APRSDeCode) or even in the Module I get an
error "Expected: end of statement"
So what am I missing?

Where exactly do you insert the module's code? Notice that it's a bad
idea to give a module and a procedure the same name ('setupAFDC' in your
case).
 
Hi David,

"Expected: end of statement" (probably) means that you've got too much in
the declaration or statement and so while the compiler thinks it's finished
the line, there's more text waiting to be parsed.

But a general statement like that is hardly helpful to you, I'm sure. To
know what's wrong I'd have to see some code.

Regards,
Fergus
 
Armin Zingler said:
Do I have to? No. If he asks, I will - probably. Writing dozens of
paragraphs that might turn out to be superfluous is not my style.

...because in the past often it did turn out. It makes more sense to write
more after getting specific follow-up questions.
 
Herfried K. Wagner said:
A really good answer. On the one hand it's really easy to write a
solution with a module ("bad" style), on the other hand it's hard for
a beginner to understand OOP concepts and write "good" code.

;-)

I think one should use the OOP concepts right from the start. I wouldn't
tell a beginner that there are Modules at all - not before he knows what
classes and shared members are.
 
Hello,

Armin Zingler said:
Do I have to? No. If he asks, I will - probably. Writing dozens of
paragraphs that might turn out to be superfluous is not my style.

Fergus will call Jack if you don't post the code.

SCNR
 
Hello,

Armin Zingler said:
I think one should use the OOP concepts right from the start.
I wouldn't tell a beginner that there are Modules at all - not
before he knows what classes and shared members are.

110% ACK. Modules are for gurus like Nick.

;-)
 
Thanks for the replies guys. I tried all the suggestions
and none of them worked. I am assuming it is due to my
lack of understanding of VB and modules.
I have a main program (Form) that has all my code.
I would like to take some of the sub-routines and make
them into modules. For example:
A sub-routine:

Public Sub setupAFDC()

setupAFDC.Col = 0
setupAFDC.Row = 0
setupAFDC.CellAlignment = 1
setupAFDC.ColWidth(0) = 350
setupAFDC.Text = "chr"
......snipped for size
End Sub

So I add a module to my project. It defaults as Module1. I
name it AFDC

I copy/paste the above sub-routine into the module.

Now. When I want to access the module how do I do it?

I tried:
Public Module AFDC
Public Sub setupAFDC(ByVal d As AFDC)

setupAFDC.Col = 0
setupAFDC.Row = 0
setupAFDC.CellAlignment = 1
setupAFDC.ColWidth(0) = 350
setupAFDC.Text = "chr"
......snipped for size
End Sub
End Module
I get an error "Expected: end of statement"

So what I am doing incorrect?
 
Hello,

David Sawyer said:
Thanks for the replies guys. I tried all the suggestions
and none of them worked. I am assuming it is due to my
lack of understanding of VB and modules.
I have a main program (Form) that has all my code.
I would like to take some of the sub-routines and make
them into modules. For example:
A sub-routine:

Public Sub setupAFDC()

setupAFDC.Col = 0
setupAFDC.Row = 0
setupAFDC.CellAlignment = 1
setupAFDC.ColWidth(0) = 350
setupAFDC.Text = "chr"
.....snipped for size
End Sub

So I add a module to my project. It defaults as Module1. I
name it AFDC

I copy/paste the above sub-routine into the module.

Now. When I want to access the module how do I do it?

I tried:
Public Module AFDC
Public Sub setupAFDC(ByVal d As AFDC)

setupAFDC.Col = 0
setupAFDC.Row = 0
setupAFDC.CellAlignment = 1
setupAFDC.ColWidth(0) = 350
setupAFDC.Text = "chr"
.....snipped for size
End Sub
End Module
I get an error "Expected: end of statement"

Do not give the module the same name as a type ('AFDC'). Inside the
'setupAFDC' sub you should write 'd' instead of 'setupAFDC'.
 
David Sawyer said:
Thanks for the replies guys. I tried all the suggestions
and none of them worked. I am assuming it is due to my
lack of understanding of VB and modules.
I have a main program (Form) that has all my code.
I would like to take some of the sub-routines and make
them into modules. For example:
A sub-routine:

Public Sub setupAFDC()

setupAFDC.Col = 0
setupAFDC.Row = 0
setupAFDC.CellAlignment = 1
setupAFDC.ColWidth(0) = 350
setupAFDC.Text = "chr"
.....snipped for size
End Sub

So I add a module to my project. It defaults as Module1. I
name it AFDC

I copy/paste the above sub-routine into the module.

Now. When I want to access the module how do I do it?

I tried:
Public Module AFDC
Public Sub setupAFDC(ByVal d As AFDC)
^^^^^
What does this AFDC mean? You also named the module AFDC. When VB resolves
the type name, it finds the Module's name first. Did you also declare a type
called "AFDC" or did you set a reference to a dll that contains the type
"AFDC"?

setupAFDC.Col = 0

setupAFDC is the name of the procedure. I think you want to pass an object
as an argument (named "d" in this case) to the procedure. If you want to
access the object, you have to write

d.Col = 0
d.row = 0
'and so on

setupAFDC.Row = 0
setupAFDC.CellAlignment = 1
setupAFDC.ColWidth(0) = 350
setupAFDC.Text = "chr"
.....snipped for size
End Sub
End Module
I get an error "Expected: end of statement"

So what I am doing incorrect?

How do you call the Sub setupAFDC? Do you call it at all?
 
Back
Top