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
    teis

    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.


    I seem to remember that C# 3.0 will have some syntactic sugar to help with that. So you could do something like:

    class Alarm {
    private string message;
    public string Message {
    get { return this.message; }
    set { this.message = value; }
    }

    private DateTime expires;
    public DateTime Expires {
    get { return this.expires; }
    set { this.expires = value; }
    }
    }

    var alarm = new Alarm( Message: "Hello, World", Expires: DateTime.Now );

    The compiler will then expand the last line of code into:

    Alarm alarm = new Alarm();
    alarm.Message = "Hello, World";
    alarm.Expires = DateTime.Now;

  • User profile image
    VBJB

    One other thing Big Smile ...never happy huh. This is only for my preference of keeping things clean. When I instantiate FMA3 in my forms application (Public FMA3 As New Mill.FMA3) I can see Alarm as in Public FMA3 As New Mill.Alarm. Is there a way I can hide this so I only expose Mill.FMA3?

  • User profile image
    Sven Groot

    VBJB wrote:
    One other thing ...never happy huh. This is only for my preference of keeping things clean. When I instantiate FMA3 in my forms application (Public FMA3 As New Mill.FMA3) I can see Alarm as in Public FMA3 As New Mill.Alarm. Is there a way I can hide this so I only expose Mill.FMA3?

    No. You have a public class with a public property of type Alarm, so Alarm itself also needs to be public. You can prevent outsiders from instantiating the Alarm class themselves by changing it's Sub New to Friend instead of Public (in which case Public whatever As New Mill.Alarm would cause a compiler error if done outside the Mill assembly), but the class itself must remain public.

  • User profile image
    VBJB

    Sven Groot wrote:

    You can prevent outsiders from instantiating the Alarm class themselves by changing it's Sub New to Friend instead of Public


    Thanks Sven. I will do that.

  • User profile image
    Tyler Brown

    If you don't like the idea of passing many parameters into the constructor, or calling multiple get/set methods... well theres not much you can do, as you're going to have to do it all one way or another if you need all that data to be specified.

    What I like to do is encapsulate the data into a class if possible, then pass an instance of that class populated with all the configuration data. Although I seldom run into situations where I require such large amounts of configuration parameters...

    Perhaps taking some time to look more closely at your design might enable you to refactor it in such a way that less data will actually be required, or perhaps things could be done more efficiently?

Conversation locked

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