473,836 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Flawed logic

I've been working on a project for the last week and I can't seem to get it working. It's supposed to be finished within the next few days so a speedy reply would be appriciated. Anyways, it seems that my program goes into some sort of endless loop that I can't find, as when I click a link to get to the page I'm working on, the page never loads, and the aspnet_wp process consumes 98% of cpu time (the other 2% is used by task manager). I'm using visual studio 2005 express beta 1 with ASP.NET 2.0.40607.0.
Here is the code:

public partial class Testaspx
{
private StreamReader questions;
private XmlTextReader questionReader;
string number;
private string prefix;

void Page_Load(objec t sender, EventArgs e)
{

this.btnNext.Cl ick += new EventHandler(th is.getAnswer);
if (!openFile()) {
lbldisplay.Text = "Error opening";
return;
}
if (!prepare())
{
lbldisplay.Text += "Error preparing";
}
getQuestion();
}

private bool prepare()
{
//do
//{
while (!questionReade r.LocalName.Equ als("benchmark" ) && !questionReader .EOF)
{
questionReader. Read();
}

if (questionReader .EOF)
{
return false;
//Server.Transfer ("grade.aspx ");
}
//} while (!questionReade r.IsStartElemen t("benchmark")) ;

if (questionReader .ReadToDescenda nt("preface"))
{
prefix = questionReader. ReadString();
}
else
{
lbldisplay.Text = "Error getting preface";
return false;
}

return true;
}

public void getQuestion()
{
string passage = "";
do
{
//do
//{
while (!questionReade r.LocalName.Equ als("question") && !questionReader .LocalName.Equa ls("benchmark") )
{
questionReader. Read();
}

if (questionReader .LocalName.Equa ls("benchmark") )
{
if (!prepare())
{
lbldisplay.Text += "Error preparing in getQuestion";
return;
}
continue;
}
// } while (!questionReade r.IsStartElemen t("question") );

if (questionReader .ReadToDescenda nt("number"))
{
number = questionReader. ReadString();
}
else
{
lbldisplay.Text = "error getting passage number";
}

} while (number != Session["next"]);

if (questionReader .ReadToNextSibl ing("passage"))
{
passage = questionReader. ReadString();
}
else
{
lbldisplay.Text = "Error getting passage";
}

while (questionReader .ReadToNextSibl ing("option"))
{
options.Items.A dd(questionRead er.ReadString() );
}
lblNumber.Text = number;
lbldisplay.Text = prefix + passage;
return;

}

/// <summary>
/// Finds and stores which answer was selected
/// </summary>
private void getAnswer(objec t sender, EventArgs e)
{
string selected = "";
selected = options.Selecte dItem.Text.Subs tring(0, 1);
if (selected != "")
{
string number = (string)(Sessio n["next"]);
Session.Add(num ber, selected);
Session["next"] = getNextNumber() ;
Server.Transfer ("Test.aspx" );
}
//return true;
}

/// <summary>
/// Gets the number of the next passage
/// </summary>
/// <returns>Numb er of next passage as string</returns>
private string getNextNumber()
{
while (!questionReade r.LocalName.Equ als("number") && !questionReader .EOF)
{
questionReader. Read();
}

if (questionReader .EOF)
{
//replace with redirect to grade page
Server.Transfer ("grade.aspx ");
//return "none";
}

string next = questionReader. ReadString();
return next;

}

/// <summary>
/// Opens the next questions file
/// </summary>
private bool openFile()
{
questions = new StreamReader(Se ssion["location"] + "\\tests\\" + (string)(Sessio n["file"]) + ".xml");
questionReader = new XmlTextReader(q uestions);
if (questions == null || questionReader == null)
{
return false;
}
return true;

}
}
Nov 18 '05 #1
4 1121
bstoddart,

I believe the problem is where you're testing if you should continue the
read.

The first thing I would do is change your while loops:

//set default return value here

while (questionReader .Read)
{
//check each row here and set return value(s)
}

//return value here

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"bstoddart" <bs*******@disc ussions.microso ft.com> wrote in message
news:40******** *************** ***********@mic rosoft.com...
I've been working on a project for the last week and I can't seem to get it working. It's supposed to be finished within the next few days so a
speedy reply would be appriciated. Anyways, it seems that my program goes
into some sort of endless loop that I can't find, as when I click a link to
get to the page I'm working on, the page never loads, and the aspnet_wp
process consumes 98% of cpu time (the other 2% is used by task manager). I'm
using visual studio 2005 express beta 1 with ASP.NET 2.0.40607.0. Here is the code:

