Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 10-11-2007, 12:19 AM   #1 (permalink)
Forum Expert
 
Join Date: May 2003
Posts: 469
Send a message via AIM to Rooster2
Default Creating a welcome tips app...

Ok, first, I'm new to C#. I bought a book (C# Build a program NOW!), flew threw it. Basically, I want to code a simple program that would act similar to the XP Welcome tips, you know the dialog box that would pop up and offer handy (hehe) tips to using XP.

So far, I've got an internal setting called tipnumber. I have it incrimenting, and saving to the settings this number each time my button is clicked. I have a textbox which displays (right now for testing) "this is tip number" and the proper number.

What I lack is a method to actually get a tip there. I originally thought I'd just do a ton of IF statements, you know one for each tip, and an else at the end to reset the number back to 0.. Then I decided to think out of the box and realized it would be pretty cool if I had another file within my app which was basically a txt/xml/other?? file which contained ALL my tips (making them easier to manage/add/alter in the future when used with other programs). Problem is, I have no idea HOW to go about this.. If anyone is bored and could point me in the right direction, I'd really appreciate it. All the stuff I've done I'm sure is elementary to you all, but I assure you a fair amount of sweat has gone into this, lol.. The book, while 'fun', doesn't really teach you a whole lot.


Rooster
__________________
Quote:
Originally Posted by Ravatar
:!: *Best Thread of the Day* :!:



The Good: It's a really good UOA clone.
The Bad: It's not a UO emulator.
The Ugly: You are going to get flamed badly for this :)
Rooster has spoken.
Rooster2 is offline   Reply With Quote
Old 10-11-2007, 04:36 AM   #2 (permalink)
RunUO Forum Moderator
 
daat99's Avatar
 
Join Date: Dec 2004
Location: Israel
Age: 27
Posts: 8,163
Send a message via ICQ to daat99 Send a message via AIM to daat99
Default

I would go with the XML solution for this program.

You'll need to do the following things:

1. Make an XML file in the following format:
Code:
<root>
<tip>this is tip 0</tip> <tip>this is tip 1</tip> <tip>this is tip 2</tip> ... <tip>this is tip N-1</tip>
</root>
2. Read the XML file when you program starts:
Code:
XmlDocument tipsXML = new XmlDocument();
tipsXML.load("tips.xml"); //change "tips.xml" to the file name and location of your xml tips file you created in step 1.
3. Get the number of tips in the file:
Code:
int numberOfTips = tipsXML.FirstChild.ChildNodes.Count; //go to "root" and get all the "tip" childs count
4. Create a method that gets a tip from a number:
Code:
private string getTipFromNumber( int number )
{
return tipsXML.FirstChild.ChildNodes[number].InnerText; //first go to the root, get the tip based on number between 0 and N-1, get the inner text (the tip itself).
}

P.S.
I haven't done any try/catch in the code so you should add them.
You will need to have access to the tipsXML variable from all the class so it should be a property and initiated in the constructor.
__________________
I always try to help
Sometimes, I don't know how....

My Web Page
Forum Rules
-------------------------------------------------------------
Extensive OWLTR System | Token System | World Teleporters
-------------------------------------------------------------
daat99 is offline   Reply With Quote
Old 10-11-2007, 07:08 AM   #3 (permalink)
Forum Expert
 
RavonTUS's Avatar
 
Join Date: Aug 2004
Location: in a house.
Age: 39
Posts: 582
Send a message via ICQ to RavonTUS Send a message via AIM to RavonTUS
Default

Greetings,

HINT, HINT... [RunUO 2.0 SVN178] Tip of the Day (a variation of MOTD)

-Ravon
__________________

Will RunUO work on Linux? Yes
RavonTUS is offline   Reply With Quote
Old 10-11-2007, 11:40 PM   #4 (permalink)
Forum Expert
 
Join Date: May 2003
Posts: 469
Send a message via AIM to Rooster2
Default

