I just thought... Such thread might help a lot of people, and all the people might show their way of coding, which will improve the imagination of others, and show them that a lot more stuff than they thought can be done.
ps: I consider myself one of the "others".
Tips:
1: To be better for everyone, if you have downloaded a free source project from somewhere - include it in here, and say if the code either matters for it or not.
2: Include the language it is coded on (C#, php, C++, VB, etc...).
3: Give some more information on what you are using it for and whatever more you want to include about the specific tip/function.
4: Say if it is either tested or not yet.
5: If you just want to make post with improvement of someone's code, why not post it? If they like it, you might have just helped them.
6: If it has been tested, say if it is finished, or you are still not very happy of the result, after all someone might help you.
Sooo... I will start with a few helpful functions:
Language: C#
Works with: LFS_External from the forums.
Result: Replaces clsConnection to clsPlayer and oposite
Level: Very easy.
Number functions in the code strip: 2
State: Tested and working like a charm. :P
----------------------------------
Language: C#
Works with: Will work with every program.
Source used: LFS_External from the forums.
Result: Removes all lfs color codes in a string.
Level: TOO easy.
Number functions in the code strip: 1
State: Tested and works perfectly.
----------------------------------
Language: C#
Works with: Will work with every program.
Source used: LFS_External from the forums.
Result: Reports errors to external text file.
Level: Not too easy.
Number functions in the code strip: 1
State: Tested and works perfectly.
Requirements: needs file "errorFile.txt" in folder "\errors\" which has to be located on the same location as your project. Also needs a string[] with additional information, which, of course can be null.
Request: It has a disabled code, which I couldn't get to work good, so if you have any solutions for it, you can tell me.
Example of how to call this function:
Note: This is code from my insim, catching errors in the MSO thread.
Tip: Use shortened codes which you have written somewhere to be recognised.
In this case the error code says:
MSO - MSO thread
TWR - Too Wide Range (of the exception)
UNN - Unknown (can't guess what exactly went wrong since the whole MSO thread is "surrounded" with try {}
MSO(UNN) will be added after the error code, so there is bigger chance if 2 errors occur in 1 second, to be recognised.
----------------------------------
Language: C#
Works with: Will work with every program.
Source used: LFS_External from the forums.
Result: Gets absolute(or more like - lowest) distance between 2 points in given _closed_ figure.
Level: Probably easy..
Number functions in the code strip: 1
State: Tested and works perfectly.
Requirements: No requirements
What I use this for mainly: To detect the distance between 2 players in nodes.
I call it like: the_class_its_located_in.absDifference(node of player1, node of player2, total count of nodes on track)
----------------------------------
Language: C#
Works with: Will work with every program.
Source used: LFS_External from the forums.
Result: Gets the time between the date you specify and today. Can be made to work with 2 different dates pretty easy too.
Level: Probably easy..
Number functions in the code strip: 1
State: Tested and works perfectly.
Requirements: No requirements
----------------------------------
Now something I need help with. I guess that the solution is pretty easy, but I didn't have much time for this function anyway...
Language: C#
Works with: Will work with every program.
Source used: LFS_External from the forums.
Result: Get days in the month.
Level: Probably easy..
Number functions in the code strip: 1
State: Not working - problems with February.
----------------------------------
Right after I finished writing the post and was about to click the Submit button - it reminded me of buttons! So there is another thing that could make your code more readable:
Language: C#
Works with: Will work with LFS_External from the forums.
Result: Display a sequence of buttons, and delete sequences.
Level: Probably easy..
Number functions in the code strip: 2
State: Working better than expected.
Note: Don't display too many buttons at once, or you may make your guest lose connection to the server..
There is a UCID - Used each time you display a button.
There is also clsConnection option - If you want to display buttons containing information about one player - with the UCID of another. UCID and clsConnection, of course - CAN be of the same person.
BTW: I never got to know what the RequestID of a button is used for... Proves what a lame coder I am lol! But people learn as long as they live, they say.
----------------------------------
That was some hot code from my program's kitchen. Now let us see something from yours.
::: I know that my post is a little bit confusing and I'm sorry. It's because I didn't take my time to structure the post and make the explanations sound more clear. So once again: I'm sorry if you don't understand something. You can always ask tho.
All the examples I gave you are simple and all you need is basic knowledge of coding to make them work.
ps: I consider myself one of the "others".
Tips:
1: To be better for everyone, if you have downloaded a free source project from somewhere - include it in here, and say if the code either matters for it or not.
2: Include the language it is coded on (C#, php, C++, VB, etc...).
3: Give some more information on what you are using it for and whatever more you want to include about the specific tip/function.
4: Say if it is either tested or not yet.
5: If you just want to make post with improvement of someone's code, why not post it? If they like it, you might have just helped them.
6: If it has been tested, say if it is finished, or you are still not very happy of the result, after all someone might help you.
Sooo... I will start with a few helpful functions:
Language: C#
Works with: LFS_External from the forums.
Result: Replaces clsConnection to clsPlayer and oposite
Level: Very easy.
Number functions in the code strip: 2
State: Tested and working like a charm. :P
static public clsPlayer convertToPlayer(clsConnection C)
{
foreach (clsPlayer P in Form1.Players)
{
if (P.PlayerID == C.PLID)
{
return P;
}
}
return null;
}
static public clsConnection convertToConnection(clsPlayer P)
{
foreach (clsConnection C in Form1.Connections)
{
if (C.PLID == P.PlayerID)
{
return C;
}
}
return null;
}
----------------------------------
Language: C#
Works with: Will work with every program.
Source used: LFS_External from the forums.
Result: Removes all lfs color codes in a string.
Level: TOO easy.
Number functions in the code strip: 1
State: Tested and works perfectly.
static public string removeColorCodes(string coloredText)
{
coloredText = coloredText.Replace("^1", "").Replace("^2", "").Replace("^3", "").Replace("^4", "").Replace("^5", "").Replace("^6", "").Replace("^7", "").Replace("^8", "").Replace("^9", "");
return coloredText;
}
----------------------------------
Language: C#
Works with: Will work with every program.
Source used: LFS_External from the forums.
Result: Reports errors to external text file.
Level: Not too easy.
Number functions in the code strip: 1
State: Tested and works perfectly.
Requirements: needs file "errorFile.txt" in folder "\errors\" which has to be located on the same location as your project. Also needs a string[] with additional information, which, of course can be null.
Request: It has a disabled code, which I couldn't get to work good, so if you have any solutions for it, you can tell me.
static public void buildErrorReportOnCrash(Exception E, string reportSituation, string errorDescr, string[] additionalInfo, string Username)
{
string errorInfo = @"errors";
string userInfo = @"users";
bool MEFCF = false;
bool SEFCF = false;
bool MEFWF = false;
bool SEFWF = false;
try
{
if (File.Exists(userInfo + "\\" + Username + ".txt") == true)
{
File.Copy(Application.ExecutablePath.Remove(Application.ExecutablePath.LastIndexOf('\\')) + "\\users\\" + Username + ".txt", Application.ExecutablePath.Remove(Application.ExecutablePath.LastIndexOf('\\')) + "\\users\\" + "BACKUP (" + Username + ")" + Convert.ToString(System.DateTime.Now.Hour) + ":" + Convert.ToString(System.DateTime.Now.Minute) + ":" + Convert.ToString(System.DateTime.Now.Second) + " " + Convert.ToString(System.DateTime.Now.Day) + "." + Convert.ToString(System.DateTime.Now.Month) + "." + Convert.ToString(System.DateTime.Now.Year));
}
}
catch
{
reportSituation += "-UBF";
}
string errorCode = Convert.ToString(System.DateTime.Now.Hour).PadLeft(2, '0');
errorCode += Convert.ToString(System.DateTime.Now.Minute).PadLeft(2, '0');
errorCode += Convert.ToString(System.DateTime.Now.Second).PadLeft(2, '0');
errorCode += Convert.ToString(System.DateTime.Now.Day).PadLeft(2, '0');
errorCode += Convert.ToString(System.DateTime.Now.Month).PadLeft(2, '0');
errorCode += Convert.ToString(System.DateTime.Now.Year) + "-" + errorDescr;
try
{
if (File.Exists(errorInfo + "\\errorFile.txt") == false)
{
File.Create(errorInfo + "\\errorFile.txt");
StreamWriter NewFile = new StreamWriter(errorInfo + "\\errorFile.txt");
NewFile.WriteLine("File created on: " + System.DateTime.Now.ToString());
NewFile.WriteLine("");
NewFile.Flush();
NewFile.Close();
}
}
catch
{
MEFCF = true;
}
try
{
/*if (File.Exists(errorInfo + "\\" + errorCode + ".txt") == false)
{
File.Create(errorInfo + "\\" + errorCode + ".txt");
StreamWriter NewFile = new StreamWriter(errorInfo + "\\" + errorCode + ".txt");
NewFile.WriteLine("File created on: " + System.DateTime.Now.ToString());
NewFile.WriteLine("");
NewFile.Flush();
NewFile.Close();
}*/
}
catch
{
SEFCF = true;
}
try
{
string mainErrorFile = "Failed to read file!";
try
{
StreamReader ErRe1 = new StreamReader(errorInfo + "\\errorFile.txt");
mainErrorFile = ErRe1.ReadToEnd();
ErRe1.Dispose();
ErRe1.Close();
}
catch { }
//Thread.Sleep(250);
StreamWriter ErWr1 = new StreamWriter(errorInfo + "\\errorFile.txt");
try
{
ErWr1.WriteLine(mainErrorFile);
}
catch { }
ErWr1.WriteLine("------------------------------------------------------------" + errorCode + "------------------------------------------------------------");
ErWr1.WriteLine("Exception: " + E.ToString());
ErWr1.WriteLine("------------------------------------------------------------");
try
{
ErWr1.WriteLine("Inner: " + E.InnerException.ToString());
}
catch
{
ErWr1.WriteLine("Inner: Inner exception not available");
}
try
{
ErWr1.WriteLine("Inner: " + E.InnerException.InnerException.ToString());
}
catch
{
ErWr1.WriteLine("Inner: Inner exception(x2) not available");
}
try
{
ErWr1.WriteLine("Inner: " + E.InnerException.InnerException.InnerException.ToString());
}
catch
{
ErWr1.WriteLine("Inner: Inner exception(x3) not available");
}
ErWr1.WriteLine("------------------------------------------------------------");
ErWr1.WriteLine("");
ErWr1.WriteLine("Report: " + reportSituation);
ErWr1.WriteLine("");
ErWr1.WriteLine("------------------------------------------------------------");
for (int tempint1 = 0; tempint1 < 15; tempint1++)
{
try
{
ErWr1.WriteLine("Add. Info(" + tempint1 + "): " + additionalInfo[tempint1]);
}
catch
{
ErWr1.WriteLine("Add. Info(" + tempint1 + "): Additional info n." + tempint1 + " does not contain any information.");
}
}
ErWr1.WriteLine("------------------------------------------------------------");
ErWr1.WriteLine("");
ErWr1.WriteLine("User: " + Username);
ErWr1.WriteLine("");
ErWr1.WriteLine("------------------------------------------------------------");
ErWr1.WriteLine("");
ErWr1.WriteLine("");
ErWr1.Flush();
ErWr1.Close();
}
catch
{
MEFWF = true;
}
try
{
/*string subErrorFile = "Failed to read file!";
try
{
StreamReader ErRe2 = new StreamReader(errorInfo + "\\" + errorCode + ".txt");
subErrorFile = ErRe2.ReadToEnd();
ErRe2.Dispose();
ErRe2.Close();
}
catch { }
///Thread.Sleep(250);
StreamWriter ErWr2 = new StreamWriter(errorInfo + "\\" + errorCode + ".txt");
try
{
ErWr2.WriteLine(subErrorFile);
}
catch { }
ErWr2.WriteLine("------------------------------------------------------------" + errorCode + "------------------------------------------------------------");
ErWr2.WriteLine("Exception: " + E.ToString());
ErWr2.WriteLine("------------------------------------------------------------");
try
{
ErWr2.WriteLine("Inner: " + E.InnerException.ToString());
}
catch
{
ErWr2.WriteLine("Inner: Inner exception not available");
}
try
{
ErWr2.WriteLine("Inner: " + E.InnerException.InnerException.ToString());
}
catch
{
ErWr2.WriteLine("Inner: Inner exception(x2) not available");
}
try
{
ErWr2.WriteLine("Inner: " + E.InnerException.InnerException.InnerException.ToString());
}
catch
{
ErWr2.WriteLine("Inner: Inner exception(x3) not available");
}
ErWr2.WriteLine("");
ErWr2.WriteLine("------------------------------------------------------------");
ErWr2.WriteLine("");
ErWr2.WriteLine("Report: " + reportSituation);
ErWr2.WriteLine("");
ErWr2.WriteLine("------------------------------------------------------------");
ErWr2.WriteLine("");
ErWr2.WriteLine("User: " + Username);
ErWr2.WriteLine("");
ErWr2.WriteLine("------------------------------------------------------------");
ErWr2.WriteLine("");
ErWr2.WriteLine("");
//ErWr.Flush();
ErWr2.Close();*/
}
catch
{
SEFWF = true;
}
if (MEFCF == true && SEFCF == true)
{
reportSituation += "-EFCF";
}
else if (MEFCF == true && SEFCF == false)
{
reportSituation += "-MEFCF";
}
else if (MEFCF == false && SEFCF == true)
{
reportSituation += "-SEFCF";
}
if (MEFWF == true && SEFWF == true)
{
reportSituation += "-EFWF";
}
else if (MEFWF == true && SEFWF == false)
{
reportSituation += "-MEFWF";
}
else if (MEFWF == false && SEFWF == true)
{
reportSituation += "-SEFWF";
}
Form1.InSim.Send_MST_Message("/msg ^3» ^1An error has occured. Please report to forums.");
Form1.InSim.Send_MST_Message("/msg ^3» ^1Report: ^8" + reportSituation);
Form1.InSim.Send_MST_Message("/msg ^3» ^1E. code: ^8" + errorCode);
Form1.InSim.Send_MST_Message("/msg ^3» ^1Occured with user: ^3" + Username);
}
Example of how to call this function:
Note: This is code from my insim, catching errors in the MSO thread.
try {}
catch (Exception EX)
{
string[] addInf = ("MSO.Msg: " + MSO.Msg + "|" + "MSO.UserType: " + MSO.UserType).Split('|');
additionalFunction.buildErrorReportOnCrash(EX, "MSO-TWR-UNN", "MSO(UNN)", addInf, Connections[GetConnIdx(MSO.UCID)].Username);
}
Tip: Use shortened codes which you have written somewhere to be recognised.
In this case the error code says:
MSO - MSO thread
TWR - Too Wide Range (of the exception)
UNN - Unknown (can't guess what exactly went wrong since the whole MSO thread is "surrounded" with try {}
MSO(UNN) will be added after the error code, so there is bigger chance if 2 errors occur in 1 second, to be recognised.
----------------------------------
Language: C#
Works with: Will work with every program.
Source used: LFS_External from the forums.
Result: Gets absolute(or more like - lowest) distance between 2 points in given _closed_ figure.
Level: Probably easy..
Number functions in the code strip: 1
State: Tested and works perfectly.
Requirements: No requirements
What I use this for mainly: To detect the distance between 2 players in nodes.
I call it like: the_class_its_located_in.absDifference(node of player1, node of player2, total count of nodes on track)
static public float absDifference(int numb1, int numb2, int bound)
{
int numH = numb1;
int numL = numb2;
if (numb1 > numb2)
{
numH = numb1;
numL = numb2;
}
else if (numb1 < numb2)
{
numH = numb2;
numL = numb1;
}
else if (numb1 == numb2)
{
return 0;
}
if (numL < bound && numH < bound)
{
return ((bound - numH) + numL);
}
else
{
return 3333333333;
//Returns error if something is messed up.
}
}
----------------------------------
Language: C#
Works with: Will work with every program.
Source used: LFS_External from the forums.
Result: Gets the time between the date you specify and today. Can be made to work with 2 different dates pretty easy too.
Level: Probably easy..
Number functions in the code strip: 1
State: Tested and works perfectly.
Requirements: No requirements
static public TimeSpan timeSinceThen(DateTime sentTime)
{
DateTime nowTime = System.DateTime.Now;
TimeSpan pastTime = nowTime.Subtract(sentTime);
return pastTime;
}
----------------------------------
Now something I need help with. I guess that the solution is pretty easy, but I didn't have much time for this function anyway...
Language: C#
Works with: Will work with every program.
Source used: LFS_External from the forums.
Result: Get days in the month.
Level: Probably easy..
Number functions in the code strip: 1
State: Not working - problems with February.
static public int daysInMonth(int Month, int Year)
{
switch (Month)
{
case 1: return 31;
case 2:
int visYearF = Year / 4;
float visYearI = Year / 4;
if (float.Parse(visYearF.ToString()) == visYearI)
{
return 29;
}
else
{
return 28;
}
case 3: return 31;
case 4: return 30;
case 5: return 31;
case 6: return 30;
case 7: return 31;
case 8: return 31;
case 9: return 30;
case 10: return 31;
case 11: return 30;
case 12: return 31;
}
return 33;
}
----------------------------------
Right after I finished writing the post and was about to click the Submit button - it reminded me of buttons! So there is another thing that could make your code more readable:
Language: C#
Works with: Will work with LFS_External from the forums.
Result: Display a sequence of buttons, and delete sequences.
Level: Probably easy..
Number functions in the code strip: 2
State: Working better than expected.
Note: Don't display too many buttons at once, or you may make your guest lose connection to the server..
static public void createSequence(string sequenceName, byte UCID, byte RequestID, clsConnection C)
{
switch (sequenceName.ToLower())
{
#region Test
case "test":
Form1.InSim.Send_BTN_CreateButton("test!", LFS_External.InSim.Flags.ButtonStyles.ISB_CLICK | LFS_External.InSim.Flags.ButtonStyles.ISB_LIGHT, 4, 4, 50, 50, 105, UCID, 40, false);
Form1.InSim.Send_BTN_CreateButton("", LFS_External.InSim.Flags.ButtonStyles.ISB_DARK, 100, 100, 50, 50, 101, UCID, 40, false);
Form1.InSim.Send_BTN_CreateButton("", LFS_External.InSim.Flags.ButtonStyles.ISB_LIGHT, 98, 24, 51, 125, 102, UCID, 40, false);
Form1.InSim.Send_BTN_CreateButton("test!", LFS_External.InSim.Flags.ButtonStyles.ISB_C2, 4, 76, 50, 50, 103, UCID, 40, false);
Form1.InSim.Send_BTN_CreateButton("test!", "Enter message", LFS_External.InSim.Flags.ButtonStyles.ISB_CLICK | LFS_External.InSim.Flags.ButtonStyles.ISB_LIGHT, 5, 7, 144, 118, 104, 104, UCID, 40, false);
break;
#endregion
}
}
static public void deleteSequence(string sequenceName, byte UCID)
{
byte startID = 0;
byte endID = 0;
switch (sequenceName.ToLower())
{
#region Test
case "test":
startID = 100;
endID = 155;
break;
#endregion
}
#region Proccess of deletion
if (startID < endID && endID > 1)
{
byte tempByte1 = startID;
while (tempByte1 <= endID)
{
Form1.InSim.Send_BFN_DeleteButton(LFS_External.Enums.BtnFunc.BFN_DEL_BTN, tempByte1, UCID);
tempByte1 += 1;
}
}
#endregion
}
There is a UCID - Used each time you display a button.
There is also clsConnection option - If you want to display buttons containing information about one player - with the UCID of another. UCID and clsConnection, of course - CAN be of the same person.
BTW: I never got to know what the RequestID of a button is used for... Proves what a lame coder I am lol! But people learn as long as they live, they say.
----------------------------------
That was some hot code from my program's kitchen. Now let us see something from yours.
::: I know that my post is a little bit confusing and I'm sorry. It's because I didn't take my time to structure the post and make the explanations sound more clear. So once again: I'm sorry if you don't understand something. You can always ask tho.
All the examples I gave you are simple and all you need is basic knowledge of coding to make them work.