Hello, I am pretty new to C# (but I have used java)

I want to use my c# class in a c++ project, this means I will need to make a COM object, It seems reasonably easy I have been using this mainly as a template. My program was a c# dll, so I am converting it.

I have been using the c# getter and setters, for example:

public int Inlet
        {
            get
            {
                return this.inlet;
            }
            set
            {
                this.inlet = value;
            }
        }

but, this, as far as I can tell, isn't a methodor a variable, (it uses the variable inlet, but it is actually two functions "merged" into one)

I can't put it in my COM interface:

[Guid("1061ABA6-19E0-4ed2-A253-9C28D89771D7")]
    public interface IGC7890Controller
    {
        //Getters and setters
        public int Column;
        public int Inlet;
        
        //other methods
    }

because they look like variables to the compiler and I get "error CS0525: Interfaces cannot contain fields"

Is there any way to not change all my getters and setters into specific GetFoo() and SetFoo(), am I being stupid and missing something?

Thanks

Although a property in C# is coded in a single block the compiler creates a get_<propname> function and set_<propname> method separately.

To code a property in an interface you need to add the get and set keywords in braces.

[Guid("1061ABA6-19E0-4ed2-A253-9C28D89771D7")]
    public interface IGC7890Controller
    {
        //Getters and setters
        int Column { get; set; }
        int Inlet  { get; set; }
        
        //other methods
    }

BTW: You also do not need the public keywords (in fact it is an error to include them).

commented: thanks for the help - its not knowing key words like "property" that makes finding answers hard! +1

Your "getters and setters" are called properties. You can declare them in an interface (see MSDN for example). I haven't worked with COM classes for C++ so i don't know if there are additional restraints on the interface you create...hope this gets you in the right direction at least.

Thanks, I'll try that!

I have a property Mode:

public enum GCMode
    {
        SPLITLESS,
        SPLIT,
        PULSED_SPLITLESS
    };

protected GCMode mode;
public GCMode Mode
        {
            get
            {
                return this.mode;
            }
            set
            {
                this.mode = (GCMode)value;
            }
        }

now, I cant use my GCMode enum out of the COM C# project, so when i want to set my mode, i want to input an int "0" for "Splitless" etc, but I cant cast them to GCMode in my C++ application using the COM object, my question is this:

Do properties reject all input that is not of the property type? is it not possable to call a set part of the code and deal with other inputs?

so i can have:

public GCMode Mode
        {
            get
            {
                return this.mode;
            }
            set
            {
                this.mode = (GCMode)value;
            }
        }

instead of the uglier (and less intuative to use?):

public GCMode Mode
        {
            get
            {
                return this.mode;
            }
            set
            {
                this.mode = value;
            }
        }

        public void setMode(int i)
        {
            Mode = (GCMode)i;
        }

You can change the type declaration of your property:

protected GCMode mode;
public int Mode
        {
            get
            {
                return (int)this.mode;
            }
            set
            {
                this.mode = (GCMode)value;
            }
        }

When you access the property from COM it will accept and return an int value but in your class it is converted and stored as your enum type.

but I can't "overide" these, so if i call "if (mode == GCMode.SPLITLESS);" in my program lots, but "if (Mode == 1);" out of my program, I would want two propertes

GCMode Mode { get; set; }
int Mode { get; set; }

i get:

error CS0102: The type '...IGC7890Controller' already contains a definition for 'Mode'


I suppose I could use "mode == (int)GCMode.SPLITLESS", but its a bit messy :/

I have decided to just name the properties different things, I was getting stuck into thinking they had to be the same as the value (as you pointed out, they don't even have to be the same type as the value)

so ModeInt and ModeGC!

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.