Parker's DevEd Blog

Musings on all software development technologies, and education involving computer science and fraud investigation

C#: Resolving Ambiguous References using "using ="

Have you ever been developing a Windows Forms application, and then somewhere in the form you want to create a new Timer (System.Threading). When you do this you run into a ambiguous reference problem because System.Windows.Forms already contains definition for a Timer. So, usually you just end up prefixing wherever you want a Threading Timer by adding the "System.Threading" text in front of it. Although, this does work it creates harder to read code, there is a solution.

using System;
using t = System.Threading;
using tTimer = System.Threading.Timer;
using System.Windows.Forms;

namespace AmbiguousReferences {
   
public partial class Form1 : Form {
       
public Form1() {
            InitializeComponent();
        }
       
private void Form1_Load(object sender, EventArgs e) {
           
Timer winFormsTimer = new Timer();
            t.
Timer threadTimer1 = new System.Threading.Timer(ShowMessage, "Timer 1", 500, t.Timeout.Infinite);
           
tTimer threadTimer2 = new tTimer(ShowMessage, "Timer 2", 500, t.Timeout.Infinite);
        }
       
private void ShowMessage(object msg) {
            Invoke((
MethodInvoker)delegate() {
               
MessageBox.Show(msg.ToString());
            });
        }
    }
}

Comments

No Comments