Tnx fellas! Never thought of using runuo stuff as a resource, lol! That's a pretty good idea.. I always refered to the code as merely scripts and mentally, even though I know better, well, you know... lol.. Tnx for the advice!
__________________
Quote:
Originally Posted by Ravatar
:!: *Best Thread of the Day* :!:



The Good: It's a really good UOA clone.
The Bad: It's not a UO emulator.
The Ugly: You are going to get flamed badly for this :)
Rooster has spoken.
Rooster2 is offline   Reply With Quote
Old 10-12-2007, 07:24 PM   #5 (permalink)
Forum Expert
 
Join Date: May 2003
Posts: 469
Send a message via AIM to Rooster2
Default

Well, I'm chokin here, rofl.. Here's what I have thus far:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using FortuneCookies.Properties;
using System.Configuration;

namespace FortuneCookies
{
    public partial class Main : Form
    {
        //Loads the About screen
        AboutFortuneCookies aboutScreen = new AboutFortuneCookies();
        //This should load the tips(aka fortunes)
        XmlDocument tipsXML = new XmlDocument();

        //sets currentFortune to the settings number.  This is no longer used and will be removed.
        int currentFortune = FortuneCookies.Properties.Settings.Default.FortuneNumber;

        public Main()
        {
            InitializeComponent();
            tipsXML.Load("tipsXML.xml");
            //go to "root" and get all the "tip" childs count
            int numberOfTips = tipsXML.FirstChild.ChildNodes.Count;
            //simple text instruction...
            this.tbFortune.Text = "Select New Fortune to see your fortune";
        }
        //private string getTipFromNumber(int number)
        private string getTipFromNumber (int number)
        {
            //first go to the root, get the tip based on number between 0 and N-1, get the inner text (the tip itself).
            return tipsXML.FirstChild.ChildNodes[number].InnerText;



        }

        private void tsbNew_Click(object sender, EventArgs e)
        {
            //My trial run to ensure the Settings were working, updating and saving.  Enabled me to verify a
            //new number was stored and used per each click of the button.  Will be removed, REM'd for now:
            //-- tbFortune.Text = "You have clicked NEW. Next number is: " + FortuneCookies.Properties.Settings.Default.FortuneNumber + ".";
            // failed attempt  tbFortune.Text = FortuneCookies.Properties.Settings.Default.FortuneNumber + tipsXML.FirstChild.ChildNodes[currentFortune].InnerText;
            tbFortune.Text = tipsXML.FirstChild.ChildNodes[currentFortune].InnerText;
            //This will take the fortune # and add 1 to it in the settings.
            FortuneCookies.Properties.Settings.Default.FortuneNumber++;
            //This will save the new fortune number to the settings.
            FortuneCookies.Properties.Settings.Default.Save( );
        }



        private void button1_Click(object sender, EventArgs e)
        {
            //This area is identical to the tsbNew_Click section directly above this section.  This will eventually be removed and was
            //here only for testing reasons.
            tbFortune.Text = "You have clicked NEW. Next number is: " + FortuneCookies.Properties.Settings.Default.FortuneNumber + ".";
            FortuneCookies.Properties.Settings.Default.FortuneNumber++;
            FortuneCookies.Properties.Settings.Default.Save( );
        }

        private void helpToolStripButton_Click(object sender, EventArgs e)
        {
            aboutScreen.ShowDialog();
        }
    }
}
I'm getting a "NullReferenceException on the
Code:
 tbFortune.Text = tipsXML.FirstChild.ChildNodes[currentFortune].InnerText;
line.. I've tried various ways (that I know), but can't seem to find the right words.

My setting (fortuneNumber) is set to one. The file name (tipsXML.xml) is correct (left it as that from the example above till I work this out).

I'm also aware I'm missing some form of statement to say "if fortuneNumber is greater than numberOfTips than reset it to 1 again".. Really at this point that isn't my concern (since I cant get it to display tip #, hehe), but if ya wanna help with that too... No one would be mad at you

Rooster
__________________
Quote:
Originally Posted by Ravatar
:!: *Best Thread of the Day* :!:



