NewCSharpWork
NewCSharpWork

Reputation: 89

C# Condition with call of function

EDIT: I try to do a condition with call of function like this:

public Channels Channel1;
public Channels Channel2;
public void Scale(string value)
{
 if((Channel1.Scale(value)) has been called) //I don't know if a syntaxe is possible for this
 {
  // do something
 }
 if((Channel2.Scale(value)) has been called)
 {
  //do something else
 }
}

Code for call in my Form:

Info pub = new Info(); // Class where i have my function Scale
private button1_Click(object sender, EventArgs e)
{
 string value = comboBox1.Text;
 pub.Channel1.Scale(value);
}

Any idea for remplace 'has been called' by something working?

Thanks you for help !

Upvotes: 1

Views: 203

Answers (2)

Pac0
Pac0

Reputation: 23149

If you just need to know if the method has been called or not called:

As a very basic solution, you can have a property in your Channel class : public bool HasScaleBeenCalled { get ; private set; } = false.

You set this field to true in the Scale method.

Then in your if you can just check the value of this flag :

if (Channels1.HasScaleBeenCalled) {} 

If you need to check that a specific value parameter has been called:

Then you may need to do a bit more complicated: keep track of all the values used :

A field List<string> ValuesThatHaveBeenScaled { get; } = new List<string>();, and add a new value to this list each time Scale is called, and in the if you can check that Channels1.ValuesThatHaveBeenScaled.Contains(value)

Upvotes: 1

Sweeper
Sweeper

Reputation: 271195

In Channels, add a bool property like this:

public bool ScaleHasBeenCalled { get; private set; }

At the end of Channels.Scale(value), set ScaleHasBeenCalled to true:

ScaleHasBeenCalled = true;

Now in the Scale method shown in your question, you can check ScaleHasBeenCalled:

 if(Channel1.ScaleHasBeenCalled)
 {
  // do something
 }
 if(Channel2.ScaleHasBeenCalled)
 {
  //do something else
 }

Upvotes: 1

Related Questions