Hi,
Does anyone know why the class RuntimeType is referencing the same class name but in brackets? as below...
RuntimeType type = (RuntimeType) base.GetType();
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Apr 4, 2011, 10:59 AM
Dim [Me] As Integer = 3
Here 'Me' is an ordinary integer variable and not the keyword 'Me' which means the same as 'this' in C#.
C# has a similar feature where the keyword is preceded by @:
int @this = 3;
Needless to say, this is normally very poor programming practice and should only be used in exceptional circumstances.
Here's an example which uses casting between base and derived classes:
Sam HobbsPosted Apr 4, 2011, 7:36 PM
Also, so as to not dwell too far off topic, I hope it is sufficient to say that it often helps to be clear about what is meant.
You have certainly answered the question much more thoroughly than I could.
VulpesPosted Apr 4, 2011, 7:11 PM
Sam HobbsPosted Apr 4, 2011, 6:05 PM
- () parentheses
- [] square brackets
- {} curly brackets
People can get confused by terminology different from everyone else. I was quite surprised to see the quote "Usually you use brackets to differentiate" .... but then I was not surprised that the word brackets was refering to square brackets.If the English have redefined English, then is there someplace where the new definitions exist? I especially need to know about correct punctuation, since as editor in this web site, I always change ":-" to ":"; am I incorrect?
Posted Apr 4, 2011, 11:19 AM
RTT rtt2 = (RTT)s; // explicit conversion - Is the part the part I needed to know, what with the brackets around the class name, I should of known better! : )
Posted Apr 4, 2011, 10:27 AM
"Usually you use brackets to differentiate a Method, property, variable, parameter or other name from a keyword.
For example, you could make a parameter to a method be something like "Me" or "Overloads." Then you'd need to put brackets around it in the method declaration and everywhere you use it.
You can generally avoid the brackets if you pick better names"
Regards,
Posted Apr 4, 2011, 10:16 AM
For arguements sake how could I re factor my code in the most simplist form in order to enable me to use the () around the class... for test purposes...
Regards,
VulpesPosted Apr 4, 2011, 8:28 AM
Firstly, static methods have no concept of the 'current' instance, remembering that they refer to the class as a whole. This means that you can't call an instance method from a static method; nor can you use 'this' or 'base'.
Secondly, even if you placed your code in an instance method, it still wouldn't work. It would only work if your Sype class inherited from the Type class but, as the latter is abstract, Sype would need to implement a whole bunch of abstract members which isn't a practical proposition.
The RuntimeType class does all this but, as it's internal to mscorlib.dll, it can't be accessed directly from your code except by using reflection.
Posted Apr 4, 2011, 7:17 AM
Just been trying to recreate the Polymorphic process myself but with no luck...
This is what im trying:-
RTT rtt = (RTT)base.GetType();
The RTT class inherits from a class name Sype which contains the following public abstract void GetType() { }
It continues to say "Keyword base is not available in a static method"
I have tried changing the GetType method to static and non static but neither work?
Regards
Posted Apr 4, 2011, 5:42 AM
VulpesPosted Apr 4, 2011, 5:32 AM
So, you need an actual instance to call it and here 'base' is used to represent the base class instance.
The brackets are needed because base.GetType() returns a Type reference (even though it's a RuntimeType) and you therefore need to cast it to RuntimeType to use it as such.
Polymorphic OO programming is great, eh ;)
Posted Apr 4, 2011, 5:20 AM
So could it also be RuntimeType type = RuntimeType.GetType(); or are the brackets there because its the same class type?
VulpesPosted Apr 4, 2011, 5:03 AM