需要帮助才能在C#中正确实现接口 [英] Help needed to properly implement interface in C#
问题描述
大家好 - 我在C#项目中实现接口时遇到问题。
我有一个带有以下界面的Windows表单:
Hi all - I have a problem with implementing interfaces in a C# project.
I have a Windows form with the following interface in it:
public interface Trigger
{
void UpdateClient();
void UpdateHotel();
}
class UpdateParties: Trigger
{
static void Main()
{
UpdateParties iImp = new UpdateParties();
iImp.UpdateClient();
iImp.UpdateHotel();
}
public void UpdateClient ()
{
this.toolStripStatusLabelGuestID.Text = "Client ID: " + globals.CurrentGuestID;
// Populate name details box
this.tbGuest.Text = globals.PreFill;
}
public void UpdateHotel ()
{
this.toolStripStatusLabelHotelID.Text = "Hotel ID: " + globals.CurrentHotelID;
// Populate name details box
this.tbHotel.Text = globals.PreFill;
GetHotelData();
}
其中GetHotelData()在表单本身的按钮单击事件的其他地方使用。
现在我尝试编译时遇到两个问题。第一个是'的错误消息''Myproject.frmDisplay.UpdateParties''不包含''toolStripStatusLabelGuestID''的定义,也没有扩展方法''toolStripStatusLabelGuestID''接受类型''的第一个参数可以找到Myproject.frmDisplay.UpdateParties''(你是否错过了使用指令或汇编引用?)
- 每次我弹出在itelf表单上引用一个控件(toolStripStatusLabelHotelID是工具条上的标签,同样的事情发生在一个名为tbGuest的文本框中),所以我得到了四种这类错误消息。
第二个错误是当调用 GetHotelData时,当前上下文中不存在GetHotelData这个名称 ()。我不想复制GetHotelData(),因为它在主窗体上播放数据表等,我不能随便乱播。
如何解决这些问题?我在表单本身有其他代码调用GetHotelData()并操控控件就好了,我不想开始破坏它。我需要做的就是从另一个表单中调用这些方法,以便这个表单更新它的显示。
where GetHotelData() is used elsewhere by a button click event on the form itself.
Now I''m facing two problems when I try and compile this. The first is an error message along the lines of ''Myproject.frmDisplay.UpdateParties'' does not contain a definition for ''toolStripStatusLabelGuestID'' and no extension method ''toolStripStatusLabelGuestID'' accepting a first argument of type ''Myproject.frmDisplay.UpdateParties'' could be found (are you missing a using directive or an assembly reference?)
- this pops up every time I reference a control on the form itelf (toolStripStatusLabelHotelID is a label on the toolstrip bar, same thing happens for a textbox called tbGuest) and so I get four error messages of this kind.
The second error is The name ''GetHotelData'' does not exist in the current context when it comes to invoking GetHotelData(). I don''t want to duplicate GetHotelData() as it plays with data tables etc. on the main form and I can''t shuffle those around.
How do I resolve these issues? I have other code in the form itself that invokes GetHotelData() and manipulates the controls just fine, and I don''t want to start breaking things. All I need to do is invoke these methods from another form so that this form updates its display.
推荐答案
不,你没有这样的Windows窗体!
重新开始 - 在Visual Studio中创建一个新的Windows窗体项目,将Form1
重命名为UpdateParties
,添加UI元素。
然后更改
公共部分类UpdateParties:表单
to
public partial class UpdateParties:Form,Trigger
并从上面的代码段中插入两个函数,和GetHotelData
函数。
No, you do not have such a Windows Form!
Start over again - create a new Windows Forms project in Visual Studio, renameForm1
toUpdateParties
, add the UI elements.
And then change
public partial class UpdateParties : Form
to
public partial class UpdateParties : Form, Trigger
and insert the two functions from your snippet above, and theGetHotelData
function.
这篇关于需要帮助才能在C#中正确实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!