public partial class Testaspx
{
private StreamReader questions;
private XmlTextReader questionReader;
string number;
private string prefix;

void Page_Load(objec t sender, EventArgs e)
{

this.btnNext.Cl ick += new EventHandler(th is.getAnswer);
if (!openFile()) {
lbldisplay.Text = "Error opening";
return;
}
if (!prepare())
{
lbldisplay.Text += "Error preparing";
}
getQuestion();
}

private bool prepare()
{
//do
//{
while (!questionReade r.LocalName.Equ als("benchmark" ) && !questionReader .EOF) {
questionReader. Read();
}

if (questionReader .EOF)
{
return false;
//Server.Transfer ("grade.aspx ");
}
//} while (!questionReade r.IsStartElemen t("benchmark")) ;

if (questionReader .ReadToDescenda nt("preface"))
{
prefix = questionReader. ReadString();
}
else
{
lbldisplay.Text = "Error getting preface";
return false;
}

return true;
}

public void getQuestion()
{
string passage = "";
do
{
//do
//{
while (!questionReade r.LocalName.Equ als("question") && !questionReader .LocalName.Equa ls("benchmark") ) {
questionReader. Read();
}

if (questionReader .LocalName.Equa ls("benchmark") )
{
if (!prepare())
{
lbldisplay.Text += "Error preparing in getQuestion";
return;
}
continue;
}
// } while (!questionReade r.IsStartElemen t("question") );

if (questionReader .ReadToDescenda nt("number"))
{
number = questionReader. ReadString();
}
else
{
lbldisplay.Text = "error getting passage number";
}

} while (number != Session["next"]);

if (questionReader .ReadToNextSibl ing("passage"))
{
passage = questionReader. ReadString();
}
else
{
lbldisplay.Text = "Error getting passage";
}

while (questionReader .ReadToNextSibl ing("option"))
{
options.Items.A dd(questionRead er.ReadString() );
}
lblNumber.Text = number;
lbldisplay.Text = prefix + passage;
return;

}

/// <summary>
/// Finds and stores which answer was selected
/// </summary>
private void getAnswer(objec t sender, EventArgs e)
{
string selected = "";
selected = options.Selecte dItem.Text.Subs tring(0, 1);
if (selected != "")
{
string number = (string)(Sessio n["next"]);
Session.Add(num ber, selected);
Session["next"] = getNextNumber() ;
Server.Transfer ("Test.aspx" );
}
//return true;
}

/// <summary>
/// Gets the number of the next passage
/// </summary>
/// <returns>Numb er of next passage as string</returns>
private string getNextNumber()
{
while (!questionReade r.LocalName.Equ als("number") && !questionReader .EOF) {
questionReader. Read();
}

if (questionReader .EOF)
{
//replace with redirect to grade page
Server.Transfer ("grade.aspx ");
//return "none";
}

string next = questionReader. ReadString();
return next;

}

/// <summary>
/// Opens the next questions file
/// </summary>
private bool openFile()
{
questions = new StreamReader(Se ssion["location"] + "\\tests\\" + (string)(Sessio n["file"]) + ".xml"); questionReader = new XmlTextReader(q uestions);
if (questions == null || questionReader == null)
{
return false;
}
return true;

}
}

Nov 18 '05 #2
Your suggestion will probably make the code easier to read and debug, but it turns out that I was having a problem with casting. In the code, I had while statement like this:
} while (number != Session["next"]);
where number is a string, by doing an explicit cast to string it works:
} while (number != (string)(Sessio n["next"]));

Thank you for your sugesstion.

Nov 18 '05 #3
bstoddart,

Interesting! I didn't think a cast (or lack thereof) could cause an infinite
loop.

In that case I would highly suggest setting your code to Option Strict and
Option Explicit.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"bstoddart" <bs*******@disc ussions.microso ft.com> wrote in message
news:45******** *************** ***********@mic rosoft.com...
Your suggestion will probably make the code easier to read and debug, but it turns out that I was having a problem with casting. In the code, I had
while statement like this: } while (number != Session["next"]);
where number is a string, by doing an explicit cast to string it works:
} while (number != (string)(Sessio n["next"]));

Thank you for your sugesstion.

Nov 18 '05 #4
Jeremy,

That's too bad that c# doesn't have option strict / explicit. They can't be
"Always On" in the same way that setting those in VB.NET work, because if it
worked the same way bstoddart never could have made the comparison without
explicitly casting the session variable in the first place.

