Property same name as type - can i get rid of the explicit namespace? - c++-cli

This does not compile without explicit namespaces.
namespace A
{
public enum class Foo {};
public interface class Bar
{
public:
property A::Foo Foo
{
virtual A::Foo get();
};
};
} // namespace A
I used the common .NET/C# style to name the type and property identical here. The explicit namespaces are annoying, especially if they are nested/verbose in a real example.
Can i get rid of the explicit namespaces somehow?
Any way to "beautify" this? I tried using/typedef of the enum in the implementation of the property in the .cpp file to get the enum in the surrounding scope. No success.

Related

How to use other class enum properties in Qml

Given is a class ClassA which contains an enum:
class ClassA : public QObject
{
Q_OBJECT
QML_ELEMENT
public:
enum EnumClassA {
};
Q_ENUM(EnumClassA)
};
A second class has a property of ClassA::EnumClassA:
#include <ClassA.h>
class ClassB : public QObject
{
Q_OBJECT
QML_ELEMENT
Q_PROPERTY(ClassA::EnumClassA value READ ...)
public:
};
This works in Qml (meaning the property value can be used in Qml), but when using qmlint/qmlsc compiler the following message is shown:
error: Could not compile binding for value: Cannot generate efficient code for content pointer of non-QVariant wrapper type double of ClassB::value with type ClassA::EnumClassA
A dirty fix could be this:
// replace "Q_PROPERTY(ClassA::EnumClassA value READ ...)" with:
Q_PROPERTY(int value READ ...)
but it would be much nicer to keep the type in the property.
Is it possible to modify the enum so that it can be compiled?
Regards,

Setting value of inherited trivial property in the constructor triggers Code Analysis Warning CA2214

I define an interface containing a trivial/simple property and an implementing class that sets the property in its constructor:
interface class IMyInterface
{
public:
property System::String^ MyName;
};
ref class MyImplementingClass : public IMyInterface
{
public:
virtual property System::String^ MyName;
MyImplementingClass()
{
MyName = "Test Name";
}
};
This doesn't seem too contentious, but when I run Code Analysis in Visual Studio 2019 using the default 'Microsoft Mixed (C++/CLR) Recommended Rules' ruleset it triggers warning CA2214:
warning CA2214: Microsoft.Usage : 'MyImplementingClass::MyImplementingClass(void)' contains a call chain that results in a call to a virtual method defined by the class. Review the following call stack for unintended consequences:
warning CA2214: MyImplementingClass..ctor()
warning CA2214: MyImplementingClass.set_MyName(String):Void
So I think it's complaining because setting this property involves calling the automatically implemented method set_MyName() and since that method is virtual it is considered a violation to call it from a constructor.
Does that mean this whole pattern is invalid?
What is the correct way to set the value of an inherited property at construction time?

unable to chain up to base constructor requiring arguments

public class Font : SDLTTF.Font {
public Font (string _filename, int _size) {
}
public void draw () {
}
}
That's my code. When I try to build it, I get:
Font.vala:4.5-4.15: error: unable to chain up to base constructor requiring arguments
public Font (string _filename, int _size) {
^^^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)
I thought I needed to override the constructor, so I tried to public override it, but now I get:
Font.vala:4.5-4.24: error: abstract, virtual, and override modifiers are not applicable to creation methods
public override Font (string _filename, int _size) {
^^^^^^^^^^^^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)
Any ideas on how to fix this? I'm trying to inherit the SDLTTF.Font class.
Have you tried putting
base(_filename, _size);
in your constructor?
EDIT:
This worked for me. Note however that SDLTTF.Font is defined in the vapi as a compact class, meaning that when you derive it, you're only allowed to define new functions for your subclass, but no instance data (member variables, etc.). If you require this, I'd recommend you go with apmasell's suggestion and create a wrapper class deriving from (G)Object.
SDLTTF is not managed by GObject, so Vala cannot create a derived class. Vala can only create derived classes if they make use of GObject, as is typical in GLib, GTK+, Pango, ATK, and many GNOME libraries.
Depending on what you want to do, you could create a new class that contains an instance of SDLTFF.Font and proxy the appropriate requests.

How to make my class implement from IMyInterface<T>?

I want to port the following code from c# to c++/cli:
class MyClass : IEnumerable<int> { ... }
I've tried
class ref class MyClass : IEnumerable<int>
but it doesn't seem to be working.
C++ has multiple types of inheritance. Don't forget to specify it, e.g.:
ref class MyClass : public IEnumerable<int>
{ };
In C++/CLI, I frequently find myself spelling out the full namespace in interface implementations. E.g.:
ref class MyClass :
public MyCompany::MyProject::MyComponent::IMyInterface
{ };
If your class doesn't actually implement the provided interface, you'll also (of course) get an error. And you'll want to remove the ^ from the class declaration. You're inheriting from an interface, not from a GC Handle to an instance of that interface.
Assuming your code is exactly what you're trying to compile, you have an extra class there. It should be just ref class, and not class ref class. Also, don't forget to translate any C# using statements to C++ using namespace, i.e.: using namespace System::Collections::Generic.

Is it Possible to Force Properties Generated by Entity Framework to implement Interfaces?

Example Interface:
public Interface IEntity
Property ID() as Integer
end Interface
I want all my EF objects to implement this interface on there Primary Keys.
Is this possible?
This seems very easy to do in CSharp but in VB you have to specifically declare which Properties/Functions/Subs are Implementing the Interface:
public Property Id() as Integer Implements IEntity.Id
Unfortunately I had to Rip out the designer file and modify the generated properties. I ended up getting rid of the Generated File all together and now keep my Models in separate classes with all of the Attribute mappings.
Yes, you can. The classes that the designer generates are declared partial. In a seperate source file you can declare additional methods for these classes. You can also declare specific interfaces that are already implemented by the generated class.
/* This is the interface that you want to have implemented. */
public interface ISomething
{
void DoSomething();
}
/* This would be part of the generated class */
partial class PartialClass
{
public void DoSomething()
{
}
}
/* This would be your own extension */
partial class PartialClass : ISomething
{
}
The classes are partial, so it should be very easy to do.