views:

66

answers:

4

I am new to vb.net and very frustrated.

Like all good programmers I want to split my code into separate files based on functionality . Some of my code interacts with users via Forms and some interacts with lab equipment behind the scenes (no direct user interaction). Sometimes a user will change something that will impact the lab equipment and sometimes something will happen with the lab equipment that a user needs to be aware of. When I use VS to create files I have to choose a Module or Form. VS then creates an empty file with a with either

Public Class Foo

End Class

or

Module Foo

End Module

If I have a bunch of files, each a Module, and if I define routines in a Module to be Friend then I can call them from other Modules, so:

Module Foo

Friend Sub DoSomeWork()

End Sub

End Module

Code in Fee can call routines in Foo -

Module Fee

 Friend Sub Stuff()

 DoSomeWork()

 End SUb

End Module

When I create a Form, VS creates a Class. I find that I can call subroutines defined in a Module from a Class but when I try to call from a Module into a Class I get an error that the routine I am trying to call is not declared. I also cannot call from one Class into another Class. Declarations seem to apply only to library routines outside my program.

I have looked through several online explanations and tutorials, but frankly I don't understand these nor do I care about "inheriting from the base class" and all the other gobbledygook that such "explanations" contain. I want to concentrate on building my application.

My Main form has the name "Main"

I tried putting all the module code into the Main Class first by renaming "Module Foo" to "Public Partial Class Main" - bad idea - creates an impossible-to-find duplicate error. I tried creating empty code files, defining them as Public Partial Class Main and putting the Module code into them, - this worked in that code in the Class Main could call the "Module" code (which was now in Main) and vice-versa, but, other Forms (of course I have more than one) are created by VS to have their own Classes and once the "Module" code is moved out of Modules into Class Main the other Forms(Classes) could not call the code anymore.

I just want some recipe (best practice) I can follow to for defining Modules and Classes so that code and data can be shared.

ANSWER from below

To invoke a subroutine in another Class you simply need to put the class name in front of the subroutine name.

So not

DoSomeWork()

but

Foo.DoSOmeWork()

This may be obvious to all of you experienced programmers but not to me. You do not have to prepend a class/module name to a Module-to-Module call or a Class-to-Module call, only to calls that are made into Classes. Personally, for the sake of consistency, I think the things should be the same, but it would probably violate some OO rule. Anyway thank you to all.

A: 

NomD,

Like all good programmers

you should indeed care

about "inheriting from the base class" and all the other gobbledygook that such "explanations"

This will make you a better programmer and taking the time to understand why proper code structuring is important will also begin to yield better results for you.

Paul Sasik
So, you know about OOP, correct? So please explain why I cannot call into a class from a Module.
NormD
@NormD: You have quite a write up and i read it several times but i still don't quite understand what the problem is. It's hard to say if you're instantiating classes or calling static methods etc. Some actual code and error output would be useful.
Paul Sasik
A: 

It seems like you don't understand the basics of object-oriented programming (OOP).

If you DON'T want to learn how to design your application in an object-oriented way, then only create modules in your application and you will be able to call functions from one to another without any problem. But this will result in code that will not be easily maintainable and scalable.

The other option is to learn OOP by picking a book about it, or following a course or tutorial on the subject. It's a significant investment but it will result in more robust code that will scale better when your application grows.

Meta-Knight
My application does not need to scale and maintainability at this time is not a requirement (this is a prototype). I WAS only creating Modules, but I had to add some Forms and Forms are added as Classes so I need to code in Classes interact with code in Modules. I have read books about OOP and I have done tutorials but these always seem to deal with things on a abstract level that fails when you have to get real-world work done. I am reminded of database purists that insist everything be 3rd normal form... Thank you for you reply.
NormD
Reason for the downvote: Does not address the question. It's more of an opinion that would have been better in a comment.
xpda
xpda: Thanks for leaving a reason for your downvote. I was about to edit with an explanation for his specific problem but I see that you solved his problem ;-)
Meta-Knight
A: 

Generally, if you have a function that needs to be called from more than one form, or from forms and modules, put it in the main module. If you have an exceptional case and need to call a function or sub in a form from another form or a module, you can declare it to be public:

Public Class Form1
public sub test(i as integer)
...
end sub
end class

and then you can call it by referring to the class.subname:

call form1.test(7)
xpda
Thank you! Thank you! Thank you!
NormD
A: 

I am not sure why two commentors seem to have an issue with VB.Net. The question would be the same regardless of the language, since both are C# and VB are built on .Net. Code can be written poorly in C#, just like VB. Please leave the language wars at home. NormD, the answer to your question should really be to direct you to the resources needed to better understand the problem. Here is an article on scope that might help a bit - class scope. The reason you are getting the behavior that you see is due to what you are working with. Modules (similar to static classes in C#) are created when you program begins, so there is no need to create them. So you can reference a method on a module, like so - module.method. Classes on the other hand, some exceptions, need to be created in order to be referenced. So to use an employee (or form class) you must create a variable of that class. So you would use dim myemp as New Employee() and then call myemp.method() from your module. This is a rather simplistic description, so please read the scope article and other MSDN articles for more information. I am sure other posters can post additional links with good information. Hope this helps a bit.

Wade

Wade73