RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Debugging Code by Line numbers ???

roadmaster

Sorceror
Debugging Code by Line numbers ???

I am currently working on some scripts and i am adding debugging code to the scripts, Im sure theres probably a better way to do it but i am learning as i go and this is what i have so far. What i am wanting to do is to set it up so my second Console.WriteLine will tell what line # has caused a problem. What i have now works but if i add code further toward the top of the file then i will have to go back and change all debugging code after the modifications. So my question is, is there some method that can be used to report the line number of a problem in a Console.WriteLine? Such as in my try statement "mFEARankThree(npc);" doesnt work right so it runs my current catch but in the second WriteLine line# 64 may not always be correct if i add code and dont remember to change all of my debugging code. so what could i replace "line# 64" with that would report the problem line number?

Code:
else if(Drow.Rank == 3)
{
  try //Debug check
  {
    mFEARankThree(npc);
  }
  catch
  {
    Console.WriteLine("***          Fatal Exception Caught          ***");
    Console.WriteLine("***        DrowEquipArmor.cs Line# 64        ***");
    Console.WriteLine("*** Method Call to method mFEARankThree(npc) ***");
  }
}


thank you for your time and any effort you expend in helping me with this.


roadmaster
 

Jason

Wanderer
This code makes no sense.

NO you can't print out a problem based on the line number.

If there is a code error, use the goto line feature of your program. I mean your idea makes no sense.

Why not just write the code, correct and you won't have a problem.
 

roadmaster

Sorceror
@Jason
If there is a code error, use the goto line feature of your program.

Sir, what are you refering to? the only "goto feature" i am familiar with would be used in a "case" statement, something like this. Is this what you are refering to?

case 16:
{
m_LastCompassionLoss = reader.ReadDeltaTime();
goto case 15;
}

I am still learning so is there another way to use "goto"? could you provide me with an example?


Why not just write the code, correct and you won't have a problem.

In an ideal world this would be great, I am not worried about my writing the code right, I am worried about someone else modifying my code "once released" and then wanting me to find out whats wrong with my code that they modified.


roadmaster
 

PM_Snake

Wanderer
i think he meant that you can press ctrl + f to search a keyword an ctrl + g ? to go to a line

If your Commandwindow tells you that you have an error in MyScript.cs at line 36748

then you go in your editor, press ctrl + g ( i think ) an write in your line 36748. Now you have to press Enter and you will be at this line.....
 

roadmaster

Sorceror
i think he meant that you can press ctrl + f to search a keyword an ctrl + g ? to go to a line

If your Commandwindow tells you that you have an error in MyScript.cs at line 36748

then you go in your editor, press ctrl + g ( i think ) an write in your line 36748. Now you have to press Enter and you will be at this line.....

I am sorry, perhaps i misrepresented what i am trying to do...

If you have a fatal error on compiling and you use the -debug switch it gives you a line # that could be the line with the problem or close to.

what i want to know is, can the code used by the debug switch be incorporated into my try catch block (in my code) so that if I have an error it will tell me the line number of the cause of the error or close to?

the reason i want to have an error check for that code is that it calls a custom function that equips a npc with items ie... armor, weapons, misc. the problem is if someone later adds items to those custom functions that causes the function to not work but will still compile just wont equip on the npc, i would like it to tell me when the item doesn't equip right. this way i would be able to find the bug more easily.

I hope i have explained this enough for you to understand what i am trying to do.


roadmaster
 

Cabadam

Sorceror
You're wanting the line number at runtime you mean rather than compile time? I'm not sure if thats available or not.

Actually catch an exception object:
try
{
...
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}

That *might* show it. But I'm not sure how RunUO is compiling the scripts in. If it is compiling in "release" mode, then the line numbers will not be available. If its in "Debug" mode, they will. At least, thats how it works on "normal" projects. I'm not 100% sure if this applies to things like this, where the scripts are compiled at runtime. Guess try it, see what you get.
 

roadmaster

Sorceror
You're wanting the line number at runtime you mean rather than compile time? I'm not sure if thats available or not.

yes exactly, if there is an error at runtime that causes my code to not process i would like to have the error show the line # of the calling method. I am still learning and maybe this cant be done, but i would like to try.

Actually catch an exception object:
try
{
...
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}

I will try this when i get home, thank you for your helpful suggestion.


roadmaster
 

roadmaster

Sorceror
Thank you for your suggestion cabadam!


Console.WriteLine(ex.StackTrace);

does the same thing as:

Console.WriteLine(ex);


I guess ill just scrap the idea and just go with Console.Writeline(ex)


thanks for the help


roadmaster
 
Top