The Good: It's a really good UOA clone.
The Bad: It's not a UO emulator.
The Ugly: You are going to get flamed badly for this :)
Rooster has spoken.
Rooster2 is offline   Reply With Quote
Old 10-13-2007, 12:13 PM   #6 (permalink)
RunUO Forum Moderator
 
daat99's Avatar
 
Join Date: Dec 2004
Location: Israel
Age: 27
Posts: 8,163
Send a message via ICQ to daat99 Send a message via AIM to daat99
Default

Tell me what the output of this line before it crash:
Code:
Console.Writeline("cur: '"+currentFortune+"' count: '"+tipsXML.FirstChild.ChildNodes.Count+"'.");
Place it before the line that crashes (you may comment the crashing line as remark).


P.S.
Regardless of the crash you can use the method getTipFromNumber to get the tip like this:
Code:
tbFortune.Text = getTipFromNumber(currentFortune);
This way you can get the tip in several locations of your script while you will need to debug only one spot when it crashes (that is the method itself).


P.P.S.
I've added some useful console.writelines to the method to catch exceptions, use this code instead of the method you have:
Code:
private string getTipFromNumber (int number)
{
	//first go to the root, get the tip based on number between 0 and N-1, get the inner text (the tip itself).
	if (tipsXML==null) //if we don't have a valid xml document open
		Console.WriteLine("I don't have an xml file to read.");
	else if (tipsXML.FirstChild==null) //if our xml document doesn't have a root element (aka first child)
		Console.WriteLine("The XML document doesn't have a root element.");
	else if (tipsXML.FirstChild.Count<=number || number<0) //make sure the number we received is between 0 and the childs count minus 1 for last child.
	{
		Console.WriteLine("I don't have a tip number: '"+number+"' in the XML file.");
		Console.WriteLine("I can accept only tips from '0' to '"+tipsXML.FirstChild.Count-1+"'.");
	}	
	else //everything seems to be ok, return the message
		return tipsXML.FirstChild.ChildNodes[number].InnerText;

	//if we reached this point than we have a problem.

	//in real programming we'll throw an invalid arguments exception and let the program crash.
	//throw new ArgumentException("Unable to access tip number '"+number+"'.");

	//but if you don't want to crash for some weired reason (like RunUO scripting) you just return an error string back:
	return "NULL REFERENCE EXCEPTION WAS PREVENTED while trying to access tip '"+number+"', please contact the administrator and report this message and the number '"+number+"'.";

	//Only have ONE option enabled and the other marked out, either throw the exception (real programmer) or return the bad message ("programming for kids"), don't have both lines active (without remarks) at the same time or you'll get "unreachable code detected" warning!
}
__________________
I always try to help
Sometimes, I don't know how....

My Web Page
Forum Rules
-------------------------------------------------------------
Extensive OWLTR System | Token System | World Teleporters
-------------------------------------------------------------

Last edited by daat99; 10-13-2007 at 12:33 PM.
daat99 is offline   Reply With Quote
Old 10-13-2007, 12:58 PM   #7 (permalink)
Forum Expert
 
Join Date: May 2003
Posts: 469
Send a message via AIM to Rooster2
Default

Quote:
Originally Posted by daat99 View Post
Tell me what the output of this line before it crash:
Code:
Console.Writeline("cur: '"+currentFortune+"' count: '"+tipsXML.FirstChild.ChildNodes.Count+"'.");
Place it before the line that crashes (you may comment the crashing line as remark).
That line didn't cause any error/crash whatsoever. It also 'did nothing' in terms of when I click the button though.

