How to pass parameter to member variable by ref?


JS

Pretty new to C#, coming from a C++ background a long time ago, so I seem to be having trouble transitioning from pointers to refs in C#.

I have a class (EColour) created with the constructor shown.

I assign (or at least try to) a reference to cellTemplate to the variable m_template.

Looking in debug, at build time, m_template is definitely not empty.

However, when I start handling the OnMouseClick event, I get a null exception error because m_template has magically become null.

Can anyone please shed some light on what I am doing wrong and how to fix it?

public EColour(ref ICellTemplate cellTemplate)
{
    m_template = (ColourTemplate)cellTemplate;
}

protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
    ColorDialog dlg = new ColorDialog();
    dlg.AnyColor = m_template.AnyColour; // This throws an exception because m_template is null
    
    base.OnMouseClick(e);
}

ColourTemplate m_template;
Klaus' pride

In C# we have two main types:

value type - its all digit types (int, float, double, long ...)
reference type -its types that inherited from object

ICellTemplateis a reference class. So what you need - just send it in the parameter as a regular variable.

public class EColour
{
    private ColourTemplate m_tamplate;

    public EColour(ICellTemplate cellTemplate)
    {
        m_template = (ColourTemplate)cellTemplate;
    }
}

Related


How to pass member function as parameter?

bad user C++ syntax is killing me. I tried to pass this+ pointer to member function: so I did the following: template <void(Myclass::*func)()> static void Myfunction(Myclass* theThis) { theThis->*func(); } This is good. But now I want to pass from this fu

How to pass member function as parameter?

bad user C++ syntax is killing me. I tried to pass this+ pointer to member function: so I did the following: template <void(Myclass::*func)()> static void Myfunction(Myclass* theThis) { theThis->*func(); } This is good. But now I want to pass from this fu

How to pass member function as parameter?

bad user C++ syntax is killing me. I tried to pass this+ pointer to member function: so I did the following: template <void(Myclass::*func)()> static void Myfunction(Myclass* theThis) { theThis->*func(); } This is good. But now I want to pass from this fu

How to pass member function as parameter?

bad user C++ syntax is killing me. I tried to pass this+ pointer to member function: so I did the following: template <void(Myclass::*func)()> static void Myfunction(Myclass* theThis) { theThis->*func(); } This is good. But now I want to pass from this fu

How to pass member function as parameter?

bad user C++ syntax is killing me. I tried to pass this+ pointer to member function: so I did the following: template <void(Myclass::*func)()> static void Myfunction(Myclass* theThis) { theThis->*func(); } This is good. But now I want to pass from this fu

How to pass member function as parameter?

bad user C++ syntax is killing me. I tried to pass this+ pointer to member function: so I did the following: template <void(Myclass::*func)()> static void Myfunction(Myclass* theThis) { theThis->*func(); } This is good. But now I want to pass from this fu

How to pass member function as parameter?

bad user C++ syntax is killing me. I tried to pass this+ pointer to member function: so I did the following: template <void(Myclass::*func)()> static void Myfunction(Myclass* theThis) { theThis->*func(); } This is good. But now I want to pass from this fu

Eigen::Ref<> as member variable

decoder I need a class with Eigen::Ref variable as static member which will be initialized by init static method . Something like this: class CostFunction { public: static Eigen::Ref<Eigen::VectorXd> data; static void init(const Eigen::Ref<Eigen::Vec

How to pass overloaded member function as parameter?

b Here's the problem I'm facing: I have an overloaded function in a class, and I want to pass one of its overloads as a parameter. But when doing so, I get the following error: "no suitable constructor exists to convert from <unknown-type> to std::function<...

How to pass overloaded member function as parameter?

b Here's the problem I'm facing: I have an overloaded function in a class, and I want to pass one of its overloads as a parameter. But when doing so, I get the following error: "no suitable constructor exists to convert from <unknown-type> to std::function<...

How to pass overloaded member function as parameter?

b Here's the problem I'm facing: I have an overloaded function in a class, and I want to pass one of its overloads as a parameter. But when doing so, I get the following error: "no suitable constructor exists to convert from <unknown-type> to std::function<...