Cannot call public method from within same class in c# - c#

I am working on a Visual Studio 2015 project from another developer using his previous solution file (.sln). It has two assemblies, one which contains the class with the main method and the other which contains a single worker class. Within the class there is currently one method. If I add another public method (examples below) I cannot call that method from the first or any other method I write for the class. It seems there is something set in this project that is not allowing me to do this. If I start a new project everything works normally.
Here is a scaled down example of what is happening.
namespace MyPoject
{
public class MyClass1
{
public void MyMethod1()
{
Console.WriteLine("TEST");
}
public void MyMethod2()
{
//This does not work/complete with intelesense
//Error is "MyClass1.MyMethod1() must declare a body because it is not marked abstract, extern or partial.
MyMethod1();
}
public void MyMethod3()
{
//Nor can I use this
//Error is Invalid token 'this' in class, struct, or interface member declaration
this.MyMethod1();
}
}
}
Any help would be greatly appreciated as the the class is fairly large and I'd rather not recreate the whole project. If instead of adding a method to the class I create another class then instantiate it I can call methods within that object which does solve the problem but I hate when something doesn't work the way I expect it to so I'd love to solve this issue.

Related

CS0051 Inconsistent accessibility: parameter type 'CharityDbContext' is less accessible than method 'Campaign.Campaign(CharityDbContext)' [duplicate]

I'm trying to pass an object (a reference to the currently logged on user, basically) between two forms. At the moment, I have something along these lines in the login form:
private ACTInterface oActInterface;
public void button1_Click(object sender, EventArgs e)
{
oActInterface = new ACTInterface(#"\\actserver\Database\Premier.pad",this.textUser.Text,this.textPass.Text);
if (oActInterface.checkLoggedIn())
{
//user has authed against ACT, so we can carry on
clients oClientForm = new clients(oActInterface);
this.Hide();
oClientForm.Show();
}
else...
on the next form (clients), I have:
public partial class clients : Form
{
private ACTInterface oActInt {get; set;}
public clients(ACTInterface _oActInt)
...which results in me getting:
Error 1 Inconsistent accessibility:
parameter type 'support.ACTInterface' is less accessible than method
'support.clients.clients(support.ACTInterface)'
c:\work\net\backup\support\support\clients.cs 20 16 support
I don't really understand what the problem is - both fields are private, and accessed by the relevant public method from the form. Googling doesn't really help, as it just points towards one element being public and the other private, which isn't the case here.
Anybody help?
Constructor of public class clients is public but it has a parameter of type ACTInterface that is private (it is nested in a class?). You can't do that. You need to make ACTInterface at least as accessible as clients.
Make the class public.
class NewClass
{
}
is the same as:
internal class NewClass
{
}
so the class has to be public
If sounds like the type ACTInterface is not public, but is using the default accessibility of either internal (if it is top-level) or private (if it is nested in another type).
Giving the type the public modifier would fix it.
Another approach is to make both the type and the method internal, if that is your intent.
The issue is not the accessibility of the field (oActInterface), but rather of the type ACTInterface itself.
What is the accessibility of the type support.ACTInterface. The error suggests it is not public.
You cannot expose a public method signature where some of the parameter types of the signature are not public. It wouldn't be possible to call the method from outside since the caller couldn't construct the parameters required.
If you make support.ACTInterface public that will remove this error. Alternatively reduce the accessibility of the form method if possible.
parameter type 'support.ACTInterface' is less accessible than method
'support.clients.clients(support.ACTInterface)'
The error says 'support.ACTInterface' is less accessible because you have made the interface as private, at least make it internal or make it public.
The problem doesn't seem to be with the variable but rather with the declaration of ACTInterface. Is ACTInterface declared as internal by any chance?
When I received this error, I had a "helper" class that I did not declare as public that caused this issue inside of the class that used the "helper" class. Making the "helper" class public solved this error, as in:
public ServiceClass
{
public ServiceClass(HelperClass _helper)
{ }
}
public class HelperClass {} // Note the public HelperClass that solved my issue.
This may help someone else who encounters this.
You can get Parameter (class that have less accessibility) as object then convert it to your class by as keyword.
In my case I hadone class in a file and I was passing a instance of that class to the constructor of my form in another file.
The problem was had declared the class without the public modifier : class MyClass {}
I could have solved it by changing it to public class MyClass {}
If this error occurs when you want to use a classvariable in a new form, you should put the class definition in the
Formname.Designer.cs
instead of the Formname.cs file.
After updating my entity framework model, I found this error infecting several files in my solution. I simply right clicked on my .edmx file and my TT file and click "Run Custom Tool" and that had me right again after a restart of Visual Studio 2012.
All the answers that say make the type ActInterface as public are right. I am only putting this post to explicitly mention why that's an issue
If a parameter to your public class constructor is private or internal qualified class, it means you wont be able to create an object of that parameter class from outside of the assembly and when you cannot instantiate the parameter object, how can you call this constructor to instantiate an object of this class ?
Try making your constructor private like this:
private Foo newClass = new Foo();

How to call function from one page to another page in asp.net using c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to .NET and Web development too. I have website written in c#. I want to declare one class and in that class i want to declare one function and that function want to call on second page. So how to write code for that?
Say I have 2 pages. One is reference.aspx and second page is edituerprofile.aspx. In reference.aspx page i want to write class and in that class want to write one function. And that function want to call in edituserprofile.aspx.
How to write this?
pls help me.
Thanx in advance
Please follow the following steps
1)add new class file to your app_code folder which is created by visual studio itself
2)write, what are all the methods/functions you need, write on that class file
3)create an object in your asp.net page for that class
4)call the methode/function of class by using object.method/function_name();
thats it
Simple code friend...
string user_name="Some_user",password="correct_password";
login(user_name,password)
{
class_name object=new class_name();
if(true==object.methode_name(user_name,password))
// do_something
else
// do_something
}
in your class file
class class_name
{
public bool methode_name(string user_name,string password)
{
//your code here
if(/*yout code here to validate user*/)
return true;
else
return false;
}
}
Create a class as public in App_code.
Create object of that class, and then
get new instance of the class to that object.
Then you can access all public functions of that class by the object
Example
public class class1
{
public void function1()
{
// do your job
}
}
and in pages
class1 objclass1 = new class1();
objclass1.function1();
There are multiple approaches, depending on what you need.
The quickest way is create an static class in App_Code, and add the method in there
public static class Utils
{
public static void MyMethod() { }
}
Then you can call this MyMethod everywhere.
Try
public Page1: BaseClass
{
public void Method1()
{
base.BaseMethod();
}
}
public BaseClass: System.Web.UI.Page
{
//.. specify your method here
protected BaseMethod()
{
}
}
Try this,
Define common class library. like below
public abstract class PageBase : System.Web.UI.Page
{
public static void AddDropDownDefaultValue()
{}
}
And you are to used in page class file like below.
protected void BindStatus()
{
AddDropDownDefaultValue();
}
Don't use the codebehind classes as repository for all methods. Use separate classes with meaningful names for that.
You cannot access instance methods from another page since you don't have an instance of it (if you don't have used Server.Transfer to get to the second page, then you could use cast the PreviousPage property to the actual type).
So you could access it if the method is static. However, i would move methods that you need at several places into their own classes anyway as already mentioned.
A more concrete example of what you're trying to do would be useful.
Otherwise you'll get all sorts of answers, many of which will be off
the mark.
You should put common code in the App_Code folder. You should also not
have any business logic inside a forms code-behind.
The fact that you need one page to call a method in another page
indicates that you haven't done this. Pages are for displaying and
interpreting actions, but they should not hold any of the business
logic.

