3

This is purely a coding practice question concerning VB.NET structures. Where do you keep one?

As an example, I've created a structure for the simple purpose of keeping information organized, I'd like this structure to be stand-alone, not belonging to any class so I wouldn't have to access it through SomeClass.MyStructure but rather simply MyStructure. So, do I keep it in a separate class and import it into my namespace? Do I keep it in a module (Static class, essentially)? Do I just make a separate VB.NET code file and place is there (I would like it to live in a separate file). Just to clarify, I need this structure to be accesses by the whole application, not just a single class.

What do you usually do in such cases?

Phonon
  • 12,549
  • 13
  • 64
  • 114
  • 1
    This is somewhat irrelevant to your question, but in the *majority* of cases, you should define a **class** to store information, rather than a structure. – Cody Gray - on strike Apr 18 '11 at 14:54
  • I agree, but this structure won't need any methods, getters, initializes or anything like that. It's simply a convenient way to pass data between classes and functions, especially to threads, since they only take one parameter. – Phonon Apr 18 '11 at 14:56
  • 1
    That's not really the best way to decide when to use a structure vs a class. I have more information in [my answer to another question](http://stackoverflow.com/questions/5620843/does-it-make-sense-to-define-a-struct-with-a-reference-type-member/5621610#5621610). The important thing is that a structure has *value* type semantics, whereas a class has *reference* type semantics. You can easily pass an instance of a class containing public fields as an argument to methods, as well. – Cody Gray - on strike Apr 18 '11 at 14:57
  • Makes sense, but I'm not sure that I need the whole complexity of a class to store and pass seven short strings. I do see your point about value-type semantics, I did not realize such difference existed. Thanks for the input. – Phonon Apr 18 '11 at 15:09
  • 1
    That's the thing, though. The "complexity" that you're describing with a class is more imagined than real. Structures can have methods, properties (getters/setters), initializers, and even constructors, just like a class. You can take your structure definition and change the word structure to class, and everything will work the exact same way. I'm not trying to be argumentative here, but you're not the first person to be confused about the difference! – Cody Gray - on strike Apr 18 '11 at 15:11
  • I think I get it = ) Just to let bring this (*not-at-all-intended-to-be-here*) discussion to a respectable cadence, I've changed my Structure to a Class = ) I'm pretty happy with it. – Phonon Apr 18 '11 at 15:17

1 Answers1

2

A structure is a Type, much like a Class.

So Yes, put it in a separate file. Then it will be 'in a namespace', the project-default one.

H H
  • 263,252
  • 30
  • 330
  • 514