This question already has answers here:
What does the caret (‘^’) mean in C++/CLI?
(8 answers)
Closed 9 years ago.
Tell me please what does ^ character in the code below:
[AttributeUsage (AttributeTargets::Class)]
public ref class ControlDescriptionAttribute : Attribute
{
public:
ControlDescriptionAttribute (String ^name, String ^description) :
_name (name),
_description (description)
{
}
property String ^Name
{
String ^get () { return _name; }
}
property String ^Description
{
String ^get () { return _description; }
}
private:
String
^ _name,
^ _description;
};
I have found it looking for something different, but have never met the usage of ^.
Can we start by telling you that is not C#? TOTALLY WRONG LANGUAGE - as you can see on the public ref. In C# that would be public class, not public ref class ;)
THat is C++/CLI and ^ is the indicator of a managed reference, the manged version of the unmanaged *
The properties erally are
String^ (managed pointer to a string).
I think you have got it confused with C++/CLI. As far as i know its not a typical usage in C#.
In simple terms, its just a pointer. In MSDN terms its a managed pointer. :)
Some references that may help:
http://msdn.microsoft.com/en-us/library/te3ecsc8(VS.80).aspx
Also look at the link supplied in the comment by #Lloyd.
Related
I have my Keyboard class:
namespace BSGameFramework
{
namespace Input
{
static public ref class Keyboard
{
public:
static KeyboardState GetState();
};
}
}
Where KeyboardState is a ref struct.
After compilation as dll from my C# application I see the function from metadata as follow:
namespace BSGameFramework.Input
{
public class Keyboard
{
public Keyboard();
public static void GetState(ref KeyboardState value);
}
}
Keyboard class has lost its static state and the function GetState is now returning void and taking a KeyboardState as referenced parameter!
Somebody know why? Thanks in advance :D
The reason is because of the return type, ref struct KeyboardState.
In C++/CLI, the "ref" vs. "value" is the thing that determines whether a type is a reference type or a value type, not "class" vs. "struct". ref class and ref struct are both the same thing as C#'s class. value class and value struct are both the same thing as C#'s struct.
Therefore, you have a C++/CLI method declared as returning a reference type, but without the ^. This data type does exist in C++/CLI, but not in C#. The method signature you see is a workaround.
There are two possible solutions to this issue:
Change KeyboardState to a value struct. From what you said, it sounds like you intended for this to be a value type from the beginning, so this is probably the best solution.
Change the return type of the method to KeyboardState^. This will let the method show up in C# the same as it does in C++/CLI. However, if you do this, you'll want to switch all uses of KeyboardState to KeyboardState^. It's a reference type, it should be used with a ^.
This question already has answers here:
method overriding in Java
(6 answers)
Closed 9 years ago.
I've got a question about overriding methods. OK, we've got an OOP here, I can understand what result I'll got. But.. How does the jdk resolve, what implementation to use in each case?
public class One {
One() {
run();
}
public void run() {
System.out.println("One");
}
}
public class Two extends One {
#Override
public void run() {
System.out.println("Two");
}
}
public class Test {
public static void main(String[] args) {
One test = new Two();
}
}
I'm really sorry for not very good code listing, I was in a hurry. Changes added.
First of all the way the classes have been declared is wrong and also static menthods do not take part in overriding, because static methods are not bound to objects.
Java is going to look up the function in the vtable for Two. If it's not found, it'll look in the vtable for One. In this case, it's found (and directly noted with #Override), so it's used.
https://stackoverflow.com/a/1543311/431415
Basically, it's going to go from most specific to least specific, looking for a function that matches.
I saw this post and I want to know if this is possible in VB.
So like extension method, do extension properties exists in VB.Net?
Here I've read they do, but cannot find any examples.
I believe that person is incorrect. From MSDN
You cannot define an extension property, field, or event.
This is almost possible. Learned this neat trick from Daniel Cazzulino.
You return a type from an extension method which exposes the properties. This is C#, but should be understandable.
public static class ListExtensions
{
// this extension method returns the type with properties
public static ListExtender<T> Extend<T>(this List<T> target)
{
//null check skipped
return new ListExtender<T>(target);
}
}
public sealed class ListExtender<T>
{
private List<T> _target;
// this is a pseudo extension property
public T First { get { return _target[0]; } }
public ListExtender(List<T> target)
{
_target = target;
}
}
Other than that, the answer is no.
According to the MSDN(draft) documentation for Visual Studio 11, extension properties are not available in VS 11 (i.e., .NET 4.5) either.
It's odd though searching does throw up a few instances where bloggers, etc., think it to be possible, including Ayende in an article on his blog here.
If I had a class with immutable members in Java, I would do this:
class MyClass {
private final String name;
private final int id;
myClass(String name, int id) {
this.name = name;
this.id = id;
}
String getName() { return name; }
int getId() { return id; }
}
In Progress-4GL, you'd typically see something like this: (Please, no lectures on Hungarian Notation. I hate it too, but it's very common in the Progress community, so it's something I just live with.)
CLASS MyClass :
DEFINE VARIABLE mcName as CHARACTER NO-UNDO.
DEFINE VARIABLE miId as INTEGER NO-UNDO.
CONSTRUCTOR PUBLIC MyClass(INPUT ipcName AS CHARACTER,
INPUT ipiId AS INTEGER):
ASSIGN mcName = ipcName
miId = ipiID.
END. /* constructor(char,int)*/
END CLASS. /* MyClass */
I was told in that in Progress 10.2B, they added the ability to make constants/final variables. However, I am unable to find any reference to it anywhere. In my Architect (version 10.2A) I do see that FINAL is considered a keyword. But the documentation behind it simply eludes me.
And if you've ever tried to search for Progress documentation, you know my dilemma.
How can I do immutable variables in Progress 10.2B? Are there any gotchyas I need to be aware of?
Thanks!
EDIT 1 I found documentation on FINAL. It appears to only apply to classes and methods. My current approach is
CLASS ImmutableString :
DEFINE PRIVATE VARIABLE mcValue AS CHARACTER NO-UNDO.
CONSTRUCTOR PUBLIC ImmutableString(INPUT ipcValue AS CHARACTER) :
ASSIGN mcValue = ipcValue.
END.
METHOD PUBLIC CHARACTER getValue() :
RETURN mcValue. /* Is a defensive copy required? */
END METHOD.
END CLASS.
You could also create a public property with a public "GET" and a private "SET":
DEF PUBLIC PROPERTY Value AS CHAR NO-UNDO
GET.
PRIVATE SET.
CONSTRUCTOR PUBLIC ImmutableString(INPUT ipcValue AS CHARACTER) :
Value = ipcValue.
END.
That's a little less code and does the same thing.
EDITED to change the property name to match the original poster's example.
I am attempting to simply add a FilterInfo class to my FilterInfo collection. I'm having a terrible time trying to understand why the following code keeps throwing the error:
System::Collections::Generic::List::Add'
: cannot convert parameter 1 from
'Ziz::FilterInfo *' to
'Ziz::FilterInfo'
I'm only learning C++/CLI, as I'm a C# developer, and I'm sure it's something simple, but I sure could use some pointers. My stripped code is as follows:
public value class FilterInfo
{
public:
char* Address;
};
public ref class Ziz
{
private:
List<FilterInfo>^ _blockList;
public:
// Constructor
Ziz(){
_blockList = gcnew List<FilterInfo>();
}
List<FilterInfo>^ GetBlockList()
{
for each(_IPFILTERINFO ip in _packetFilter->GetBlockList())
{
// _IPFILTERINFO is the native C++ struct.
FilterInfo* f = new FilterInfo();
_blockList->Add(f);
}
return _blockList;
}
You declared _blockList as
List<FilterInfo>^ _blockList;
but you are trying to add
FilterInfo* f
to it. It cannot work since one is a pointer and the other one is a reference.
I'm not sure how "value" fits in but in
public value class FilterInfo
{
public:
char* Address;
};
You are derefore declaring an unmanaged class
to make it managed, you should use
public ref class FiterInfo
This will allow you to use FilterInfo* without having to manage memory explicitely.
Finally, char* is not so great in C++/CLI, I would recommend using System::String
_blockList->Add(*f);
You have to construct your FilterInfo with gcnew as well. You can't really mix and mash these together without marshaling.
FilterInfo is not FilterInfo*. If you want a List of pointers to FilterInfo, you need to say that List<FilterInfo*>. Since FilterInfo is a value class here though you'll likely just want to skip the new.
FilterInfo fi;
_blockList->Add(fi);
public ref class A
{
};
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
ICollection<A^>^ oCollection = gcnew List<A^>();
oCollection->Add(gcnew A());
return 0;
}