Link to home
Start Free TrialLog in
Avatar of pointeman
pointemanFlag for United States of America

asked on

Interface Switch Form1?

I have the following code on Form1 to use one of two Queue interfaces. I currently use this code to switch between queues. My 'Proposed Switching' class does choose the correct queue interface, I just cannot call the interface methods like Enqueue(), Dequeue(), Peek(), etc... Help!

[Form1 code]          
        private MsmqSwitch _mqueue = new MsmqSwitch();

         private void btnLogin_Click(object sender, EventArgs e)
        {
            QueueType queueType;

            if (rbtRegularQueue.Checked)
                queueType = QueueType.NonTransactional;
            else
                queueType = QueueType.Transactional;

            _mqueue.Login(queueType, queueString);
        }

        private void btnSendMsg_Click(object sender, EventArgs e)
        {         
             _mqueue.Enqueue(txtMessage.Text, "message label here"); //Cannot Call 'Enqueue'
        }

Open in new window


[Switching class]
 

public enum QueueType { NonTransactional = 1, Transactional = 2 };

  public class MsmqSwitch : IMsmQueue, IMsmQueueTrans
  {
        IMsmQueue _mqueue;
        IMsmQueueTrans _mqueueTrans;

        public MessageQueue Login(QueueType QueueType, string queueString)
        {
            switch (QueueType)
            {
                case QueueType.NonTransactional:
                    _mqueue = new MsmQueue(queueString);
                    return (MessageQueue)_mqueue;
                    
                case QueueType.Transactional:
                    _mqueueTrans = new MsmQueueTrans(queueString);
                    return (MessageQueue)_mqueueTrans;                                 
            }
            return null;
        }
    }

Open in new window

Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

The MessageQueue class (.NET) does not contain IMsmQueue or IMsmQueueTrans. thus the reason your code does not work.
Change your switch class as follows: (only if MsmQueue and MsmQueueTrans are subclasses of MessageQueue)
public class MsmqSwitch : IMsmQueue, IMsmQueueTrans  
  {
        MessageQueue _mqueue;
        MessageQueue _mqueueTrans;

        public MessageQueue Login(QueueType QueueType, string queueString)
        {
            switch (QueueType)
            {
                case QueueType.NonTransactional:
                    _mqueue = new MsmQueue(queueString);
                    return _mqueue;
                    
                case QueueType.Transactional:
                    _mqueueTrans = new MsmQueueTrans(queueString);
                    return _mqueueTrans;                                 
            }
            return null;
        }
    }

Open in new window

Avatar of pointeman

ASKER

Yea, I know. Those are my classes which use MessageQueue class. I need to switch between the two interfaces I built.
I've been reading about loosely coupled interface programming although not sure it apples here.
I'm not even sure how that would compile. Your MsmqSwitch class proclaims to implement the IMsmQueue and IMsmQueueTrans interfaces, but you haven't shown anything that would indicate you did implement such. Is this the complete code for the MsmqSwitch class?
Yea, the IMsmQueue and IMsmQueueTrans are part my first idea. I forgot to remove them from on cut-n-paste. I'm trying to build a component for .net usage. There are two Interfaces (which you noticed), each performs very well, just need a way to swap them and still access each methods. Also trying to eliminate unnecessary code.
ASKER CERTIFIED SOLUTION
Avatar of pointeman
pointeman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Unless IMsmQueue and IMsmQueueTrans or its classes have a base interface or class, you can not use your current code as is.
Please post IMsmQueue and IMsmQueueTrans and their respective class declarations (I only need propertiies and method signatures)
Example 2 is a perfect answer if using nearly identical Interfaces like me. Take a look...