Quote:
P.S.
Regardless of the crash you can use the method getTipFromNumber to get the tip like this:
Code:
tbFortune.Text = getTipFromNumber(currentFortune);
This way you can get the tip in several locations of your script while you will need to debug only one spot when it crashes (that is the method itself).
changed it to this.
Quote:
P.P.S.
I've added some useful console.writelines to the method to catch exceptions, use this code instead of the method you have:
Code:
private string getTipFromNumber (int number)
{
	//first go to the root, get the tip based on number between 0 and N-1, get the inner text (the tip itself).
	if (tipsXML==null) //if we don't have a valid xml document open
		Console.WriteLine("I don't have an xml file to read.");
	else if (tipsXML.FirstChild==null) //if our xml document doesn't have a root element (aka first child)
		Console.WriteLine("The XML document doesn't have a root element.");
	else if (tipsXML.FirstChild.Count<=number || number<0) //make sure the number we received is between 0 and the childs count minus 1 for last child.
	{
		Console.WriteLine("I don't have a tip number: '"+number+"' in the XML file.");
		Console.WriteLine("I can accept only tips from '0' to '"+tipsXML.FirstChild.Count-1+"'.");
	}	
	else //everything seems to be ok, return the message
		return tipsXML.FirstChild.ChildNodes[number].InnerText;

	//if we reached this point than we have a problem.

	//in real programming we'll throw an invalid arguments exception and let the program crash.
	//throw new ArgumentException("Unable to access tip number '"+number+"'.");

	//but if you don't want to crash for some weired reason (like RunUO scripting) you just return an error string back:
	return "NULL REFERENCE EXCEPTION WAS PREVENTED while trying to access tip '"+number+"', please contact the administrator and report this message and the number '"+number+"'.";

	//Only have ONE option enabled and the other marked out, either throw the exception (real programmer) or return the bad message ("programming for kids"), don't have both lines active (without remarks) at the same time or you'll get "unreachable code detected" warning!
}
Had problem with this block/method.. Tells me that System.Xml.XmlNode does not contain a definition for 'Count' (or count for what it's worth).
__________________
Quote:
Originally Posted by Ravatar
:!: *Best Thread of the Day* :!:



The Good: It's a really good UOA clone.
The Bad: It's not a UO emulator.
The Ugly: You are going to get flamed badly for this :)
Rooster has spoken.
Rooster2 is offline   Reply With Quote
Old 10-13-2007, 05:15 PM   #8 (permalink)
RunUO Forum Moderator
 
daat99's Avatar
 
Join Date: Dec 2004
Location: Israel
Age: 27
Posts: 8,163
Send a message via ICQ to daat99 Send a message via AIM to daat99
Default

sorry about that
change:
Code:
else if (tipsXML.FirstChild.Count<=number || number<0) //make sure the number we received is between 0 and the childs count minus 1 for last child.
to:
Code:
else if (tipsXML.FirstChild.ChildNodes.Count<=number || number<0) //make sure the number we received is between 0 and the childs count minus 1 for last child.
__________________
I always try to help
Sometimes, I don't know how....

My Web Page
Forum Rules
-------------------------------------------------------------
Extensive OWLTR System | Token System | World Teleporters
-------------------------------------------------------------
daat99 is offline   Reply With Quote
Old 10-13-2007, 06:26 PM   #9 (permalink)
Forum Expert
 
Join Date: May 2003
Posts: 469
Send a message via AIM to Rooster2
Default

It is giving me the Null Reference Exception. The number reported counts up per click, and originally started at 3 and I tried a few times, so at about 7 now. There are 19 current tips.. tipsXML.xml file looks as such:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<root>
	<tip>this is tip 0</tip> 
	<tip>this is tip 1</tip> 
	<tip>this is tip 2</tip> 
	<tip>this is tip 3</tip>
	<tip>this is tip 4</tip>
	<tip>this is tip 5</tip>
	<tip>this is tip 6</tip>
	<tip>this is tip 7</tip>
	<tip>this is tip 8</tip>
	<tip>this is tip 9</tip>
	<tip>this is tip 10</tip>
	<tip>this is tip 11</tip>
	<tip>this is tip 12</tip>
	<tip>this is tip 13</tip>
	<tip>this is tip 14</tip>
	<tip>this is tip 15</tip>
	<tip>this is tip 16</tip>
	<tip>this is tip 17</tip>
	<tip>this is tip 18</tip>
	<tip>this is tip 19</tip>
