How can I call method whithout creating class.
Example
public class1 {
class2 = new class2();
int size;
private void method() {
size = class2.size;
}
}
public class2 {
private void method() {
//call method from class1
}
}
You can do that making the method of class1 static (add the static reserved word before private)
That way you can call the method as class1.method();
Hope this is what you are looking for!
I mean it:
public Class1 {
Class2 class2 = new Class2();
public int size;
public Class1() {
class2.handler += method1;
}
private void method1() {
size = class2.size;
}
}
public Class2 {
...
public int size;
public delegate void Handler();
public Handler handler;
private void method2() {
size = UpdateSize();
handler?.Invoke();
}
private int UpdateSize() {
...
}
}
Related
I'm trying to do something, but I don't know if it's possible
I have these classes:
public Class1
{
private Class2 class2;
public Class1
{
this.class2 = new Class2();
}
public Class2 Do()
{
return class2.Build();
}
}
internal Class2
{
public internal Class2 Build()
{
return this;
}
public void Fill()
{
}
}
var class1 = new Class1();
class1.Do().Fill();
I want it when the user types class1.Do (). only the Fill () method is accessible. How can I do this?
The goal would be to prevent class1.Do().Build();
There are quite a few errors in the code posted as mentioned in a comment. For the sake of an example answer, I made all of the troublesome places public. So while this will compile, take it with a grain of salt and adjust your accessibility modifiers appropriately.
Firstly, I created a new interface to only expose Fill, and not Build:
public interface IClass2Helper
{
void Fill();
}
I made a couple of small changes to Class1 and Class2 so that they become compilable, to implement the interface in Class2 and return the interface in the Do() method in Class1:
public class Class1
{
private Class2 class2;
public Class1()
{
this.class2 = new Class2();
}
public IClass2Helper Do()
{
return class2.Build();
}
}
public class Class2 : IClass2Helper
{
public Class2 Build()
{
return this;
}
public void Fill() { }
}
And added a simple test class/method to verify:
class TestProgram
{
static void Main()
{
var class1 = new Class1();
class1.Do().Fill(); //This line is OK
//The below line will have an error since the interface doesn't expose a Build method
class1.Do().Build();
}
}
A fiddle to the code:
https://dotnetfiddle.net/5EPPfO
Here is the code I currently have, the question follows after:
class Program
{
static void Main(string[] args)
{
var obj1 = new A();
obj1.DoIt();
obj1.SetFlyBehavior(new BehaviorB());
obj1.DoIt();
string input = Console.ReadLine();
}
};
class BaseOfA
{
protected ObjectBehavior behavior;
public void DoIt()
{
behavior.DoIt();
}
public void SetBehavior(ObjectBehavior ob) {
behavior = ob;
}
};
class A : BaseOfA {
public A() {
behavior = new BehaviorA();
}
}
interface ObjectBehavior {
void DoIt();
}
class BehaviorA : ObjectBehavior {
void ObjectBehavior.DoIt() {
Console.WriteLine("Behavior: A");
}
}
class BehaviorB : ObjectBehavior {
void ObjectBehavior.DoIt() {
Console.WriteLine("Behavior: B");
}
}
Now my question is, in this case, how am I going to make it work so that I can assign both BehaviorA and BehaviorB to instance obj1 as long as they implement ObjectBehavior?
You are calling obj.SetFlyBehaviour this method is not defined anywhere. The method you define on BaseOfA is called SetBehaviour. Once that is fixed the code you gave compiles fine for me
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
This is a pretty beginner question but I'm stumped and I can't figure out how to get what I want from this. I have my first class that obtains information (database/textfile/whatever) but I want it to relay that information into Class2.
For instance, the first:
public class Class1
{
private int first;
private string firstString;
private bool isTrue;
public void SomeMethod()
{
first = 1;
firstString = "FirstString";
isTrue = true;
}
}
Here SomeMethod sets all the attributes that I need to pass into Class2.
ClassTwo looks like
public class Class2
{
private int first;
private string FirstString;
private bool isTrue;
private int second;
private string SecondString;
private bool isFalse;
public void SomeOtherMethod()
{
}
}
Here what I want is for SomeOtherMethod() to set the first set of attributes with the values that were set in Class1's SomeMethod(). So that I can create an object of type Class2 and add what I want to it.
As some other commentators stated, you really should reuse your data definitions. Something like this can get you started:
public class Class1
{
private int _myInt;
private string _myString;
private bool _myBool;
public void SomeMethod()
{
_myInt = 1;
_myString = "FirstString";
_myBool = true;
}
}
public Class2
{
private Class1 _first = new Class1();
private Class1 _second = new Class1();
public void SetFirst(Class1 obj)
{
_first = obj;
}
}
and then use the classes like this:
Class1 c1 = new Class1();
Class2 c2 = new Class2();
c1.SomeMethod();
c2.SetFirst(c1);
You have to define get accessors for the properties of Class1 because they are all unreachable from outside the class and Class2 needs to use their values. Defining public properties with get accessors can be useful:
private int first;
public int First
{
get
{
return first;
}
}
Having every property in Class1 defined like this, you can access the values. After calling SomeMethod, two objects' properties can be equalized in two simple ways (See also: Signatures and overloading):
public void SomeOtherMethod()
{
Class1 tempClass = new Class1();
tempClass.SomeMethod();
this.first = tempClass.first;
this.FirstString = tempClass.firstString;
this.isTrue = tempClass.isTrue;
}
public void SomeOtherMethod(Class1 myClass) // Overloaded method
{
this.first = myClass.first;
this.FirstString = myClass.firstString;
this.isTrue = myClass.isTrue;
}
Even though the techniques above seem like to be what you asked for, the best is to initialize a class's properties using constructors. This way, you don't have to call SomeMethod each time you create a Class1 object, and you can also set its default values whenever a new one is created. Also, giving more general names to the properties will save you from duplicates. I write some code to provide you an understandable syntax that will prevent future problems of non-accessibility and repetition.
public class Class1
{
private int number;
public int Number
{
get { return number; }
}
private string name;
public string Name
{
get { return name; }
}
private bool isTrue;
public bool IsTrue
{
get { return isTrue; }
}
public Class1()
{
number = 1;
name = "FirstString";
isTrue = true;
}
public Class1(int value1, string value2, bool value3)
{
number = value1;
name = value2;
isTrue = value3;
}
}
public class Class2
{
private Class1 firstClass;
private Class1 secondClass;
public Class2()
{
firstClass = new Class1();
secondClass = new Class1(2, "SecondString", false);
}
}
If you're going to define many Class1 objects in Class2, then a solution such as an array or a list becomes must. I'll give a short example, see MSDN List page.
private List<Class1> class1List = new List<Class1>();
class1List.Add(new Class1());
class1List.Add(new Class1(2, "SecondString", false));
Having an event like this:
class ABC
{
delegate bool X (int a);
event X eventX;
}
ABC.eventX+=someMethod; //works
I assume the delegate is then created implicitly by compiler?
Yes, prior to .NET 2 you had to manually specify it:
ABC.eventX+=new X(someMethod);
But it is now created implicitly with this syntax:
ABC.eventX+=someMethod;
Yes, it's automatically created.
For example:
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
(new Program()).Entrance();
}
public void Entrance()
{
ABC a = new ABC();
a.eventX += callback;
}
protected bool callback(int a)
{
return true;
}
}
class ABC
{
public delegate bool X(int a);
public event X eventX;
}
}
The Program class will be this if you see in reflector:
internal class Program
{
// Methods
protected bool callback(int a)
{
return true;
}
public void Entrance()
{
ABC a = new ABC();
a.eventX += new ABC.X(this.callback);
}
private static void Main(string[] args)
{
new Program().Entrance();
}
}