Option Explicit forces the cast to be made or the code won't run. Since
number was declared as a string in VB.NET Option Explicit would have not let
the code run unless the session object were also cast as a string...

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Jeremy Davis" <Je*********@di scussions.micro soft.com> wrote in message
news:94******** *************** ***********@mic rosoft.com...
The problem was that since Session["next"] was coming out as object, the != was doing a reference comparison rather than a value comparison--and was
always evaluating false, since number and Session["next"] are two different
objects in memory. The explicit cast caused a string comparison, as was
desired. Oh, and C# doesn't have the concept of "Option Strict" or "Option Explicit". They're effectively always on.
"S. Justin Gengo" wrote:
bstoddart,

Interesting! I didn't think a cast (or lack thereof) could cause an infinite loop.

In that case I would highly suggest setting your code to Option Strict and Option Explicit.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"bstoddart" <bs*******@disc ussions.microso ft.com> wrote in message
news:45******** *************** ***********@mic rosoft.com...
Your suggestion will probably make the code easier to read and debug, but
it turns out that I was having a problem with casting. In the code, I had while statement like this:
} while (number != Session["next"]);
where number is a string, by doing an explicit cast to string it

works: } while (number != (string)(Sessio n["next"]));

Thank you for your sugesstion.


Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1972
by: Roshan | last post by:
This is regarding the article titled "C++ & Double-Checked Locking" by Scott Meyers and Andrei in DDJ July 2004 issue. I think the reasoning in this article is fundamentally flawed due the authors mixing up a couple of concepts (observable behavior & side effects) that are defined separately in the standard. Background: In the following statement... class Singelton {
9
1296
by: clintonG | last post by:
Simply a postulation but consider the following... // breaks when declared in content page that uses a MasterPage <img src="App_Themes/SmokeAndGlass/Images/smoke_METRO184x26.gif" /> // must run as a web server control <img src="App_Themes/SmokeAndGlass/Images/smoke_METRO184x26.gif" runat="server" /> However... When we want to change themes dynamically how do we modify the
0
1214
by: Al Fatykhov | last post by:
Using MABLE logic engine with existing .NET applications. MABLE web services provide an interface to MABLE business objects and logic. Let us review some technical details of the MABLE web services. · MABLE utilizes SOAP 1.2 protocol. · MABLE uses AXIS 1.4 as a web service transport. · MABLE support state-full conversations by implementing a conversation session.
0
1916
by: Wim Vanhoof | last post by:
----------------------------------------------------------- WLPE' 06 - CALL FOR PAPERS Workshop on Logic-based Methods in Programming Environments (satellite workshop of ICLP’06) August 16, 2006
0
1595
by: fiona | last post by:
FOR IMMEDIATE RELEASE Catalyst release low cost logic processing tool 87% of defects in software are errors in logic Yucca Valley, CA, September 2006 - Catalyst Development Corporation, publisher of SocketWrench and SocketTools, today announced the release of its first application software, a logic processing software tool. LogicGem is designed to provide a familiar, easy-to-use way to create,
15
2587
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to determine who needs to receive the text message then send the message to the address. Only problem is, the employee may receive up to 4 of the same messages because each thread gets the recors then sends the message. I need somehow to prevent...
14
3576
by: rabbitrun | last post by:
Hi Everyone, I work for a financial company. I am planning to give a presentation to rest of the development team (15 people) here on moving server side logic to client-side javascript for an internal intranet application rewrite. This approach will definitely stir up hot debate from hardcore server-side Java folks who wants to do UI stuff even on the server!. Since I am pretty much known as the JS or UI Guy of the group, my Boss...
9
2746
by: SAL | last post by:
Hello, I have a Dataset that I have table adapters in I designed using the designer (DataLayer). I have a business logic layer that immulates the DataLayer which may/may not have additional logic in. My business classes are, of course, decorated with the: <System.ComponentModel.DataObject() attribute. So, I drop a GridView on a webform and set its datasource to an ObjectDatasource which in turn is using one of my business logic...
15
2444
by: bruno.desthuilliers | last post by:
On 27 juin, 18:09, "John Salerno" <johnj...@NOSPAMgmail.comwrote: For which definitions of "content" and "logic" ??? The point of mvc is to keep domain logic separated from presentation logic, not to remove logic from presentation (which just couldn't work). Templating systems are for presentation logic. Whether they work by embedding an existing complete programmation language or by providing they're own specialised mini-language (or...
0
10539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10584
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5645
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5815
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4446
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3108
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.