0

I've been writing a program in VB.Net that uses a form to pass information along to other classes and I have a question regarding my current code structure. I personally don't like the various controls in the form to be exposed for other classes to change at will, so I'd prefer to use read-only properties of those controls for the classes to get their information. The trouble is, the properties have ended up taking a lot of space in the form's class and, I feel, is making it unwieldy and harder to read. Are there any standards/is there any advice regarding this that I could follow?

To help clarify, I'd much rather use, for example,

MsgBox(MyForm.PartNumber)
'Or
MsgBox(MyForm.PartType)

instead of

MsgBox(MyForm.PartNumberTextBox.Text)
'Or
MsgBox(MyForm.PartTypeComboBox.SelectedItem.ToString())

As the former properties look better (IMO) and the latter can risk things like

MyForm.PartNumberTextBox.Value = "Something else"

Any suggestions/responses would be appreciated.

N Biggs
  • 3
  • 2
  • Code review is better suited to answer your question –  Jun 26 '18 at 19:30
  • If you feel the code to expose the Form's control's properties is cluttering up the main code file, you could move it to its own a [Partial Class](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/partial) either in the same file or or separate file (similar to FormName.designer.vb). – TnTinMn Jun 26 '18 at 19:31
  • 1
    You don't have to cram *every* data item you manipulate into a single Form class. Separating the model from the view is a standard programming paradigm. But not enforced in Winforms like it is in other UI frameworks. Just add it yourself. – Hans Passant Jun 26 '18 at 22:17
  • @TheWhiteWolf Thanks for the tip. I wasn't aware of that site so I'll definitely remember that. – N Biggs Jul 05 '18 at 18:09
  • @TnTinMn I was considering using a partial class, but elsewhere I've looked there has been some contestation on whether a partial class is good to use or not (aside for forms and their designer partial class). – N Biggs Jul 05 '18 at 18:09

1 Answers1

0

You could create read-only properties on the form that are wrappers around the values in the controls:

Public ReadOnly Property PartNumber As String
   Get
      Return PartNumberTextBox.Text
   End Get
End Property

That way you can access the values external to the Form without risk of them being modified:

MsgBox(MyForm.PartNumber)
RogerMKE
  • 667
  • 4
  • 12
  • That's one part of my dilemma. I can make all those properties, but then my class size is doubled with all the properties I want to add, which makes it harder to read. I'm more asking for ways to balance that out. – N Biggs Jul 05 '18 at 18:05
  • To make it more readable, usually what I do these situations is to wrap the properties in a #Region "Custom Properties". Visual Studio will collapse that region when you open your code, hiding all those properties unless you want to expand them for some reason. – RogerMKE Jul 06 '18 at 19:42