|
I am attempting to design an interface for a set of calculation methods, inputs are:
MethodName
Collection of RateSet names
optional collection of tenors (key/value) per Rateset
Collection of Parameters (key/value)
Based on the MethodName the interface should then pass the collection values to the method
The method will crunch the values and output a collection of RateSets with Rates (key/value)
How would the implementation of MethodName to the actual method be done?
Reflections, case statement ???
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
If you are passing the MethodName as a string, then reflection is probably your best option for calling it.
However, having said that, if you are designing the interface, why not have it with a property that is a method delegate? That way, instead of passing the name of the method to your implementation, you can pass the method itself. No reflection needed, no limited switch-case constructs, and no nasty little bugs when the signature of MethodName does not quite match what was expected.
Cheers,
Mick
------------------------------------------------
It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
|
|
|
|
|
Midi_Mick wrote: why not have it with a property that is a method delegate In which case what would be the benefit of using an interface, it would be no different to using the method direct.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I was thinking you were looking for something like this:
public delegate ICollection<RateSet> Method(ICollection RateNames, IDictionary Parameters, IDictionary Tenors = null);
interface IRatesCalculator {
Method Method { get; }
ICollection RateNames { get; set; }
IDictionary Parameters { get; set; }
IDictionary Tenors { get; set; }
ICollection<RateSet> Results { get; }
}
public class RatesCalculator1 : IRatesCalculator {
public RatesCalculator1() {
Method = (ICollection RateNames, IDictionary Parameters, IDictionary Tenors) => {
List<RateSet> rateSet;
return rateSet;
}
};
public Method Method { get; }
public IDictionary Parameters { get; set; }
public ICollection RateNames { get; set; }
public IDictionary Tenors { get; set; } = null;
public ICollection<RateSet> Results {
get {
return Method?.Invoke(RateNames, Parameters, Tenors);
}
}
}
public class RatesCalculator2 {
public RatesCalculator2(Method calculator) {
Method = calculator;
}
public Method Method { get; }
public IDictionary Parameters { get; set; }
public ICollection RateNames { get; set; }
public IDictionary Tenors { get; set; } = null;
public ICollection<RateSet> Results {
get {
return Method?.Invoke(RateNames, Parameters, Tenors);
}
}
}
You may even need to make Method a Read/Write property if you wanted to set it after the creation of the class object.
Cheers,
Mick
------------------------------------------------
It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
|
|
|
|
|
This is a case where I would change my thinking around and use the Strategy patter[^] instead.
This space for rent
|
|
|
|
|
I think you are close to rediscovering abstract factory.
Calling a common interface which is implemented in number or classes and interface calling concrete methods according to some decision.
I suggest you read implementing factory/abstract factory before digging into reflection.
---
Pd.
|
|
|
|
|
So why aren't you just using a delegate here?
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
The methodname comes from a user selection and is a string!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
If it is from a user-selection, that you don't need to use a string. Most controls have a "display" value for the string, while your real value may be an enum. As Pete said, a strategy-pattern
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Yeah, seems like an obvious reason to go with a delegate method. Don't even need to create new classes to handle it, and the delegate effectively takes the place of an interface.
Psudocode!
delegate object MyDelegate(IEnumerable<kvp> parameters, IEnumerable<kvp> tenors = null);
Dictionary<string,MyDelegate> UIDelegateAssociation = new{{Key = "uiSelection", Value = (p,t){}}};
UIDelegateAssociation["uiSelection"].Value.Invoke(params,tenors);
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
Can we use Azure Table as database in CRM application?
|
|
|
|
|
Can we? Yes. Should we? Depends on lot of factors.
Azure table is basically NoSql key-value set up. Does you data fit into this model?
"It is easy to decipher extraterrestrial signals after deciphering Javascript and VB6 themselves.", ISanti[ ^]
|
|
|
|
|
Thank's for answer.
How to maintain ACID properties and referential integrity in Azure Tables.
|
|
|
|
|
There is no referential integrity. Table Storage is just a key/value pair storage, like a Dictionary collection in .NET.
|
|
|
|
|
I'm trying to get a window to move along the x axis however currently the code(timer4_tick) that i have is having no effect. Is there something that i am missing? I'll try to post only the code that is relevant.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
public partial class Form1 : Form
{
private int WindowVx, WindowVy;
private int WindowX, WindowY;
private int width = Screen.PrimaryScreen.WorkingArea.Width;
private int height = Screen.PrimaryScreen.WorkingArea.Height;
private void timer1_Tick(object sender, EventArgs e)
{
WindowX = random.Next(width);
WindowY = random.Next(height);
int wx = random.Next(width / 2);
int wy = random.Next(height / 2);
WindowVx = random.Next (1, 4);
WindowVy = random.Next (1, 4);
this.Left = WindowX;
this.Top = WindowY;
this.Width = wx;
this.Height = wx;
}
private void timer4_Tick (object sender, EventArgs e)
{
WindowX += WindowVx;
if (WindowX < 0) {
WindowVx = -WindowVx;
} else if (WindowX + width > ClientSize.Width) {
WindowVx = -WindowVx;
}
this.Refresh ();
}
this.timer4 = new System.Windows.Forms.Timer (this.components);
this.timer4.Enabled = true;
this.timer4.Interval = 1000;
this.timer4.Tick += new System.EventHandler (this.timer4_Tick);
private System.Windows.Forms.Timer timer4;
|
|
|
|
|
That's because it doesn't move anything. All it does is change the values of your internal variables, which are only subsequently used in your timer1_tick method, but immediately overwritten.
You need to affect the Top and Left properties of the window itself in order to move it.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Oh thank you! I got it working now!
|
|
|
|
|
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Ok, I've gotten it to move horizontally, vertically and diagonally fine so i've added boundaries so that it can't move past the screen limits. However when i added that it just shakes back and forth. From what i understand it first increases the x co-ordinates, moving the window to the right. Then the if statement checks if it has gone past the left of the screen and if it has it will reverse the direction the window moves. The second if statement checks that x co-ordinates including the width of the window doesn't past the width of the screen. Is this all correct?
private void timer4_Tick (object sender, EventArgs e)
{
this.Left += WindowVx;
if (this.Left < 0) {
WindowVx = -WindowVx;
}
if (this.Left + this.Width > ClientSize.Width) {
WindowVx = -WindowVx;
}
this.Refresh ();
}
|
|
|
|
|
So what you want it to do is move all the way right, then when it reaches the edge, turn round and move the other way?
To do that isn't difficult - when you reach the screen edge, change the sign on the value you are adding! Then, when you reach the other edge, change it back again.
Left += deltaX;
if (Left < 0 || (Left + Width) > ClientSize.Width)
{
deltaX = -deltaX;
} You don't need to specify this all the time - you only need it when you have a local variable of the same name as your class level one and you want to access the class level variable.
You also don't need to refresh anything - when you update Left and Top they will refresh for you.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hm... for some reason the window just moves back and forth kind of like it's vibrating. I'm not really sure what's causing it as the code without the if statement works fine.
This is the code right now:
this.Left += WindowVx;
if (Left < 0 || (Left + this.Width) > ClientSize.Width){
WindowVx = -WindowVx;
}
|
|
|
|
|
Check your "ClientSize" object and see what values it has - at a guess the Width is zero.
You know how to use the debugger, yes?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I checked and the width is 282 i think. And by debugger you mean like the play button on xamarin studio? That's what i've been using to test my program.
Also just in case i'll post the rest of my form information.
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(282, 253);
this.ControlBox = false;
this.Controls.Add(this.webBrowser1);
this.KeyPreview = true;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.Text = "test";
this.TopMost = true;
this.ResumeLayout(false);
|
|
|
|
|
OK: so you can't affect the Left and Top of your current window - because the ClientSize of the current window is going to be at best the same as the width of the window!
So, if you are trying to move something within the current window, use its properties: Left, Width, and Top, and the Window Client Area.
Your original code shows you being inside the form, so this refers to the whole form, not anything within it. If you are trying to move the form, then you need to get the information on sizing from whatever it is displayed on - probably the desktop (or android equivalent, if that's what you are using).
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
When you say move the form do you mean the things inside the window? I'm trying to move the entire window and by information on sizing do you mean my screen resolution? If i use Left, Width, and Top, and the Window Client Area to move the thing inside the window, do i need something else to move the window itself?
Edit: Oh i just looked it up and form is the window itself.
|
|
|
|