Tech Off Thread

23 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Class Design

Back to Forum: Tech Off
  • User profile image
    VBJB

    Alright can someone explain this to me? I am trying to create an assembly that is made up of many classes, but I am having trouble making it all work as a single object. This is what I want in my assembly.

    I want to be able to say Public FMA3 As New Mill.FMA3 in my forms application (the assembly name is Mill). The main purpose of the FMA3 class is to set all its properties in the Sub New(). I have all this figured out so far.

    Here is where I am struggling. I want to have properties such as FMA3.Alarm.AlarmNum, FMA3.Alarm.AlarmMsg, or FMA3.Alarm.hasAlarm (other properties FMA3.Current.Tool, FMA3.Current.MCode..). I am guessing FMA3.Alarm needs to be a namespace?! How do I physically set this up?

    This is what I have so far, but I have not been able to get it to work. I have an assembly named Mill, inside that assembly I have a class named FMA3 and another class named Alarm (seperate files; FMA3.vb and Alarm.vb) and I am guessing the Namespace in class alarm would be Namespace FMA3. The local variables in Public Class Alarm would be labeled as Private Shared so the FMA3 class can set the ReadOnly properties inside the Alarm class, but none of this is working right. Can someone help me?

    I also tried nested classes, but it was to hard to manage. I would like to break out each namespace in its own class.

  • User profile image
    Sven Groot

    Sorry, I don't even know where to begin on this. I suggest you read up on what namespaces and classes are actually for as well as guidelines on naming and usage.

    I don't mean to offend, but what you've described makes no sense at all. What are you actually trying to accomplish?

    You also seem to have a completely wrong idea of what Shared means (it means the variable is shared between instances of the same class; even if you create 100 instances, there's only one copy of that variable and only one value). If you want them to be accessible from other classes in the same assembly only, use Friend (although the idea that a class would initialize the values of another class goes against everything that OO design is about, so I suggest you reconsider that). And ReadOnly fields can only be initialised (set the value on the same line as the declaration), they can never be assigned afterwards, regardless of their access level.

  • User profile image
    Tyler Brown

    What you want to do is not possible. You cannot create namespaces within classes. You can however place many classes into a namespace. What you should probably do is pick up a book that introduces the concept of object oriented programming. Then take another stab at this project.

    All of the properties you've mentioned should be using one less '.' in them which could be accomplished by refactoring your design. Create a property called FMA3.CurrentTool which would return the current tool, or FMA3.AlarmMsg, FMA3.HasAlarm, etc...

    What exactly does FMA3 stand for anyways?

  • User profile image
    VBJB

    Tyler Brown wrote:
    Create a property called FMA3.CurrentTool which would return the current tool, or FMA3.AlarmMsg, FMA3.HasAlarm, etc...


    This is where I started, but after looking at it I thought I could group things better. Working with the many properties of my Class FMA3, groups of similar properties started forming. I thought I could organize things better, putting similar properties together. So I started experimenting with nested classes and it worked. I could call FMA3.Alarm.Msg or FMA3.Alarm.Number, but I noticed another things it was hard to manage everything in one file. So I started breaking them out in separate files and that is where I ran into trouble.

    All I am trying to do is group similar properties together. Is there any other way of doing this besides FMA3.AlarmMsg, FMA3.HasAlarm, etc...?

    Thanks for your time explaining your thoughts.

    Tyler Brown wrote:

    What exactly does FMA3 stand for anyways?


    A FMA3 is a type of computer controlled machine that I am interfacing a computer with using FOCUS1.

  • User profile image
    blowdart

    VBJB wrote:
    This is where I started, but after looking at it I thought I could group things better. Working with the many properties of my Class FMA3, groups of similar properties started forming. I thought I could organize things better, putting similar properties together. So I started experimenting with nested classes and it worked. I could call FMA3.Alarm.Msg or FMA3.Alarm.Number, but I noticed another things it was hard to manage everything in one file. So I started breaking them out in separate files and that is where I ran into trouble.


    Ah, I see (Ohhh big machine that goes ping)

    So what you'd really like is something like

    Machine.Feature.Property

    Yes, you can do it, have a machine class, and a class per feature. Expose the properties in the feature class, and make the feature object public in the machine class.

    As an exercise for the reader is this, or is this not, a good idea from an OO viewpoint?

  • User profile image
    VBJB

    Thank you for your help. Should I keep all this in one .VB file or can I separate the features into their own class/file?

  • User profile image
    Sven Groot

    VBJB wrote:

    Thank you for your help. Should I keep all this in one .VB file or can I separate the features into their own class/file?


    Separate files is the way to go, much easier to keep track of where things are.

  • User profile image
    VBJB

    Sven Groot wrote:
    VBJB wrote:

    Thank you for your help. Should I keep all this in one .VB file or can I separate the features into their own class/file?


    Separate files is the way to go, much easier to keep track of where things are.


    Now, I am at the very beginning of this thread. I am unable to do it. I need a little guidance. Here is my conceptual design.

    I have an assembly named Mill (FMA3 is a type of mill). Inside that assembly I will have separate .vb files for each of the features. My Class FMA3 will initialize each feature right? Each class will have there local variables m_AlarmMsg, which will be set from the FMA3 Class (the local variables in the Alarm class needs to be labeled as Friend right). How will I align the "namespaces"? If I do not use namespaces I will get Mill.FMA3 and Mill.Alarm not Mill.FMA3.Alarm.

  • User profile image
    Sven Groot

    You should, whenever at all possible, not use Friend fields. The point of OO design is that all code dealing with the data should be in the class that contains the data. Try something like this:

    Alarm.vb:

    Public Class Alarm
    Private mAlarmMessage As String

    Public Sub New(ByVal alarmMessage As String)
    mAlarmMessage = alarmMessage
    End Sub

    Public ReadOnly Property AlarmMessage() As String
    Get
    Return mAlarmMessage
    End Get
    End Property
    End Class


    FMA3.vb
    Public Class FMA3
    Private mAlarm As Alarm

    Public Sub New()
    mAlarm = New Alarm("This is the alarm message")
    End Sub

    Public ReadOnly Property Alarm() As Alarm
    Get
    Return mAlarm
    End Get
    End Property
    End Class

  • User profile image
    VBJB

    I see! It's starting to make sense. One other question, let's say the alarm class has five or more properties, would you pass all five of them as constructors or is there another way to do this when a property list is lengthy?

  • User profile image
    blowdart

    VBJB wrote:
    I see! It's starting to make sense. One other question, let's say the alarm class has five or more properties, would you pass all five of them as constructors or is there another way to do this when a property list is lengthy?


    Ah, style issues. I'd say it depends. If you need all five before the object can perform it's purpose then pass them into the constructor, if you need just 3, have 2 constructors, one taking 3 and one taking 5. And so on.

  • User profile image
    VBJB

    Yeah, I guess you could call it style. I love the example Sven provided me, but it seems silly if I have ten properties to call New Alarm("This","That","This","That"....). I like things looking clean, but for now I am going to use Sven's example.

    Sorry my initial description of my problem was described so awfully.Embarassed

  • User profile image
    blowdart

    VBJB wrote:
    Yeah, I guess you could call it style. I love the example Sven provided me, but it seems silly if I have ten properties to call New Alarm("This","That","This","That"....). I like things looking clean, but for now I am going to use Sven's example.

    Sorry my initial description of my problem was described so awfully.Embarassed


    See to me a constructor that takes everything is cleaner than 10 property set calls.

    Ah, time for a religous war on this issue.

  • User profile image
    VBJB

    blowdart wrote:

    See to me a constructor that takes everything is cleaner than 10 property set calls.

    Ah, time for a religous war on this issue.


    What do you mean, something like a collection (array) that would set all the properties. An array is passed in and the Alarm's sub new would iterate through the array and set all the properties.

  • User profile image
    blowdart

    VBJB wrote:
    blowdart wrote:
    See to me a constructor that takes everything is cleaner than 10 property set calls.

    Ah, time for a religous war on this issue.


    What do you mean, something like a collection (array) that would set all the properties. An array is passed in and the Alarm's sub new would iterate through the array and set all the properties.


    Oh dear lord *NO*

    Alarm(int param1, int param2, string param3, string param4, DateTime param5, etc etc etc)

    No, individual parameters. Use an array and type checking vanishes. Bad move.

  • User profile image
    VBJB

    Sven Groot wrote:

    Alarm.vb:

    Public Class Alarm
       Private mAlarmMessage As String

       Public Sub New(ByVal alarmMessage As String)
          mAlarmMessage = alarmMessage
       End Sub

       Public ReadOnly Property AlarmMessage() As String
          Get
             Return mAlarmMessage
          End Get
       End Property
    End Class
     


    FMA3.vb

    Public Class FMA3
       Private mAlarm As Alarm

       Public Sub New()
          mAlarm = New Alarm("This is the alarm message")
       End Sub

       Public ReadOnly Property Alarm() As Alarm
          Get
             Return mAlarm
          End Get
       End Property
    End Class
     



    Using this example I am still not 100% there or am I not reading it right. My properties are still going to read FMA3.AlarmMsg or FMA3.AlarmNum not FMA3.Alarm.Msg or FMA3.Alarm.Number.

  • User profile image
    Sven Groot

    I'm not sure I understand what you mean. Using the example I posted you can do this, for instance:

    Dim fma3 As New FMA3
    Console.WriteLine(fma3.Alarm.AlarmMessage)

  • User profile image
    VBJB

    Sven Groot wrote:

    Public ReadOnly Property Alarm() As Alarm


    Owe, I see! I posted my comment without trying it. Alarm() as Alarm; this will look at alarm and show its properties. I was thinking of as Public ReadOnly Property Alarm() As string. Thanks for all your help Sven and blowdart.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.