How to call a Class = new Class(); declare one time? - c#

In windows Form App I get call
private Class1 c1;
private void Print()
{
c1 = new Class1();
}
but in next one i don't want to declare new Class because it make a wrong value for my program and get error.
it's like
private void Print2()
{
c1 ; //**( I don't want to declare new one )**
}
edit 2
i'd like to do like you said but argument in my code is like
public CandleCollection GetCandleCollection()
{
CandleCollection collection = null;
try
{
collection = SymbolList[cbxSymbol.Text];
}
catch (Exception) { }
return collection;
}
private Class1 c1 = new Class1(<collection>); **it's need to call collection**
private void Print()
{
c1 = new Class1();
}
private void Print2()
{
c1 ; //**( I don't want to declare new one )**
}
edit 3
This is my original code to call c1
private void Print()
{
CandleCollection collection = GetCandleCollection();
Class1 c1 = new Class1(collection);
}

Move your CandleCollection variable out of the public method so you can also use it out of this method.
Then, you can instantiate the Class1 variable only once:
private CandleCollection collection = null;
public CandleCollection GetCandleCollection()
{
try
{
collection = SymbolList[cbxSymbol.Text];
}
return collection;
}
private Class1 c1 = new Class1(collection);
private void Print()
{
c1;
}
private void Print2()
{
c1;
}

Related

How to make an update interface which gets called in main update

Interface:
public interface IonStateLoadUpdate
{
void onStateLoadUpdate();
}
Class 1:
class foo
{
private void onLoadUpdate()
{
// How to call interface onStateLoadUpdate here ?
}
}
Class 2 which inherits IonStateLoadUpdate
class poo: IonStateLoadUpdate
{
public void onStateLoadUpdate()
{
// This should get call once foo calls onLoadUpdate
}
}
What about this:
class foo
{
private void onLoadUpdate(IonStateLoadUpdate x)
{
IonStateLoadUpdate.onStateLoadUpdate();
}
}
Then:
var p = new poo();
...
...
var f = new foo();
f.onLoadUpdate(p);

How to call a method from another class without creating an object

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() {
...
}
}

C# | Assigning new class to interface object

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

C# Derivied class doesn't pass values?

I have issue (I think) with derived class. When I add some points to my list in Form 1, I can see count and iterate through listA, and it seems its okay. I have below code:
/* Add points to lists */
class A {
private Point _singlePoint;
protected List<Point> _listA = new List<Point>();
protected List<Point> _listB = new List<Point>();
//properties
public List<Point> listA {
get { return _listA; }
set { _listA = value; }
}
public Point singlePoint {
get { return _singlePoint; }
set { _singlePoint = value; }
}
public virtual void addToListA(Point a) { }
}
class B : A {
public override void addToListA(Point a) {
_listA.Add(a);
}
}
public partial class Form1 : Form {
A _myPoints = new B();
private void drawPoint()
{
for (int i = 0; i < int.Parse(txtBoxPointsCount.Text); i++)
{
_myPoints.singlePoint = new Point(rnd.Next(100, 300), rnd.Next(100, 300));
_myPoints.addToListA(_myPoints.singlePoint);
}
foreach(Point p in _myPoints.listA)
{
graph.FillEllipse(new SolidBrush(cPoint), p.X, p.Y, 6, 6);
}
}
}
In other class I want to do some math on points, but I got ArgumentOutOfRangeException
class D {
A _myPoints = new B();
public void calulations(int iterations) {
randomPoint = rnd.Next(0, _myPoints.listA.Count);
System.Windows.Forms.MessageBox.Show(_myPoints.listA.Count.ToString());
try {
for (int i = 0; i < _iterations; i++) {
//here some math
}
}
catch(ArgumentOutOfRangeException e) {
// here I got errors about no items in my list
}
} }
In class D, in MsgBox I got count = 0. Maybe I wrote classes in wrong way? Method drawPoint() is called before calculations().
Could you help? Thanks in advance :)
That is because you are operating two completely different objects,
You have created an object here
public partial class Form1 : Form
{
A _myPoints = new B();
and then in class D created another
class D
{
A _myPoints = new B();
So you are adding point to your first object and trying to access in other object.

Two Interacting C# Classes

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));

Categories

Resources