Results 1 to 2 of 2

Thread: How to allow a user to change between derived types?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    How to allow a user to change between derived types?

    I've asked this question on SO and didn't really get anywhere so I think I'm probably coming at this from the wrong angle and need more than just Q&A help as I can't see a way around it.

    The problem
    have a WinForms program that has to talk to different pieces of hardware using different connection types such as TCP, USB, etc. Using the connections entails using a set of actions provided by VISA (e.g. OpenTcpSession() or OpenUsbSession(), etc)

    The connections are used to connect to different pieces of hardware when some method decides that a particular hardware operation requires a certain type of connection (specified by some other data).

    At some point in the future I might like to add more types of connections.

    A user of the application can select and edit a connection, part of which entails changing the connection type (i.e. change from using TCP to using USB). Changing the type means different method implementations will be used to operate the physical connection. It is this change of type when editing a connection that I am working on.

    The data related to each currently configured connection (including type data) is persisted via an external data source (a JSON file, but this is pretty irrelevant). there might be several, one or none of each connection type

    My solution so far

    I have an abstract class Connection with some derived types such as Tcp, Usb, etc, they have common methods and properties from the abstract class (e.g. Address, Open(), etc) but the implementations differ to get the different data and address formats in the desired format and to operate the connections using different methods.

    The application starts by populating a List<Connection> from the source of data. On this List are objects of the types derived from Connection. There could be several Tcp, a couple Usb, some other and so on.

    To present the user with controls to allow the changing of type when one of the connections from the list is selected but still keep the usage generalised the form has RadioButton controls that are created by a method that uses reflection to generate one object of each of the derived classes (unrelated to the list of connections from the external data source) and adds the names of them to the form, with a common CheckedChanged event handler.

    For example:

    c# Code:
    1. ConnectionIEnumerable = GetObjectsDerivedFromType<Connection>();
    2. foreach (Connection c in ConnectionIEnumerable)
    3. {
    4.      var rb = new RadioButton();
    5.      rb.Name = c.ToString() + "RadioButton";
    6.      rb.CheckedChanged += new EventHandler(CommonEventHandler);
    7.      form1.flowLayoutPanel1.Controls.Add(rb);
    8. }

    Where I'm stuck

    How can this generic handler that is used "know" what type to convert the currently selected object to? How does the RadioButton store and impart the information about a related type that should be converted to? Is there a better way of knowing what object type to convert to/Instatiate from user input without hard-coding it directly?

    I've looked at trying this plan other ways but it always comes back to a requirement that at some point there would need to be some kind of if checking the current type to convert it, or checking the type to be converted to, thus introducing non-generality.

    I'm kind of thinking I might end up with something along the lines of the following (for example only, I'm not saying I currently use, or need to use the .Tag property or ConvertTo):

    c# Code:
    1. currentObject.ConvertTo( (Type)( (RadioButton)sender).Tag ) );
    Thanks

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Re: How to allow a user to change between derived types?

    I think I've figured out how to do this for this design:

    c# Code:
    1. // Save the type information to the RadioButton at control creation
    2. rb.Tag = c.GetType();

    Then:

    c# Code:
    1. // Use the type information in the CheckedChanged event
    2. SomeConnection = (Connection)Activator.CreateInstance((Type)((RadioButton)sender).Tag);

    Still interested in opinion as to if there is a better way to structure everything here though!
    Thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width