</root>
I did have that N-1 line at the end (wasn't sure what N-1 meant, if it should be there, etc..) It did the same thing..
__________________
Quote:
Originally Posted by Ravatar
:!: *Best Thread of the Day* :!:



The Good: It's a really good UOA clone.
The Bad: It's not a UO emulator.
The Ugly: You are going to get flamed badly for this :)
Rooster has spoken.
Rooster2 is offline   Reply With Quote
Old 10-14-2007, 02:11 AM   #10 (permalink)
RunUO Forum Moderator
 
daat99's Avatar
 
Join Date: Dec 2004
Location: Israel
Age: 27
Posts: 8,163
Send a message via ICQ to daat99 Send a message via AIM to daat99
Default

Quote:
Originally Posted by Rooster2 View Post
It is giving me the Null Reference Exception. The number reported counts up per click, and originally started at 3 and I tried a few times, so at about 7 now. There are 19 current tips.. tipsXML.xml file looks as such:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<root>
	<tip>this is tip 0</tip> 
	<tip>this is tip 1</tip> 
	<tip>this is tip 2</tip> 
	<tip>this is tip 3</tip>
	<tip>this is tip 4</tip>
	<tip>this is tip 5</tip>
	<tip>this is tip 6</tip>
	<tip>this is tip 7</tip>
	<tip>this is tip 8</tip>
	<tip>this is tip 9</tip>
	<tip>this is tip 10</tip>
	<tip>this is tip 11</tip>
	<tip>this is tip 12</tip>
	<tip>this is tip 13</tip>
	<tip>this is tip 14</tip>
	<tip>this is tip 15</tip>
	<tip>this is tip 16</tip>
	<tip>this is tip 17</tip>
	<tip>this is tip 18</tip>
	<tip>this is tip 19</tip>
</root>
I did have that N-1 line at the end (wasn't sure what N-1 meant, if it should be there, etc..) It did the same thing..
Ok, I can see the problem now, the first child in your XML file is the XML declaration, we need to choose the first ELEMENT in the document instead


So, we change the method like this:
Code:
private string getTipFromNumber(int number)
{
	//first go to the root, get the tip based on number between 0 and N-1, get the inner text (the tip itself).
	if (tipsXML == null) //if we don't have a valid xml document open
		Console.WriteLine("I don't have an xml file to read.");
	else if (tipsXML.DocumentElement == null) //if our xml document doesn't have a root element (aka first ELEMENT child)
		Console.WriteLine("The XML document doesn't have a root element.");
	else if (tipsXML.DocumentElement.ChildNodes.Count <= number || number < 0) //make sure the number we received is between 0 and the childs count minus 1 for last child.
	{
		Console.WriteLine("I don't have a tip number: '" + number + "' in the XML file.");
		Console.WriteLine("I can accept only tips from '0' to '" + (tipsXML.DocumentElement.ChildNodes.Count - 1) + "'.");
	}
	else //everything seems to be ok, return the message
		return tipsXML.DocumentElement.ChildNodes[number].InnerText;

	//if we reached this point than we have a problem.

	//in real programming we'll throw an invalid arguments exception and let the program crash.
	throw new ArgumentException("Unable to access tip number '"+number+"'.");

	//but if you don't want to crash for some weired reason (like RunUO scripting) you just return an error string back:
	//return "NULL REFERENCE EXCEPTION WAS PREVENTED while trying to access tip '" + number + "', please contact the administrator and report this message and the number '" + number + "'.";

	//Only have ONE option enabled and the other marked out, either throw the exception (real programmer) or return the bad message ("programming for kids"), don't have both lines active (without remarks) at the same time or you'll get "unreachable code detected" warning!
}
__________________
I always try to help
Sometimes, I don't know how....

My Web Page
Forum Rules
-------------------------------------------------------------
Extensive OWLTR System | Token System | World Teleporters
-------------------------------------------------------------
daat99 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5