|
You didn't provide all of the code, so we can't play with it without compiler errors, but...
0) Generally speaking, if it works (well), comment the hell out of it, move on with your project, and don't look back.
1) You could create an (abstract?) base class with a GetGCD method that accepts/returns a generic object, and while that would probably make the base class fairly complex, at least that complexity would be concealed in derived classes.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Are you thinking of a setup something like this (I would probably create a ModdedInteger class and use that instead of Integers plus a Characteristic field if I could make this approach work)?
abstract class Polynomial<T> where T : IArithmetic<T>...
class IntegerPolynomial : Polynomial<Integer>, IArithmetic<IntegerPolynomial>...
class ModdedPolynomial : Polynomial<ModdedInteger>, IArithmetic<ModdedPolynomial>...
class RationalPolynomial : Polynomial<Rational>, IArithmetic<RationalPolynomial>...
class FieldElement : Polynomial<RationalPolynomial>, IArithmetic<FieldElement>...
The problem I've had with that is, suppose I'm trying to genericize the Integer.GetGCD/FieldElement.GetGCD algorithm to act on Polynomial objects. The only thing I can think to do with the signature is this:
Polynomial<T> GetGCD<T>(Polynomial<T> a, Polynomial<T> b) where T : IArithmetic<T>
But then if I pass IntegerPolynomial objects, I get back a Polynomial<integer>, when what I really want is another IntegerPolynomial. I have the same problem with every other method that returns an object whose type is the same as the class it belongs to. I would have to extract the coefficients from the Polynomial object and use them to construct an object of the derived type I actually wanted almost every time I used any of them. This is why I mentioned considering the possibility of shoving everything into a single generic class - in that case I would be passing Polynomial<integer> objects, and the Polynomial<integer> object I'd get back would be exactly what I'd want.
In case you're inclined to play with it, here's the whole program.
|
|
|
|
|
Reality - If your code is complex, nothing you do is going to actually reduce the complexity of the code (short of using someone else's library of code) . It's just going to move that complexity to one or more new locations.
Opinion - Given the complexity of your code, your actual aim should be to make the code as efficient as possible, and that means reducing/eliminating casting/boxing to the extent that it is possible, and avoiding the use of linq and reflection. Put your code into an assembly and let 'er rip. Over-thinking it is not worth delaying further development of your project.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I don't get all the nuances, but maybe you can consider a "component-based" approach; where the needed "behaviours" are "attached"; versus inherited and / or defined explicitly.
Unity (and "VB") is based on that; with "scripting" (pretty well) limited to filling in handlers.
Sort of like: it has "legs" "attached" or it doesn't; it has a "voice" or it doesn't; etc.
All Unity "components" are just "children" of a "higher" component.
The run time "infers" what every component does or is doing for each "frame".
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Is it possible to be more concrete about what this would involve? Would it involve using different C# language features, or is it more of a general design paradigm?
|
|
|
|
|
It's more of a general design pattern.
In terms of C#, "everything" derives from Object.
And if at its core, there is a class defined as:
Class Component : object {
List<component> Components;
}
then you can model any object and behaviour by "attaching" "child" components to "parent" components.
In your case, you would probably use a "factory" to create "math" components and wire them up into "higher-class" math / poly objects.
If you can create a "hierarchy" / mind map of your poly model; then it may make more sense.
I don't need to say: bicycle; unicycle; or trike ... I just count the wheels; if there are any.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Hello, I try to use an Javascript to autofill a textbox.
HtmlElement headElement = webBrowser3.Document.GetElementsByTagName("head")[0];
HtmlElement scriptElement = webBrowser3.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptElement.DomElement;
element.text = "function fillbox() { document.getElementById('ember891').value = 'test@test.com' }";
headElement.AppendChild(scriptElement);
webBrowser3.Document.InvokeScript("fillbox");
I try it here: account.sonyentertainmentnetwork.com/home/index.action , when you click the bottom button the register, then start and there the first textbox for the email.
When I open google chrome on this site and enter :
javascript:document.getElementById('ember891').value = 'test@test.de' in the console, everything is just fine, but it won't work with the c# application. So I want to get it working, that I am able to fill the textbox with a button click per Javascript.
modified 17-Apr-18 10:39am.
|
|
|
|
|
The page shows up as blank in my browser.
Most companies had to add code to prevent people from automating the sign-up proces; it is the reason captcha's exist.
If what you are doing is a valid use-case, then the people from Sony will probably help out.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I updated the link, account.sonyentertainmentnetwork.com/home/index.action you have to click on the bottom button to register a new account, then click on Start and there is the site that I speak from.
|
|
|
|
|
That's something they don't want to automate or allow; good luck with the "I'm not a robot" test
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
The I'm not a Robot test pops now out, so the user can verify it and the program does the rest. Everything works now, used another site to create psn accounts. Problem solved by myself.
|
|
|
|
|
Have to agree; the whole thing stinks.
I heard 40 - 60% of all accounts are faked; you are part of the problem.
You're probabably not even from "Switzerland" (test.de).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Lol I do not have to prove anything to you. I am Swiss believe it or not
|
|
|
|
|
But you don't deny faking accounts?
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
If you mean the Account on codeproject, no I do not fake any accounts here, this is my only one. If you mean Sony then I won't say anything without my lawyer
|
|
|
|
|
Well, we know what you meant.
And I'll let Sony know you're interested ... considering their past experiences with your type.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I have uploaded a C # ActiveX control inside an MFC ActiveX control.
The WebBrowser inside the C # ActiveX control does not communicate with the script.
Is there a solution?
|
|
|
|
|
Please do not post the same question in multiple forums.
|
|
|
|
|
OK. The .NET Framework side post has been deleted.
|
|
|
|
|
Yes. Since the user can "disable AciveX content", use something else.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I'm building a fraction application and I have recently fixed an issue with a divide by zero exception. However after fixing this problem, a whole new one arose, the problem I am having now is that the textbox in which the answer displays always states the denominator as 0 when it is supposed to show the reduced form.
The code for both is below and I appreciate any help or feedback
<pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Fractionapp
{
class Fraction
{
private int numerator;
private int denominator;
public Fraction()
{
numerator = 0;
denominator = 0;
}
public Fraction(int num, int den)
{
numerator = num;
Denominator = den;
Reduce();
}
public int Numerator
{
get
{ return numerator; }
set
{ numerator = value; }
}
public int Denominator
{
get
{
return Denominator;
}
set
{
if (value == 0)
{
throw new ArgumentOutOfRangeException("Denominator", value, "Denominator cannot be zero");
}
}
}
private void Reduce()
{
int GCD = 0;
for (int x = 1; x <= denominator; x++)
{
if ((numerator % x == 0) && (denominator % x == 0))
GCD = x;
}
if (GCD != 0)
{
numerator = numerator / GCD;
denominator = denominator / GCD;
}
}
public override string ToString()
{
return numerator + "/" + denominator;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Fractionapp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculateFraction_Click(object sender, EventArgs e)
{
{
Fraction fr2 = new Fractionapp.Fraction(Convert.ToInt32(txtNumerator.Text), Convert.ToInt32(txtDenominator.Text));
txtReduced.Text = "The lowest form of the inputted fraction is: " + fr2.ToString();
}
}
}
}
|
|
|
|
|
Hi,
you tricked yourself in this one. First some comments, as requested:
1.
property Denominator throws when the value is zero, yet without parameters you create a Fraction with denominator zero??? This should be 1 IMO.
2.
Inside Reduce() you apply a brute force approach by trying each and every possible common divisor. Rather than trying them all in ascending order, you should use descending order and stop as soon as you find one.
3.
Again, inside Reduce(), you also try divisor 1, which always will be a common divisor. Such test is unnecessary, and so is the if (GCD != 0) test, as GCD will be 1 or larger.
4.
In the setter half of Denominator you fail to set anything.
5.
Now check the casing inside your Fraction(int num, int den) constructor!
Please check why several of the above issues are relevant to your program's current symptom.
6.
When you fixed all of the above, then consider entering some negative numbers, and check whether your program fails for negative denominators; fix if necessary.
7.
Rather than using Convert.ToInt32() you should use int.TryParse() which takes a bit more code but offers an indication the text actually is/isn't representing a valid number, so you can act accordingly.
8.
Needless to say, there are much better algorithms to determine a GCD, e.g. Euclid's.
modified 13-Apr-18 22:11pm.
|
|
|
|
|
Thanks for the reply! I guess I'm just not understanding what you're explaining for comments 2, 3, 4. Comment 1 I was messing around trying to fix things (so the exception would fire and forgot to set it back to 1, that's been fixed). If I change the D to a d in the 2 parameter constructor it works perfectly just doesn't fire the exception. Yet changing the d to a D causes any number entered in the denominator box to show as a zero in the answer box. I'm trying to understand why this happens and the code to fix it (has to be simple), I'm a complete coding novice so I don't really understand this without visuals.
|
|
|
|
|
a line
denominator=value;
is missing!
|
|
|
|
|
Hah! I guess that's what I get for copying and pasting, I've got this in like 3 different files (don't ask), one of the programs must've had that line missing and I copied from it, thank you though!
|
|
|
|