Note that there are some explanatory texts on larger screens.

plurals
  1. POtype casting error in C#
    primarykey
    data
    text
    <p>I am attempting to have a write a class that simulates different frequency signals. I would like for the class to take the type of object that I am testing as a parameter, in order to test methods within that calling class. I am getting an error when I try to type cast myObject as myType. Obviously I am doing this incorrectly. I have omitted much of the superfluous code in this class, as to focus on the context of my question.</p> <pre><code> public MaxFinderTestChannel(TestSignals signal, Object ob) { myType = ob.GetType(); myObject = ob as myType; switch (signal) { case TestSignals.Linear: { constant = 1; for (double dd = 0.0; dd &lt;= 5 * Math.PI; dd += .001) { x = dd; values.Add(x); y = x + constant; values.Add(y); } } break; case TestSignals.NegativeSine: { constant = -1; for (double dd = 0.0; dd &lt;= 5 * Math.PI; dd += .001) { x = dd; values.Add(x); y = Math.Sin(25 * x) + constant; values.Add(y); } } break; case TestSignals.Parabola: { constant = 2; for (double dd = (-Math.PI); dd &lt;= Math.PI; dd += .1) { x = dd; values.Add(x); y = Math.Pow(x, 2) + constant; values.Add(y); } } break; case TestSignals.Sine: { constant = 1; for (double dd = 0.0; dd &lt;= 5 * Math.PI; dd += .001) { x = dd; values.Add(x); y = Math.Sin(25 * x) + constant; values.Add(y); } } break; default: throw new Exception("Invalid signal type:" + this.Name.ToString()); } pulseTrain = values.ToArray(); } public virtual bool EnumDataPoints(bool eventsOnly, double startTime, double duration, Collection&lt;ISampleProcessor&gt; sampleProcessors) { for (double time = timestamp; !breakloop &amp;&amp; time &lt; endtime; time += .001) { time = Math.Round(time, 4); if (time == timestamp) { if (myObject.Compute(time, ref value) == false) { breakloop = true; } timestamp = Math.Round(pulseTrain[index], 4); if (index &lt; pulseTrain.Length - 1) { index++; } value = Math.Round(pulseTrain[index], 4); if (index &lt; pulseTrain.Length - 1) { index++; } } } return !breakloop; } </code></pre> <p>The problem is that I get a compile error when I attempt to loop through my timestamps. The Compute() method is enforced by a specific interface, and several different concrete classes use this method.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload