Why does the C++/CLI compiler get confused so easily with symbols? - c++-cli

Here is my code:
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
namespace Tests {
ref class MyCollection : public IEnumerable<int> <----HERE!
The C# compiler, for instance, will recognize that the only IEnumerable<T> it has in those namespaces is from System::Collections::Generic. Why can't the C++/CLI compiler do the same? Unlesss I type its full name or at least Generic::IEnumerable<int>, it won't recognize it and will fire a C2872 error: ambiguous symbol.
Am I missing something here?

Given your namespaces IEnumerable is ambiguous
MS define IEnumerable in both System::Collections and System::Collections::Generic which of the 2 do you want your code to use?

Related

cppwinrt A subclass of Panel (or other class),What constructors are needed?

I'm sorry my English isn't very good.
I have a class like this
struct WrapPanel :winrt::Windows::UI::Xaml::Controls::PanelT<WrapPanel>
{
public:
WrapPanel(std::nullptr_t) {};
// other code.....
}
Use in other classes
WrapPanel wrapPanel{ ItemsPanelRoot().try_as<WrapPanel>()};
//Error C2440 'initializing': cannot convert from 'initializer list' to 'WrapPanel'
like:
https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/move-to-winrt-from-cx
Converting from a base runtime class to a derived one
If you author a runtime class in C++/WinRT, you'll get several classes with the same name in different namespaces. Let's suppose that there's a namespace named NS1, in which there's a runtime class called WrapPanel, you'll get winrt::NS1::WrapPanel, winrt::NS1::implementation::WrapPanel and winrt::NS1::factory_implementation::WrapPanel. The first one is the "projection" of the runtime class, which we usually use; the second one is the "implementation", which implements the runtime class; the third one is the "factory", which is used by module.g.cpp.
If you convert a base class to a derived one, you should use the "projection". It seems that you have used the "implementation".

VB Syntax of Baseclass Inheritance

I am wondering what the following snippets code means. Specifically the section: "Of EntityType As {BusinessEntity, New})". I'm a little new to VB inheritance so some of the syntax is a little foregn to me. I understand that a base class is being used to add functionality to all business objects that inherit from it but the syntax is throwing me off a bit. Is there a name for this design pattern?
Public Class AppObjectBase(Of EntityType As {BusinessEntity, New})
Inherits BusinessObject(Of EntityType)
...
...
Public Class NavTreeObj
Inherits NavTree(Of NavTreeEntity)
End Class
Public Class NavTree(Of EntityType As {NavTreeEntity, New})
Inherits AppObjectBase(Of EntityType)
...
...
Do you know what the Of EntityType part on its own means? If not then you should read about generic types. As for the As {BusinessEntity, New} part, that means that EntityType must either be or inherit from the BusinessEntity type and it must also have a parameterless constructor. By specifying those constraints on the generic type parameter, you are able to access members of the BusinessEntity type and also invoke the constructor to create new instances within the method.

Declaring an object without using the namespace of the class

I have a class library project named MyWidget, with the only class being named MyWidget.
In another project, I've added a reference to my class library, and in my new class, I've tried typing in
Imports MyWidget
and
Imports MyWidget.MyWidget
However, later in my class when I try to create a new reference, Visual Studio is not allowing me to type in this:
Private widget As MyWidget
However, Visual Studio is giving me a "Type Expected." error and forcing me to also include the namespace, like so:
Private widget As MyWidget.MyWidget
I read the MSDN documentation regarding the Imports statement. I should be able to leave off the namespace when declaring the object because I have the imports statement at the top of the program. I've tested this with standard namespaces, and it works fine, but when I try it out with my class, it doesn't.
Am I missing something in the MyWidget class that will allow me to leave off the namespace when declaring the object?
I also tried renaming the namespace to MyClasses, thinking maybe that Visual Studio was getting the namespace confused with the class. However, even with
Imports MyClasses.MyWidget
I still get an error when trying to define a MyWidget object without the MyClasses Namespace.
Since the Namespace and Class have the same name the compiler gets confused when you try to instantiate MyWidget, despite the Imports statement. Just because there is an Imports statement, doesn't mean you can't fully quanlify a type (even if you have Imports System.IO.File, you can still call System.IO.File.WriteAllText), thus the confusion on the compilers end. An alternative would be to use an Alias.
Imports AWidget = MyWidget.MyWidget
Then..
Dim objWidget As New AWidget
It appears that the issue was the namespace and the class having the same name. After changing the namespace to MyClasses and the class to MyWidget, the following statements worked:
Imports MyClasses
...
Private widget as MyWidget

Where is the Type class?

i just started learning vb.net.
But i couldnt find out where the System.Type class is.
I googled, but couldnt find any answers.
Here is what i did:
Module m
Sub Main(ByVal e as String())
Dim ass as Assembly = Assembly.LoadFrom(e(0))
Dim assobj as Type() = ass.GetTypes()
For Each m As Type In assobj
Console.WriteLine(m.Name)
next
I changed the directory to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727> and supplied System.dll as argument
But i couldnt find Type class
So,where is the Type class??
If anything is wrong,ignore my ignorance.
Thank you.
Hello again,
i got another problem,this whole thing i mentioned above is about making a console app similar to java's javap.exe utility
if you give the class fullname as argument then it should print substantial info about the class.
the problem is - how do i know which .dll file to load depending upon the class name i give as input???(this is the reason why i expected System.Type would be in System.dll file)
I used ILSpy (free tool) to have a look. It is in the mscorlib.dll
Like Michal and Mr Lister stated it's in mscorlib.dll.
I found it a different way though, if take your code
Dim assobj as Type() = ass.GetTypes()
and right click on the word Type and press "Go To Definition" (shortcut key F12)
The default display is slightly different for VB.net & C#. (For me anyway)
For VB.Net:
You'll see the Object Browser, you can notice that the Type is a Member of System
If you click on System you'll notice that it is a member of mscorlib. If you click on mscorlib, then you can see where the DLL is actually stored.
If you are using C# then you will see:
#region Assembly mscorlib.dll, v4.0.30319
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\mscorlib.dll
#endregion
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Security;
namespace System
{
// Summary:
// Represents type declarations: class types, interface types, array types,
// value types, enumeration types, type parameters, generic type definitions,
// and open or closed constructed generic types.
[Serializable]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(_Type))]
[ComVisible(true)]
public abstract class Type : MemberInfo, _Type, IReflect
{
//snip
}
}

C++/CLI equivalent of C#'s 'readonly' keyword

What's the C++/CLI equivalent of C#'s readonly keyword?
Specifically, how do you write a public or protected member of a C++/CLI class such that it is readonly when referenced from C#?
I just found out (thanks to Literal field versus constant variable in C++/CLI) that
C# const is literal in C++/CLI, and
C# readonly is initonly in C++/CLI