2014-02-18 121 views
0

被承认我有一个用于创建C#观的新实例不会在溶液

不幸的是,对Outlook中的一个新的约会时,我设置Outlook

Outlook.Application outlookApp = new Outlook.Application(); // creates new outlook app 

“outlookApp的新实例代码“错误24字段初始值设定项不能引用非静态字段,方法或属性 ) Outlook.AppointmentItem oAppointment =(Outlook.AppointmentItem)outlookApp.CreateItem(Outlook.OlItemType .olAppointmentItem);

整个代码仅供参考。

Outlook.Application outlookApp = new Outlook.Application(); // creates new outlook app 
Outlook.AppointmentItem oAppointment = (Outlook.AppointmentItem)outlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem); // creates a new appointment 

oAppointment.Subject = "Holiday request for " + name ; // set the subject 
oAppointment.Body = " "; // set the body/send details of contact form below 
oAppointment.Location = "Nicks Desk!"; // set the location 
oAppointment.Start = Convert.ToDateTime(notesDate); // Set the start date NEEDS TO BE THE DATE THAT THE USER ENTERS IN DATEFROM 
oAppointment.End = Convert.ToDateTime(notesDate); // End date NEEDS TO BE THE DATE THAT THE USER ENTERS IN DATETO 
oAppointment.ReminderSet = true; // Set the reminder 
oAppointment.ReminderMinutesBeforeStart = 15; // reminder time 
oAppointment.Importance = Outlook.OlImportance.olImportanceHigh; // appointment importance 
oAppointment.BusyStatus = Outlook.OlBusyStatus.olBusy; 

oAppointment.Save(); 
Outlook.MailItem mailItem = oAppointment.ForwardAsVcal(); 
//who is sending the email 
mailItem.SentOnBehalfOfName = NameInput; 
// email address to send to 
mailItem.To = "mailto:[email protected]">[email protected]; 
// send 
mailItem.Send();  

任何想法?

+2

你会得到什么错误? – SLaks

+0

错误字段初始值设定项无法引用非静态字段,方法或属性 关于outlookApp – DevAL

回答

2

由于错误试图告诉您,您不能在字段初始值设定项中引用您的实例(包括其他字段),因为它们在构造函数之前运行。

将初始值设定项移至某个方法。

+0

您是什么意思?我试图将每个部分添加到不同的方法,但其余的程序然后标记。 修正了它,我现在在mailItem.Send(); 警告方法'Microsoft.Office.Interop.Outlook._MailItem.Send()'和非方法'Microsoft.Office.Interop.Outlook.ItemEvents_10_Event.Send'之间的歧义。使用方法组。 – DevAL

+1

@亚历克斯:你不能在课堂上直接“做东西”。一个类只能包含定义 - 字段,属性,方法,事件等。一个类不能*直接*包含必须逐行执行的代码,以便按照“按需执行”的方式逐步执行。这样的代码 - 比如你发布的内容 - 需要进入类内部的一个方法。如果您遇到问题,我建议您将所有代码(包括该文件中的所有内容)发布出来,以便我们可以清楚地了解它目前的组织结构,并发布如何重新排列它的示例。 – Chris

1

正如你在留言中提到,你得到的错误是:

错误24场初始化不能引用非静态字段,方法或属性outlookApp

这是没有立即从您发布的代码中清除,但是您收到此错误的事实意味着我怀疑您发布的整个代码示例直接包含在类中,而不是在该类的方法或构造函数中:

public class MyClass 
{ 
    // the code you posted is contained here 
} 

您发布的代码中,第一行在此位置完全有效。然而,第二行在这种情况下是非法的,其后的许多行也是如此。在初始化oAppointment字段时,通过使用outlookApp字段来完成此操作 - 但字段初始值设定项不允许引用其他字段,因为不能保证字段初始化的顺序,因此outlookApp甚至可能没有价值。

的可能性是,你需要移动的大部分或全部您发布到一个构造函数或方法,在这里你写的线条将是有效的代码:

public class MyClass 
{ 
    public void MyMethod() 
    { 
     // the code you posted should be contained here 
    } 
} 

究竟是什么MyClass的和看的MyMethod比如,我没有足够的信息来告诉你。