Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Tuple. Does not contain a definition for Item1

Tags:

c#

tuples

class WakeUP
{
    [DllImport("kernel32.dll")]
    public static extern SafeWaitHandle CreateWaitableTimer(IntPtr lpTimerAttributes, 
                                                              bool bManualReset,
                                                            string lpTimerName);

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetWaitableTimer(SafeWaitHandle hTimer, 
                                                [In] ref long pDueTime, 
                                                          int lPeriod,
                                                       IntPtr pfnCompletionRoutine, 
                                                       IntPtr lpArgToCompletionRoutine, 
                                                         bool fResume);

    public event EventHandler Woken;

    private BackgroundWorker bgWorker = new BackgroundWorker();

    public WakeUP()
    {
        bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
        bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
    }

    public void SetWakeUpTime(DateTime time, String tName)
    {
        // Create a 7-tuple. 
        var wutargs = new Tuple<string, string>(time.ToFileTime().ToString(), tName.ToString()); 
        bgWorker.RunWorkerAsync(wutargs);
    }

    void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (Woken != null)
        {
            Woken(this, new EventArgs());
        }
    }

    private void bgWorker_DoWork(object sender, DoWorkEventArgs e) 
    {
        var thetuple = e.Argument;
        long wakeuptime = (long)thetuple.Item1;

        using (SafeWaitHandle handle = CreateWaitableTimer(IntPtr.Zero, true, "Timer"))
        {
            //if (SetWaitableTimer(handle, ref "12:00 AM", 0, IntPtr.Zero, IntPtr.Zero, true))
            //{
                //using (EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.AutoReset))
                //{
                    //wh.SafeWaitHandle = handle;
                    //wh.WaitOne();
                //}
            //}
            //else
            //{
                //throw new Win32Exception(Marshal.GetLastWin32Error());
            //}
        }
    }

Visual Studio is telling me in regards to:

        long wakeuptime = (long)thetuple.Item1;

Error 1 'object' does not contain a definition for 'Item1' and no extension method 'Item1' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) C:\Users\esepich\Documents\Visual Studio 2013\Projects\SepysAlarmV1ecs\SepysAlarmV1\WakeUP.cs 57 46 SepysAlarmV1

How am I supposed to access the elements of the tuple?

Thank you for posting...

like image 835
Eae Avatar asked Feb 03 '26 20:02

Eae


1 Answers

The argument could be any type. It's passed as an object, so cast it back to the original type:

var thetuple = (Tuple<string, string>)e.Argument;

You're going to get a compiler error on the next line too:

long wakeuptime = (long)thetuple.Item1;

Consider using Convert.ToInt64 or Int64.TryParse.

like image 173
Grant Winney Avatar answered Feb 05 '26 10:02

Grant Winney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!