Unpossible
Unpossible

Reputation: 10697

VB.Net: Including common code in multiple files

I'm new to the .NET platform (old-time ASPer) and for a project have a simple pair of .aspx scripts that take some querystring data and process it. They both make use of the same Subs, Functions in them, so naturally I'd like to move the code to a common resource.vb file. Having done some reading, it looks like my options are a Code-Behind type setup, and placing the Subs, Functions in a .vb file and placing that file in App_Code.

So, the Code-Behind model doesn't seem to make sense here, as the functions are shared by both files. Placing the Subs and Functions in /App_Code/resources.vb and I am getting the following error when trying to call any of the routines:

Statement is not valid in a namespace

Reading this answer, I thought the last solution would be fine, but not so. Any pointers? This whole solution is meant to be simple and portable, something someone can deploy into their domain by simply uploading the files, so portability is also important.

Thanks,

Paul

Upvotes: 1

Views: 2201

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

The basic building block of VB.NET is the class, or alternatively a module. You cannot just dump functions into a code file, you need to associate them with a class or module.

For an easy fix, just put them inside a module:

Module FooBar
    ' Your methods here
End Module

But that is code smell and you shouldn’t let it get out of hand. .NET is inherently object oriented so you should put some thought into developing a consistent architecture where each class has one responsibility and performs the according actions.

(See SOLID principles)

Upvotes: 2

Related Questions