What must I do to access my own public method?

I have created folders in my project named Classes, Forms, and Models.
Let's say my project is named ABC, so the folder hierarchy is:
ABC
Classes
Forms
Models
In \Models\, I have a class named ApplicationModel.cs, which contains a public method named GetApplications().
However, when I call that method from elsewhere in the same ABC project, I get, "The name 'GetApplications' does not exist in the current context"
I've added:
using ABC.Models;
to the calling class, but it makes no difference. I right-clicked GetApplications() to hopefully see "Resolve" there, but no go.
What must I do to access my own public method?
It would be helpful to see the definition of GetApplications() and the code that's attempting to call it, but I assume it's either a static or an instance method of the ApplicationModel class. In either case, you may have made your code aware of the namespace of the ApplicationModel class with the using statement, but the method must either be called on the class or an instance of the class, like so:
If GetApplications is a static method,
var applications = ApplicationModel.GetApplications();
If it's an instance method:
var appModel = new ApplicationModel(); // or, retrieve the instance from elsewhere...
var applications = appModel.GetApplications();
One way or another, you must refer to the class containing GetApplications in order to call it. If this doesn't help you solve the problem, please edit your question to contain the definition of the method, and the calling code.
Sounds like you're using a static function. Did you forget the static keyword?
A static function "runs" from a class, not an object:
public static string[] GetApplications()
It is hard to give definitive advice without some code on how you are trying to call that method. I can think of two possible ways:
either you are trying to call the method via the ApplicationModel class(ApplicationModel.GetApplications()), in which case you need to declare the method static
or you need to call the method on an object, but you are using the type -- in this case declare/create an object of type ApplicationModel and call the method on that object; (e.g. ApplicationModel model = new ApplicationModel(); model.GetApplications();)
Looks like the class is not marked as public.
You class should be
namespace ABC
{
namespace Models
{
public class ApplicationModel //class needs to be public is accessed outside the namespace
{
}
}
}

