Method

S

shapper

Hello,

I am converting some old code from VB.NET to C# using an online
converter but I still get a few errors:

public static Object Settings() {
return ConfigurationManager.GetSection("BonsAlunos");
}
public static ElementCollection GetAllElements(string section) {
ElementCollection elements =
Settings.GetType().GetProperty(section).GetValue(Settings, null);
return elements;
}

I get the following error:
MyApp.Config.Settings()' is a 'method', which is not valid in the
given context

On code line:
ElementCollection elements =
Settings.GetType().GetProperty(section).GetValue(Settings, null);

What am I doing wrong?

Thanks,
Miguel
 
A

Alberto Poblacion

shapper said:
I am converting some old code from VB.NET to C# using an online
converter but I still get a few errors:

public static Object Settings() {
return ConfigurationManager.GetSection("BonsAlunos");
}
public static ElementCollection GetAllElements(string section) {
ElementCollection elements =
Settings.GetType().GetProperty(section).GetValue(Settings, null);
return elements;
}

I get the following error:
MyApp.Config.Settings()' is a 'method', which is not valid in the
given context

On code line:
ElementCollection elements =
Settings.GetType().GetProperty(section).GetValue(Settings, null);

What am I doing wrong?

You have defined a method called Settings and then you are calling it
as if it were a property instead of a method.

The solution is to either change the method into a property:
public static Object Settings
{
get { return ConfigurationManager.GetSection("BonsAlunos"); }
}

Or to call it as a method instead of a property:
ElementCollection elements =
Settings().GetType().GetProperty(section).GetValue(Settings, null);

(but not both at the same time)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top