构造函数中的虚拟成员调用 [英] Virtual member call in constructor

查看:24
本文介绍了构造函数中的虚拟成员调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我在不同的上下文中运行相同的 winform 以控制按钮的可见性、启用文本字段和 winform 标题文本.我决定这样做的方法是简单地将一个字符串传递给表单构造函数,并使用几个 if 语句检查它,这些语句又包含所需的 winform 调整.

In my application I am running the same winform in different contexts to control visibility of buttons, enabeling of text fields and the winform header text. The way I decided to do this is simply by passing a string to the form constructor and check it with a couple of if statements that in turn contain the desired winform tweaks.

if (formContext == "add")
{
    Text = "Add member";
}
if (formContext == "edit")
{
    Text = "Change role";
    userTextBox.Enabled = false;
    searchButton.Visible = false;
}

这很好用,但是文本"关键字会被 ReSharper 添加一条蓝色波浪线,并带有以下消息:构造函数中的虚拟成员调用.这是一个潜在的问题还是只是某种过于热情的 ReSharper 消息.

This works fine, however the "Text" keywords get a blue squigly line added by ReSharper with the following message: Viritual member call in constructor. Is this a potential problem or just some sort of overly enthusiastic ReSharper message.

对于改进我的实施的任何澄清或建议将不胜感激.

Any clarification or suggestions for improvement of my implementation would be much appreciated.

推荐答案

基类 ctor 中的虚拟成员调用 可能 导致子类中的某些逻辑在调用子类的 ctor 之前运行(从而在对象有机会将自身初始化为一致状态之前).

A virtual member call in the base class ctor could cause some logic to run in the subclass before the subclass' ctor is called (and thus before the object gets a chance to initialize itself to a consistent state).

这只是一个很好的提醒,所以你知道你正在做的事情可能会导致一些令人讨厌的意外行为.

It's just a nice reminder so you know you are doing something that could potentially cause some nasty unexpected behavior.

这篇关于构造函数中的虚拟成员调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