One method shared by several classes

I have just one method that I need several different classes to access and it just seems lame to make a utility class for just one method. The classes that need to use this method are already inheriting an abstract class so I can't use inheritance. What would you guys do in this situation?
[I]t just seems lame to make a utility
class for just one method
Just do it, it will grow. It always does. Common.Utilities or something of that nature is always necessary in any non-trivial solution.
Keep in mind that a class is just a small, focused machine. If the class only has one method then it's just a very small, focused machine. There's nothing wrong with it, and centralizing the code is valuable.
There is a cheat that you can use :-)
Create an Interface that your classes can "implement" but, create an extension method on that interface, your classes then magically get that method without having to call the utility class...
public Interface IDoThisThing {}
public static void DoThisThingImpl(this IDoThisThing dtt)
{
//The Impl of Do this thing....
}
Now on your classes you can just add the IDoThisThing
public class MyClass, MyBaseClass, IDoThisThing
{
//...
}
and they Get that thing :-)
Note, this is only syntatic sugar around effectively a utility class, but it does make the client code very clean (as just appears as a method on your class).
What do you mean you can't use inheritance?
If you write the method in the abstract class, you can also write the implementation (not everything in an abstract class needs to be abstract).
But generally, it's advisable to have some sort of 'GeneralUtils' class; cause you end up with a few of these functions.
I'd need more info to give a definite answer.
However a well-named class with a single well-named method could work wonders for readability (as compared to an inheritance based solution for instance)
Since you use the term utility method, I'd say create a static class with the static method and be done with it.
can use extension methods...
namespace ExtendMe
{
public interface IDecorate { }
public static class Extensions
{
public static void CommonMethod(this IDecorate o) { /* do stuff */ }
}
public class Blah :IDecorate {}
public class Widget : IDecorate {}
class Program
{
static void Main(string[] args)
{
new Blah().CommonMethod();
new Widget().CommonMethod();
}
}
}

Problem with partial method C# 3.0

Do you know the answer to following question?
Let us say, it MyMethod() is declared
as partial method in MyPartialClass in
MyPartialClass.cs. I have also
provided body of MyMethod() in
MyPartialClass in MyPartialClass2.cs.
I use a problem without answer“Magic”
code generator which has actually
generated MyPartialClass.cs, let us
say based on some DB schema. Some
innocent guy changes schema for good
and then runs “Magic”.
MyPartialClass.cs is re-generated but
with MyMethod2() instead of MyMethod()
declaration. Think of me. I am
thinking that I have implemented
MyMethod() which is used by “Magic”
but in reality, “Magic” is using
MyMethod2(). C# compiler does not tell
me that I have partial method
implemented without any declaration
and my implementation is not used at
all!
Any solution?
I think it is a problem without an answer.
EDIT I got an answer :-). I had a typo in my code and that is why compiler was not flagging error. Jon already has pointed that out.
You should get error CS0759. Test case:
partial class MyClass
{
partial void MyMethod()
{
Console.WriteLine("Ow");
}
}
partial class MyClass
{
partial void MyMethod2();
}
Compilation results:
Test.cs(6,18): error CS0759: No defining declaration found for implementing
declaration of partial method 'MyClass.MyMethod()'
Does that not do what you want it to?
In short, no; that is the point of partial methods - the declaring code doesn't need to know whether an implementation is provided or not.
Of course - you could just not declare the partial method: consume it assuming it exists; if you don't provide it, the compiler will complain of a missing method.
There is a hacky way to check at runtime (with partial methods), which is to have the other half update a ref variable:
partial void Foo(ref chk);
partial void Foo(ref chk) { chk++;}
(and verify it changes) - but in general, partial methods are designed to not know if they are called.
Another approach is a base-class with an abstract method - then it is forced by the compiler to be implemented.
This is the whole purpose of partial methods. If the method is not implemented, it is removed without a trace, and without a warning.
One solution to this type of problem would be to use a double derived pattern in your code generation. This is used extensively by DSLTools and is quite powerful.
Write the following code by hand :
public class MyClassBase
{
public abstract void MyMethod();
//Put all other methods required by the class here.
}
public partial class MyClass : MyClassBase
{
//This class is entirely empty!
}
Generate the following code in magic.
public partial class MyClass
{
public void MyMethod(){}
}
If someone fails to implememnt MyMythod() in the generated code, you will get a compiler error.

Categories