In java, not in c#

  • Thread starter Thread starter John Bailo
  • Start date Start date
J

John Bailo

Create a method BBB

private String BBB(String s)
{
return s + "hello";
}

Then call the method with

String BBB = BBB(company);


This will work in java, but not in c#;
 
John Bailo said:
Create a method BBB

private String BBB(String s)
{
return s + "hello";
}

Then call the method with

String BBB = BBB(company);


This will work in java, but not in c#;

Since 'company' is an undeclared variable, it won't compile in either.

Perhaps you'd like to construct a complete example?
 
Mike said:
Since 'company' is an undeclared variable, it won't compile in either.

Perhaps you'd like to construct a complete example?


try this then:

private String BBB(String s)
{
return s + "hello";
}

private void myCall()
{
String company="Texeme";
String BBB = BBB(company);
}
 
Tor Iver Wilhelmsen said:
You can work around the "feature" using this.BBB(company), but it's
still somewhat puzzling, yes.

It likely stems from delegate invocation syntax.

In C# its entirely legal to do something like:

MyDelegate BBB = new MyDelegate(this.BBB);
BBB("company");

Thus, an invocation expression on a variable is legal in *some* situations,
so the particular example given is ambiguous in *some* cases.

Instead of trying to guess, the compiler does the right thing and picks the
variable instead of the member function.
 
That's good for java.. It really is..

Practically however, if you where naming functions correctly, you would use
a verb for the method.. You know what a verb is, it's a doing word.. i.e.
GetSomething, DoSomething, CalculateSomething...
Compare that to a variable which is normal just a name...

Not only that, you should also consider clarity of code.. Most coding
standards (regardless of company) have different naming conventions that
prevent this sort of conflict, normally through casing and prefixing of
variables...

Personally, I think if you ever get confused with something like this, or if
you think this is a serious problem, then the code you are writing is most
likely very unmaintainable...

Just my 2c..

Eddie de Bear
 
Back
Top