|
Ryano121 wrote: I just wondered how i would go about changing the ToString method to print out the polynomial in the form 2x^2 + 3x + 4 etc
By overriding it's original implementation and providing their own. If you take a look at the source[^] file called "Complex.cs", you'll find that behaviour implemented starting on line 433;
433 public override string ToString()
434 {
435 if (this == Complex.Zero) return "0";
MSDN has a How To[^]-article on the subject
I are Troll
|
|
|
|
|
Thanks for the reply!
Thanks for info on overriding! However it also seems that in Polynomial.cs, the override of ToString presents the object in the format 4 + 3x + 2x^2 instead of the conventional 2x^2 + 3x + 4.
I was just wondering if there was a way of modifying the existing ToString in the Polynomial.cs class to get the string in this format. I think it may be something to do with the for loop however the modifications i have made so far have only resulted in index out of bounds exceptions being made
Thanks again
|
|
|
|
|
Ryano121 wrote: it doesnt come out correctly.
That tells me you did something wrong.
Ryano121 wrote: Any suggestions would be greatly appreciated!
Fix your mistake. And if you can't, provide more information so people here can help you.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Sorry about that
Basically the Polynomial class has a Complex[] field that stores the coefficients of the equation. e.g 1,7,12 for x^2 + 7x + 12. The class has an overriden ToString method that gives a representation of what the equation is. However the given implementation given below provides 12 + 7x + x^2 instead of the conventional x^2 + 7x + 12 that i am looking for.
public override string ToString()
{
if (this.IsZero()) return "0";
else
{
string s = "";
for (int i = 0; i < Degree + 1; i++)
{
if (Coefficients[i] != Complex.Zero)
{
if (Coefficients[i] == Complex.I)
s += "i";
else if (Coefficients[i] != Complex.One)
{
if (Coefficients[i].IsReal() && Coefficients[i].Re > 0)
s += Coefficients[i].ToString();
else
s += "(" + Coefficients[i].ToString() + ")";
}
else if (i == 0)
s += 1;
if (i == 1)
s += "x";
else if (i > 1)
s += "x^" + i.ToString();
}
if (i < Degree && Coefficients[i + 1] != 0 && s.Length > 0)
s += " + ";
}
return s;
}
Or also here
http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=19446[^]
I have tried to modify the for loop to
for (int i = Degree -1; i >= 0; i--)
But that gives the + signs after the equation. I have also tried to modify other parts of the method yet it usually results in array index out of bounds exceptions.
Can anyone give me some pointers on how to modify the method to give the 'conventional' interpretation of equations?
Thanks again
|
|
|
|
|
|
OK, the problem is with the operator signs; it is caused by the non-ideal approach taken originally: there is an if statement trying to look at the next coefficient. It is bound to go wrong when traversing the array in the other direction. And it is plain wrong as is anyway, just imagine a few consecutive coefficients being zero.
The correct approach would implement this rule: each term needs a "+" sign if it hasn't a negative coefficient and isn't the first term in the output. Putting it like that, you don't need to look at other coefficients, you just keep one state variable telling you whether or not this is the first term to be seen.
In pseudo-code:
bool firstTerm=true;
foreach(term in polynomial) {
if (coef==0) skip;
if (!firstTerm && coef>0) output("+");
output(term);
firstTerm=false;
}
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
I am using c# window application. I am trying to pass the value of variable from one form to other. But it is giving error:Can any one correct this? thanks in advance.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace WindowsApplication1
{
public partial class Form1 : System.Windows.Forms.Form
{
public string frm1;
Form2 f = new Form2();
private string stringOnForm1;
public Form1(string sTEXT)
{
InitializeComponent();
textBox1.Text = sTEXT;
}
private void Form1_Load(object sender, EventArgs e)
{
f.Show();
f.button2.Visible = false;
Random rand = new Random();
Color[] c = { Color.Green, Color.Red };
this.button1.BackColor = c[rand.Next(0, c.Length)];
this.button2.BackColor = c[rand.Next(0, c.Length)];
if (this.button1.BackColor == Color.Red && this.button2.BackColor != Color.Red)
{
f.button2.Visible = true;
f.s = "1 Message received";
f.s1 = "ur gas is being leaked";
}
else if (this.button2.BackColor == Color.Red && this.button1.BackColor != Color.Red)
{
f.button2.Visible = true;
f.s = "1 Message received";
f.s1 = "ur electric is being leaked";
}
else if (this.button1.BackColor == Color.Red && this.button2.BackColor == Color.Red)
{
f.button2.Visible = true;
f.s = "2 Messages received";
f.s1 = "ur electric and gas is being leaked";
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
} }
Error:
No overload for method 'Form1' takes '0' arguments
|
|
|
|
|
Either try adding the default constructor back to your source code:
public Form1() {
InitializeComponent();
}
or modify the creation of the Form1 to have a string parameter for parameter sTEXT. For example:
... a = new Form1("SomeText");
|
|
|
|
|
What the message says is this: (for some reason, not sure which) your app needs a Form1 constructor without parameters, and you only have a single constructor, one with a string parameter.
I have three pieces of advice for you:
1.
learn to use all available information; when using a debug build, a compile-time error as well as a run-time exception provides you with file name or class name, and line number information. So you can easily figure out where it is unhappy. The problem may exist at that very location, or it gets detected at that location but is located elsewhere.
In this case I bet it is pointing to something like Application.Run(new Form1()); in perhaps a file called Program.cs
2.
learn to properly post a question. Your problem description (what it does, what you hope it would do, what your overall goal is) is not so good (you may have noticed most of your posts so far have been down-voted by one or more people). Your usage of PRE tags is fine, however we have seen 10 generations of your code already; and it badly needs a clean up, so please remove all empty handlers, all commented code, all redundant empty lines; then show only the relevant part of it. Which you probably haven't this time, if my hunch is right.
3.
Stop your hip-hop experiments for a while; choose, buy and study an introductory book on C# and learn how to program in a structured way. It will teach you many things including the use of meaningful names (not: Form1, Form2, f), object orientation, proper code design, etc.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
I realise this sounds a bit pathetic, but I have developed a workaround for a weakness in the pictureBox class, and I will publish it as an article on CodeProject. However I haven't yet upgraded from VC2005 (as a freelancer I am having trouble getting work in these straightened times and don't reall want to shell out for VS2010 just at the moment). Before I publish I would like to check whether the problem has been solved in later versions of .NET. I have a test program all ready. If someone with VS2010 could spare half an hour to run the test it would save me wasting my and everyone else's time on an unecessary article.
Dave
|
|
|
|
|
VS2010 comes in Express versions http://www.microsoft.com/express/Downloads/[^] - that way you can evaluate your software at no charge to you...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
Doh!. Thanks OriginalGriff. I didn't even realise they did a free Express version. That's done the trick. What an idiot!!
Dave
|
|
|
|
|
Welcome!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
You don't need Visual Studio to compile against .net anyway. I recommend learning to make do without it.
|
|
|
|
|
Interesting! What do you use then?
Dave
|
|
|
|
|
csc.exe (C Sharp Compiler) is included in each version of the .NET framework. Command line only, but it works.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
Dave Midgley wrote: What do you use then?
You need to understand the difference between Visual Studio, which is an IDE, and csc which is the compiler. You can use both independently, although you need the compiler to build projects from within VS.
I must get a clever new signature for 2011.
|
|
|
|
|
Right, I compile at the command line[^], like a real man.
Compiling WinForms applications is more complex, and I haven't written anything about it, but it's not a big deal.
Otherwise, I recommend using an Express edition.
|
|
|
|
|
Dave Midgley wrote: but I have developed a workaround for a weakness in the pictureBox class
The only weakness with the PictureBox class is actually using it. It's the most over-abused control in the entire ToolBox.
What weakness are you talking about?
|
|
|
|
|
I agree it's not very good. I was quite surprised to find that it does actually support animated GIFs, and then unsurprised to find that it doesn't support them properly by implementing a non-infinite number of loops.
Dave
|
|
|
|
|
Hi All,
I would like to assign a value to a variable in the Default.aspx.cs section of my code. This value will never change. To prevent having to capture this value throughout the code I want this variable to be accessable throughout the Web Application.
The code looks like this:
public partial Class _Default : System.Web.UI.Page
{
public static int timeout;
protected void Page_Load(object sender, EventArgs e)
{
timeout = toVal;
}
}
One of the classes I want to access the static "timeout" variable:
public partial class FormPage : System.Web.UI.Page
{
protected void Page_PreInit(object sender, EventArgs e)
{
}
}
I had another idea to make a seperate class file out of the code that gets the timeout value from the web.config file. Each time the timeout value is needed call the class and return the timeout. I just feel this would be a waste being that the timeout value will always be the same.
Any help would be really appreciated.
I'm also open to suggestions.
Thank you
modified on Saturday, February 12, 2011 10:51 PM
|
|
|
|
|
Put it in the web.config file or in a session variable.
Static variables are for the life of the class, not globally applied - they will be removed when the instance of the application terminates.
When does the app terminate? For ASP.NET, it terminates when the page load is finished.
I.e. The page request arrives at the server. The app is started, returns the page and is terminated. Any postback, or new page request starts a new instance of the app.
Putting it in a static variable won't work: the static variable would have to be set up anew every time the page is loaded!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
Thank you for your reply.
The timeout is stored in the web.config file, this is where the value is being retrieved from. I need to use this value in my code on multiple pages within the web application.
I am trying to add the value to a session variable within the default.aspx.cs file and then trying to retrieve it from the session variable in another but keep getting the error:
"Object reference not set to an instance of an object."
This indicates to me that the session variable is null, but how can this be when I previously populated it in the default page?
Here is some basic code I put together to make it simpler for testing:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session.Add("Test", "Testing session pass");
}
}
public partial class FormPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Session["Test"].ToString());
}
}
Do you know what the problem is?
Thank you
|
|
|
|
|
I just tried it locally to be on the safe side, and the only way I could duplicate your problem was by closing the browser and starting a new run, but going directly to a page rather than via the default page. Are you sure you loaded the default page first?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
Thank you. That was the problem, I wasn't running the code from the default.aspx file when testing it out of VS.
Thank you for taking the time to help me out with this. I appreciate it.
|
|
|
|