locked
Class File RRS feed

  • Question

  • Hi All

    What should I do in terms of creating multiple classes that inherit one-another with respect to creating a new Class file and naming it?

    For example,

    I would normally Add a new class file by going to the tool bar Add New Item > Class then give it a name called 'Data.vb' then I would write my class in it which in this case would be Public Class Data as the parent class, but what If i wanted to create another class which inherits from Data called Public Connections, would I create a brand new Class file by going into the tool bar and Add New Item > Class or do I just write it in the same Class file called Data.vb?

    Or should I call the file a name that covers all the different classes within its that relate to Data such as 'DataFromSQLServer.vb' and within this Class file i would create Public Class Data, Public Class Connections, etc...

    Thanks

    Thursday, April 17, 2014 1:47 PM

Answers

  • There is no a "fixed rule" for that, but a kind of best practice said that is better to have (if possibile) 1:1 relation between classes and files.

    So in your scenario I suggest you to have:

    1 file called "Data.vb" in wich you declare a class with "Public Class Data...."

    1 file called "Connections" in wich you declare the class with

    Public Class Connections
    Inherits Data


    Davide 'n3wt0n' Benvegnù

    If you found my answer helpful, please vote it.
    If you think that my answer resolve your issue, plase "Mark it as answer".

    • Marked as answer by shamas2121 Thursday, April 17, 2014 2:19 PM
    Thursday, April 17, 2014 1:59 PM

All replies

  • There is no a "fixed rule" for that, but a kind of best practice said that is better to have (if possibile) 1:1 relation between classes and files.

    So in your scenario I suggest you to have:

    1 file called "Data.vb" in wich you declare a class with "Public Class Data...."

    1 file called "Connections" in wich you declare the class with

    Public Class Connections
    Inherits Data


    Davide 'n3wt0n' Benvegnù

    If you found my answer helpful, please vote it.
    If you think that my answer resolve your issue, plase "Mark it as answer".

    • Marked as answer by shamas2121 Thursday, April 17, 2014 2:19 PM
    Thursday, April 17, 2014 1:59 PM
  • From a technical standpoint, there is nothing stopping you from defining multiple classes in the same .vb file. However in practice it is usually better to create one class PER .vb file. This is because the .vb file has to have a name, and can have only one, and it would make sense for your .vb file names to match the class that is defined within them.

    Sometimes if I am quickly prototyping something, I may define the second class inside the first code file, but then once I decide if that code will stay in the project, it gets its own code file.

    As far as naming them, that is your preference too, but often times child classes do not have their names directly linked to their parent they inherit from. Only when it makes sense for that to be the case. You want to name the class specifically for what it does, not for what it inherits from.


    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com

    Thursday, April 17, 2014 2:01 PM
  • Thanks

    But would the issue be that if I had 50 classes im going to have 50 files?

    Im new to OOP... but typically in a meduim sized application, how many classes would it normally have?

    Thanks

    Thursday, April 17, 2014 2:20 PM
  • Yes, you probably gonna have 50 files.

    Obviosuly, it's better to make an "intelligent" folders structure that allows you to manage, archive and find classes based on their purpose or their "functionality".

    I think it's not possible to define a medium sized application average classes number, because it really depends on the application type, on how you structure it, etc... Anyway, you can consider that i.e. I have several projects with hundreds of classes.


    Davide 'n3wt0n' Benvegnù

    If you found my answer helpful, please vote it.
    If you think that my answer resolve your issue, plase "Mark it as answer".

    Thursday, April 17, 2014 2:24 PM