I have the below code in my C# program:
namespace test
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string val = "0";
parent o1 = new parent();
}
}
public class parent
{
string test1()
{
string val = "2";
return val;
}
}
public class child:parent
{
string test1()
{
string val = "3";
return val;
}
}
}
How to access the method test of class parent?
I tried by creating object of parent but unable to access the method.
what's wrong in my code?
You need to add Access Modifier. Use internal or public with method because if you not mention it then it will be consider as private
public class parent
{
internal string test1()
{
string val = "2";
return val;
}
}
You have missed the public access modifier before the method declaration. By default the method is private
Related
I'm just learning C# after using VBA for many years, I'm not a professional and this is something I do in my leisure. I'm looking to replicate the logic of using a public variable that can be accessed from a method and incremented by one each time when clicking button cmdPublicVartest , Below is the code I have so far, but am getting the error
An object reference is required for the non-static field, method, or property, in the publicvar class, it looks because it's a static class, however if I remove it from a static class, I would have to call an instance of the class on the button cmdPublicVartest. Is there a way I can keep publicvar a static class, so I don't have to do an instance of the class on the button?
namespace testDB
{
public partial class Database : Form
{
public string publictest = "public test";
public int pUblicint = 0;
public static void PublicVar()
{
MessageBox.Show(publictest + pUblicint);
pUblicint++;
}
private void cmdPublicVartest_Click(object sender, EventArgs e)
{
testDB.Database.PublicVar();
}
}
}
You cannot access non-static fields from a static method because they belong to an instance of the class, and when calling a static method you do not have an instance.
You could either make the fields static like this
public partial class Database : Form
{
public static string publictest = "public test";
public static int pUblicint = 0;
public static void PublicVar()
{
MessageBox.Show(publictest + pUblicint);
pUblicint++;
}
private void cmdPublicVartest_Click(object sender, EventArgs e)
{
testDB.Database.PublicVar();
}
}
Or make the method non-static like this
public partial class Database : Form
{
public string publictest = "public test";
public int pUblicint = 0;
public void PublicVar()
{
MessageBox.Show(publictest + pUblicint);
pUblicint++;
}
private void cmdPublicVartest_Click(object sender, EventArgs e)
{
PublicVar();
}
}
Just revise your code to this:
namespace testDB
{
public partial class Database : Form
{
public string publictest = "public test";
public int pUblicint = 0;
public void PublicVar()
{
MessageBox.Show(publictest + pUblicint);
pUblicint++;
}
private void cmdPublicVartest_Click(object sender, EventArgs e)
{
PublicVar();
}
}
}
While PublicVar() is static, the fields it references (publictest, pUblicint) are not. You have to make them static as well or make PublicVar() not-static.
I have 2 classes where 1 class is like the main class and I have a secondary class which is instantiated from the main class. How am I able to use the main class' method from the secondary class. Here's code to give an illustration of what I want.
public class MainClass
{
private SecondaryClass secondaryClass;
private int testValue;
public MainClass()
{
this.secondaryClass = new SecondaryClass();
testValue = 0;
}
public void updateTestValue (int val)
{
testValue = val;
}
}
public Class SecondaryClass : Form
{
public SecondaryClass()
{
}
private void button1_click(Object sender, EventArgs e)
{
// I want to be able to do this:
primaryClass.updateTestValue(100);
}
}
You can make classes communicate without having one derive from another.
public class MainClass
{
private SecondaryClass secondaryClass;
private int testValue;
public MainClass()
{
this.secondaryClass = new SecondaryClass(this.UpdateTestValue);
testValue = 0;
}
public void UpdateTestValue (int val)
{
testValue = val;
}
}
public class SecondaryClass : Form
{
private Action<int> UpdateValue { get; }
public SecondaryClass(Action<int> updateValue)
{
this.UpdateValue = updateValue;
}
private void button1_click(Object sender, EventArgs e)
{
this.UpdateTestValue(100);
}
}
In this organization, primary class is passing a delegate to its own instance-level method when it creates the secondary class. Secondary class calls that delegate when appropriate, without ever knowing what function that is.
This is the example of the callback pattern.
There are other variants of the same idea. For example, primary class could implement an interface which defines the UpdateValue method. Then, it passes this reference to every object which needs access to that method. Other objects, like an object of secondary class, would then simply call a method of that interface, once again not knowing that it is in fact the primary class they are referencing.
public interface IListener
{
void Update(int value);
}
public class MainClass : IListener
{
private SecondaryClass secondaryClass;
private int testValue;
public MainClass()
{
this.secondaryClass = new SecondaryClass(this);
testValue = 0;
}
public void Update(int val)
{
testValue = val;
}
}
public class SecondaryClass : Form
{
private IListener Listener { get; }
public SecondaryClass(IListener listener)
{
this.Listener = listener;
}
private void button1_click(Object sender, EventArgs e)
{
this.Listener.Update(100);
}
}
The price of this solution is one additional type in the system (interface IListener), and the benefit is that you can avoid working with delegate syntax. Delegates have a drawback that their arguments have no names, and therefore you can easily make a bug if you mix them up.
public class MainClass: Form
{
private int testValue;
public MainClass()
{
testValue = 0;
}
public void updateTestValue (int val)
{
testValue = val;
}
}
public class SecondaryClass : MainClass
{
public SecondaryClass()
{
}
private void button1_click(Object sender, EventArgs e)
{
// I want to be able to do this:
updateTestValue(100);
}
}
A class can only have one base class
What you could do is move the :Form base class up to the primary class and then from your secondary class have it's base class as Primary class and use the functions as follows.
public class PrimaryClass : Form
{
private int testValue;
public void PrimaryClassMethod()
{
Console.WriteLine("Method from Primary Class");
}
public void UpdateTestValue (int val)
{
testValue = val;
}
}
public class SecondaryClass : PrimaryClass
{
public void CallPrimaryClassMethod()
{
this.PrimaryClassMethod();
this.UpdateTestValue(10000);
}
}
https://dotnetfiddle.net/PC2WVu
I have a class P as part of namespace D with several fields and related properties
namespace Driver
[Export(typeof (P))]
public class Pilot : Send
{
private bool _b1;
...
public bool B1
{
get { return _b1; }
private set
{
if (_b1 != value)
{
_b1 = value;
NotifyOfPropertyChange(() => B1);
}
}
}
And then another class in the same namespace with some methods
namespace Driver
public class PilotEng
{
public void Statistics()
{
....
}
public void Running()
{
....
}
What is the best way to access and use the parameters of class P in class PE methods?
There are many ways for PilotEng to access information from Pilot.
Pass in instance of Pilot at PilotEng construction:
public class PilotEng
{
private Pilot myPilot;
public PilotEng(Pilot pilot)
{
myPilot = pilot;
}
public void Statistics()
{
var whatever = myPilot.B1;
....
}
public void Running()
{
....
}
}
somewhere else...
public void SomeMethod()
{
Pilot p = new Pilot();
PilotEng pe = new PilotEng(p);
pe.Statistics();
}
update your method signature(s) to take in an instance of pilot to work with:
public class PilotEng
{
public void Statistics(Pilot pilot)
{
var whatever = pilot.B1;
....
}
public void Running()
{
....
}
}
somewhere else...
public void SomeMethod()
{
Pilot p = new Pilot();
PilotEng pe = new PilotEng();
pe.Statistics(p);
}
Both are valid, one may be more valid than another, and there are several other ways to accomplish this. It all depends on what you're actually trying to do.
I was able to access show() method in another class
class program
{
int num;
string name;
void store()
{
num = 1;
name = "pc";
}
private class heai
{
public void show()
{
Console.WriteLine("hai");
}
}
void display()
{
store();
Console.WriteLine(num + name);
//how to access show() here ??
}
}
You need to have an instance from heai to access show.
Like this:
void display()
{
var h = new heai();
store();
Console.WriteLine(num + name);
h.show(); // here you are calling show on the instance of heai
}
By the way: this has nothing to do with nested classes. It would be the same for any other class.
In case you don't want to have an instance of heai, you could make show static:
private class heai
{
public static void show()
{
Console.WriteLine("hai");
}
}
And then call it like this:
void display()
{
store();
Console.WriteLine(num + name);
heai.show(); // note: we are not accessing an instance of heai here, but the class itself
}
Change the access modifier of the class to public or protected.
a simple question :
public class class1
{
public string string1;
public class class2
{
public string string2
{
get{ string tmp = class1.string1; }
}
}
}
I want to be able to reach class1.string1 from class2.string2.get, but I cant. What would you recommend me to change, so that I can do that?
Thanx
Passing class1 reference to class2 in constructor:
public class class1 {
public string string1;
public class class2 {
private class1 _Reference;
public class2(class1 reference) {
if (reference == null) {
throw new ArgumentNullException("reference");
}
_Reference = reference;
}
public string string2 {
get { return _Reference.string1; }
}
}
}
Passing class1 reference to class2 after both classes have been created:
public class class1 {
public string string1;
public class class2 {
private class1 _Reference;
public class1 Reference {
set { _Reference = value; }
}
public string string2 {
get { return _Reference.string1; }
}
}
}
static void usage() {
var foo = new class1();
var bar = new class1.class2();
bar.Reference = foo;
string value = bar.string2;
}
There is no means of accessing a class from within a nested class that I know of. Class nesting doesn't lead to automatic instantiation of the surrounding class, it's just a (usually rather smelly) means of structuring your code.
You would either need a reference to an actual instance of Class1 inside Class2 or you'd need a static method on Class1.
Another way to accomplish this would be to use inheritance, but that's a whole different beast to tame:
public class Class1 {
protected String String1 { get; set; }
}
public class Class2 : Class1 {
public String String2 {
get {
String PropertyFromClass1 = base.String1;
// ...
}
}
}
That said: Your code wouldn't compile, string2's getter doesn't return anything. And please make yourself familiar with C#'s naming conventions.
Thanx for the suggestions. Due to the specific nature of the code, I had to solve this situation with a global public static class in another namespace.
Coming from Java I faced this "problem" when I started developing in C#.
As clearly explained by Dennis Traub and in this article in C# you can't access outer class members or methods. So you have to implement what in Java happens automatically:
class OuterClass {
string s;
// ...
class InnerClass {
OuterClass o_;
public InnerClass(OuterClass o) { o_ = o; }
public string GetOuterString() { return o_.s; }
}
void SomeFunction() {
InnerClass i = new InnerClass(this);
i.GetOuterString();
}
}