Tutorial :Is it possible get the functionality of namespaces inside a class?



Question:

I have the following code:

Public Class Form1  Private Function Step1_Execute() As Boolean      Return Step1_Verify()  End Function    Private Function Step1_Verify() As Boolean      Return True  End Function    Private Function Step2_Execute() As Boolean      Return Step2_Verify()  End Function    Private Function Step2_Verify() As Boolean      Return True  End Function  End Class  

What I would like to do is be able to separate the code, similar to the following (which obviously doesn't work):

Public Class Form1    Namespace Step1      Private Function Step1_Execute() As Boolean          Return Step1_Verify()      End Function        Private Function Step1_Verify() As Boolean          Return True      End Function  End Namespace    Namespace Step2      Private Function Step2_Execute() As Boolean          Return Step2_Verify()      End Function        Private Function Step2_Verify() As Boolean          Return True      End Function  End Namespace    End Class  

I would like the functionality of namespaces inside of a class, as in something that would let me call Step2.Execute() instead of having to put Step2_ in front of a whole bunch of functions. I don't want to have to create separate classes / modules for step1, step2, etc.

Is there a way that I can accomplish namespace functionality from inside a class?


Solution:1

How about just using a module (equivalent for the most part to a static class in C#)? That would seem to do the job well and give you the reference style you want.

Module Step1      Private Function Execute() As Boolean          Return Verify()      End Function        Private Function Verify() As Boolean          Return True      End Function  End Module    Module Step2      Private Function Execute() As Boolean          Return Step2_Verify()      End Function        Private Function Verify() As Boolean          Return True      End Function  End Module  

I believe you can even nest VB.NET Modules within other classes, if you wish.

Edit:

Just realised that all of the modules have the same contract (set of method signatures), so it would actually make sense to implement an interface here, and create instances of the class. A tad more code perhaps, but if you want to abstractify things, it may be worth the effort.


Solution:2

No.

Namespaces are nothing real existing. A namespace is merely a part of the name of a class. So if you have the class System.Text.StringBuilder, the "using System.Text" part is a hint to the compiler like "if you don't find the type, look for all types with the unknown identifier that start with 'System.Text' in front of the unknown part.


Solution:3

Obviously you need to split this up in 3 classes. One that will execute the code. And than 2 that inherit the same interface with an execute and verify method in it.

The executing class wil then contain a collection of this interface and you just add them in the order you wan to execute them.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »