Today I had something weird happen in my copy of Resharper 5. I have a class that looks like this:
public class Foo
{
public string Username { get; private set; }
public Foo (string userName) { Username = userName; }
public void Bar()
{
DoWork(Username);
}
public void DoWork(string userName) { }
}
When I start to type DoWork(us I get the following from intellisense:
Notice that it's pulling up the constructor argument, and it ends with a colon: userName:
What's going on here?
EDIT:
As Reed answered below, this is a new C# 4 feature called Named and Optional Arguments. It's purpose is to allow you to specify the name of an argument, rather than it's position in a parameter list. so you don't have to remember the position of an argument in the argument list to use it (though this is largely meaningless with intellisense). It does make optional arguments easier to use though.
Thanks Reed.
This is Resharper providing intellisense support for Named and Optional Arugments.
C# 4 added support for these. You can now have a method defined like this:
public void DoWork(int someArgument = 3, string userName = "default")
{
// ...
If you want to call this with a different "userName" but leave the default for other parameters, you can do:
this.DoWork(userName: "FooUser");
Resharper 5 adds support for this syntax in intellisense.
You didn't include the ; at the end of userName
public Foo (string userName) { Username = userName; }
Related
I have some problem, so I have a few global fields in class, for each I want to do the same code but I don't want to repeat code - just use one method for that. And there I want to send these global fields in argument of this method and as second argument I want to send value for this field.
I tried with object, generic type but I don't know how to do that. Here is example:
private void setName(String _name) {
if(isNull(_name)) {
this.name = "";
} else {
this.name = _name.toString();
}
}
And a few other methods use the same code but with other fields and argument and I want to do something like that:
private void setField(some_field, _value) {
if(isNull(_value)) {
this.some_field = "";
} else {
this.some_field = _value;
}
}
Could someone help?
For example I have 2 global fields:
String name, int age.
For them I need to use the same code (if) and I want do it in one method. In this case I have to use global field as argument and as second argument use correct value for this field so instead:
this.name = argument;
this.age=argument;
use: globa_field_argument = argument;
Example:
setField( this.name, "Test" );
setField( this.age, 5 );
First off I would probably declare your global fields as 'public static'.
for example:
public static int myNumber = 10;
public static String myString = "foo";
Secondly you should have to pass your fields as an argument since you can access them inside the method itself.
When you redefine the fields anywhere in your code you will do this:
this.myNumber = newNumber;
this.myString = newString;
Basically, I want to override a parent class with different arguments. For example:
class Hold<T> {
public var value:T;
public function new(value:T) {
set(value);
}
public function set(value:T) {
this.value = value;
}
}
Then override that class, something like:
class HoldMore extends Hold<T> {
public var value2:T;
public function new(value:T, value2:T) {
super(value);
set(value, value2);
}
override public function set(value:T, value2:T) {
this.value = value;
this.value2 = value2;
}
}
Obviously this will return an error, Field set overloads parent class with different or incomplete type. Is there a way around this? I tried using a public dynamic function, and then setting set in the new() function, but that gave a very similar error. Any thoughts?
This is just a complement to #stroncium's answer, which is totally correct.
Here is an example how it could look like:
class Hold<T> {
public var value:T;
public function new(value:T) {
set(value);
}
public function set(value:T) {
this.value = value;
}
}
class HoldMore<T> extends Hold<T> {
public var value2:T;
public function new(value:T, value2:T) {
super(value);
setBoth(value, value2);
}
// you cannot override "set" with a different signature
public function setBoth(value:T, value2:T) {
this.value = value;
this.value2 = value2;
}
}
alternatively, you could use an array as parameter or a dynamic object holding multiple values in order to "set" them using the same method, but you loose some of the compiler's type checking.
If you wrote the base class you could add an optional argument to it, this would be a workaround though, not directly what you want to do.
In the current state it totally won't work. There is not only 1 problem, but few of them:
Type T is meaningless in context of this new class, you should either use some concrete type or template this class over T.
You can not change the number of arguments of function when overriding it. However you can add another function(with a different name) to accept 2 arguments and do what you want (which is the way you would use in most languages, by the way).
I don't really understand how you see a contravariance problem there. The actual problem is that haxe doesn't support function overload. (It actually does, the function signature is name + full type, but that's not what you would want to write nor support, and is mostly used for js/java externs.)
Unfortunately the language doesn't allow it.
This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
In C#, is "this" keyword required? [duplicate]
(6 answers)
Closed 8 years ago.
since many years i'm trying to figure out that what does 'this' do in c#.net
e.g.
private string personName;
public Person(string name)
{
this.personName = name;
}
public override string ToString()
{
return this.personName;
}
}
The this keyword allows you to explicitly refer to a member of the current instance.
In your case, you can just as easily leave it off - the rules in C# will find the member variable.
However, if you use a parameter with the same name as a member variable, or have a local with the same name, using this specifies which variable to use. This allows you to do:
private string personName;
public Person(string personName)
{
// this finds the member
// refers to the argument, since it's in a more local scope
this.personName = personName;
}
Tools like StyleCop enforce the use of this everywhere, since it completely removes any ambiguity - you're explicitly saying you want to set a member (or call a function, etc) within the current instance.
this refers to the object that the method belongs to. It can be used - like demonstrated in the other answers - for scope choosing. It can also be used when you want to use the current object as an entire object(that is - not a specific field, but the object as a whole) - for example:
public class Person{
private string name;
private Person parent;
public Person(string name,Person parent=null;){
this.name = name;
this.parent = parent;
}
public Person createChild(string name){
return new Person(name,this);
}
}
this refer to the instance of the class. Usually you don't use it as it becomes noise,but in some case it's important to use it.
public class Foo
{
private string bar;
public Foo(string bar)
{
//this.bar refer to the private member bar and you assign the parameter bar to it
this.bar = bar;
//Without the this, the bar variable in the inner scope bar, as in the parameter.ΒΈ
//in this case you are assigning the bar variable to itself
bar = bar;
}
}
I have a situation where I want the variable to be capitalized for documentation eg
(trivalized for example)
///AT+$COMMAND$
void At$COMMAND$()
{
}
So I want the user of the template to type in something like "Blah" and that gets used in the method name, but the documentation part gets changed to "BLAH".
eg
///AT+BLAH
void AtBlah()
{
}
Can I do this? I see in the macros I can capitalize the first letter, but I'd like the whole word capitalized. Is it possible to create custom macros?
They just updated documentation to meet changes in macros in Resharper 8. You can check it at http://confluence.jetbrains.com/display/NETCOM/4.04+Live+Template+Macros+%28R8%29
With the new docs it is quite easy, my implementation goes here:
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using JetBrains.DocumentModel;
using JetBrains.ReSharper.Feature.Services.LiveTemplates.Macros;
using JetBrains.ReSharper.Feature.Services.LiveTemplates.Hotspots;
namespace ReSharperPlugin
{
[MacroDefinition("LiveTemplatesMacro.CapitalizeVariable", // macro name should be unique among all other macros, it's recommended to prefix it with your plugin name to achieve that
ShortDescription = "Capitalizes variable {0:list}", // description of the macro to be shown in the list of macros
LongDescription = "Capitalize full name of variable" // long description of the macro to be shown in the area below the list
)]
public class CapitalizeVariableMacro : IMacroDefinition
{
public string GetPlaceholder(IDocument document, IEnumerable<IMacroParameterValue> parameters)
{
return "A";
}
public ParameterInfo[] Parameters
{
get { return new[] {new ParameterInfo(ParameterType.VariableReference)}; }
}
}
[MacroImplementation(Definition = typeof(CapitalizeVariableMacro))]
public class CapitalizeVariableMacroImpl : SimpleMacroImplementation
{
private readonly IMacroParameterValueNew _parameter;
public CapitalizeVariableMacroImpl([Optional] MacroParameterValueCollection parameters)
{
_parameter = parameters.OptionalFirstOrDefault();
}
public override string EvaluateQuickResult(IHotspotContext context)
{
return _parameter == null ? null : _parameter.GetValue().ToUpperInvariant();
}
}
}
The code sample below indicates that you can call private methods using property notation, e.g. val instead of getVal(), and presumably val = "something" instead of setVal("something")
class Foo {
String foo = val
private getVal() { "val"}
}
assert new Foo().foo == "val"
I'm aware that this style is "officially supported" for public methods, but is it's use for private methods a bug/quirk, which one should rely on (much like the ability to access private members from outside a class)?
I've probably got the wrong end of the stick, but doesn't the same thing happen in Java...
Can't see how that differs from having:
class PrivateTest {
public String foo = getVal() ;
private String getVal() {
return "val" ;
}
}
and then the test class:
public class PrivateMain {
public static void main( String[] args ) {
PrivateTest pt = new PrivateTest() ;
System.out.println( pt.foo ) ;
}
}
The test class will still print out val
now that you mention it...
i don't remember how private members are handled.
but at least in the 1.8 instance i have running (no security manager in place), you can do things like
println new Date().fastTime
println new Date().normalize()
-- edit
i should really pay more attention
in your example, new Foo().foo is just accessing a standard groovy property.
new Foo().val or new Foo().getVal() on the other hand, would actually access the private members.
-- edit 2
wow. i truly didn't remember GROOVY-3010