1

Why is MainWindow/this.ab hidden and cannot be seen? I suppose private should be seen inside mainWindows.

It seems like C# throws error Not all code return path value if I put return not at the end of the method.

Can c# return string and void at the same time? If I am wrong, what is the better code? In PHP,the code easily works. I need to know how it can get to work in C#.

public static string a(string type,string a)
{
  return MainWindow.ab(type, a);
}
public static void a(string type)
{
  MainWindow.ab(type);
}
private string ab(string type,string a=null)
{
  if (type == "1")
  {
    return "1";
  }
}
Babu James
  • 2,740
  • 4
  • 33
  • 50
  • There are a number of problems here. First, instance methods (non-`static`) must be called with a reference to an instance of that class. Second, your non-`void` method must return a value on all code paths. What is the return value of `ab` if `type != "1"`? I'd suggest you start by reading [Methods (C# Programming Guide)](http://msdn.microsoft.com/en-us/library/ms173114.aspx) – p.s.w.g Jul 14 '14 at 05:43

2 Answers2

1

Better code is

public static string a(string type,string a)
{
  return MainWindow.ab(type, a);
}
public static string a(string type)
{
  return MainWindow.ab(type);
}
private static string ab(string type,string a=null)
{
  if (type == "1")
    return "1";
  else 
    return null;
}

Why does MainWindow/this.ab is hidden and cannot be see ?

Because method is not correct and is not static.

Can c# return string and void same time ?

No, you can return null instead using of void

Zheglov
  • 61
  • 8
0

If you want to use 'ab' method as you use it in your example, ab should be defined as follows inside MainWindow:

 public static string ab(...)

In regard of "return" error - not all execution paths return a string, e.g. if type != "1", return value is not provided.

Oleg Gryb
  • 5,122
  • 1
  • 28
  • 40
  • I want to hide it from outside view..Outside view only can see a method/function only. Either i change it as private or public it output error "returns void, a return keyword must not be followed by an object expression" – user2524126 Jul 14 '14 at 05:52
  • You can't return both 'void' and 'string' in one function, but you can use 'null' as another answer has suggested. In regard of visibility - if you make it 'private', it will be visible inside the class where you defined it, protected will make it available in derived classes as well, public - for everyone inside and outside. Make you mind, what you really want :) – Oleg Gryb Jul 14 '14 at 06:09