Can a Visual Basic (.NET / 2010) file be split up for readability?
Asked Answered
M

6

5

I'm writing a program in Visual Basic 2010. It's a HMI (Human-Machine Interface) and therefore has a whole whack of buttons that just send commands to other devices. As a result, there are a huge pile of event handlers for clicking buttons that can't really be broken down into modules (unless my understanding of modules is wrong).

Essentially, I'd like to be able to move all the event handlers, for, say, button presses to a different file. Can this be done or is it important that they stay in "MainWindow.xaml.vb"? (All my buttons reside in one fullscreen window, some are hidden by tabs).

Thanks,

--Erik T

Mulry answered 29/10, 2010 at 16:4 Comment(1)
How and when to break things up into modules is something you will find yourself revisiting again and again. Partial classes will solve your problem, but IMO it's not the intended purpose of partial classes - partial classes were added mostly so code generated programatically could be in a different file.Ghat
A
13

You can use Partial Classes to break up your class definition into multiple files which might help organize your code.

The link above will help explain exactly how to use Partial Classes. The following link will show you the VB.NET Syntax (and how to use Class Designer to split things up):

How to: Split a Class into Partial Classes

Acidimetry answered 29/10, 2010 at 16:6 Comment(2)
I should be able to migrate a decent portion of my code into a separate file like this. Awesome!Mulry
FWIW, this worked, but if you move event handlers to a separate file via Partial Classes the XAML designer in Visual Studio 2010 can no longer give you a convenient link to the event handler via the Properties window (it shows up as blank and if you double-click it will create a new, blank event handler... and then compile will of course complain about multiple identical signatures.)Mulry
E
4

I like to use regions to hide code (the code highlighter dose not do this kinda thing so ill use an image) alt text

Keep in mind that this is only hiding the code in VS the file is still compleatly intact and the compiler will just ignore the #Region and #End Region lines

Ennis answered 29/10, 2010 at 16:32 Comment(2)
I have been using regions, BUT they don't really help when you have a 400 foot long page that you really just want to have multiple sections of in different tabs of Visual Studio. Still a good tip though.Mulry
ahh, true it really depends on the person i guess.Ennis
L
3

Sure use partial class. Check this article,

http://visualbasic.about.com/od/usingvbnet/a/partclses.htm

Landmass answered 29/10, 2010 at 16:6 Comment(0)
P
3

You can use Partial Classes to split these into separate files, if required.

Punchball answered 29/10, 2010 at 16:6 Comment(0)
S
1

Could you separate some of the functionality into custom controls instead? Possibly even create the buttons on the fly based on some external data?

Saito answered 29/10, 2010 at 16:45 Comment(0)
G
1

For the modularity of the UI, you could explore the composition capacities of WPF/SL described here and with helpers and examples here . But it`s a really different architecture, and might be a major refactoring if you already have a lot of code.

For the buttons, you should be able to use commanding that should hide most of your event handlers. Read about commanding here.

Gleaning answered 29/10, 2010 at 17:12 Comment(2)
I actually AM using WPF, but I'm sort of using it with a Windows Forms mindset. I mostly switched to WPF so my app would scale to different screen resolutions with minimal effort. Commanding looks fascinating, and the views thing could help me out too. Thanks!Mulry
@Mulry : Youre welcome ! I agree that the transition from Windows Forms to MVVM is a big jump, but it really worth it. Try it on a small scale project, and youll see the benefits rapidly, if you search SO, you`ll find tons of ressources about MVVM :)Gleaning

© 2022 - 2024 — McMap. All rights reserved.