10 things about leading

by Ronnen

Reflecting on my first year in my new role as a Teacher, here’s just a little of what I have learned.
1. Vision. Don’t just focus on the bricks. See the cathedral. albert-einstein
2. Courage. Don’t be limited by your job description. Make a difference in any way you can.
3. Creativity. Think in new ways. Experiment with different ideas. Innovate.
4. Resilience. Rise above disappointments, complaints and people who work against you.
5. Empathy. Remember that everyone has a story. Be patient. Avoid talking to people when you haven’t had enough sleep.
6. Reflection. Admit when you are wrong and apologize. Occasionally apologizing when you weren’t wrong might be helpful too.
7. Collaboration. The power of a helpful, inspirational, global PLN is immeasurable. A trusted in-school PLN is even more valuable.

8. Communication: Talk. Explain. Ask. Listen. You can achieve a great deal via one conversation at a time.
9. Initiative. If you have an idea, run with it. Don’t wait for a better time, particular conditions or permission to try.
10. Persistence. If you believe in something, fight for it, negotiate for it, pay for it, work for it.
11. Humility. Ask for help. Consult. Get advice. If something is worthwhile, you don’t need your name on it. It’s not about who gets the credit.

Most Amazing Video

by Ronnen

If this video doesn't bring a tear to your eyes and makes you smile for the rest of the day, you should reconsider some issues..Watch it from beginning to end - you won't regret it.





Using gmail inside C# app

by Ronnen

Sending Email from a C# application using Gmail account could not be easier. All It takes is writing few lines of code, and the magic appears, like in many C# implementations.

  • add ‘using System.Net.Mail;’
  • copy the code below (better - wrap it with a function), and change the relevant parameters. (username,password..)

Enjoy !

If you have any comment\improvement – drop me a line at the bottom.

 

class SendWithGmail { static void Main(string[] args) { MailMessage mail = new MailMessage(); mail.To.Add("DestinationUser@gmail.com"); mail.To.Add("DestinationUser@yahoo.com"); mail.From = new MailAddress("SourceUser@gmail.com"); mail.Subject = "Email using Gmail"; string Body = "Hi, this mail is to test sending mail"; mail.Body = Body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.Credentials = new System.Net.NetworkCredential ("SourceUser@gmail.com", "SourceUserPassword"); smtp.EnableSsl = true; smtp.Send(mail); } }





















Bookmark and Share

Log4Net

by Ronnen

log4net is a great tool for creating Log file. (apache product)
After a short research, I understood how to use that tool for a simple writing-logs task.
Basic steps for using described here, and can be execute in a few minutes.
(there is a small project at the end, so you don't have to 'bother' and implement the steps)

How To Use:

First, You will need the dll file (260 kb).
You can download it from here, or you can find it inside my attached project. (link at the bottom)

Put that dll file in a place that will be easy to reference to.
Add a Reference to that dll from your project.

Add the following code to the Assembly.cs file.

// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.config in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.

In the app.config file:
Inside the 'configSections' tag,
Add a new section-line using 'log4net' name:

<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>






Add a new section ("log4net") to the same app.config file (Sibling to 'configSections')

<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="C:\Documents and Settings\Admin\Desktop\Logfile.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value=
"%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>
</log4net>

















In the class:
Add using log4net;

Define a static logger variable at the top of your class.
Something like this:

private static readonly ILog log = LogManager.GetLogger(typeof(Program));
(Here 'Program' is the class name)

Start using the logger in your class, Enjoy !

My Test class is:

class Program
{
// A static logger variable that will reference the class name ('Program')
private static readonly ILog log = LogManager.GetLogger(typeof(Program));

static void Main(string[] args)
{
log.Info("Entering application.");

for (int i = 0; i < 3; i++)
{
log.DebugFormat("In The loop (i = {0})", i);
log.ErrorFormat("Error message (i = {0})", i);
log.FatalFormat("Fatal message (i = {0})", i);
log.WarnFormat("Warn message (i = {0})", i);
}

log.Info("Exiting application.");
}
}
























The output is:

2009-11-04 09:36:46,078 [10] INFO Program [(null)] - Entering application.
2009-11-04 09:36:46,125 [10] DEBUG Program [(null)] - In The loop (i = 0)
2009-11-04 09:36:46,140 [10] ERROR Program [(null)] - Error message (i = 0)
2009-11-04 09:36:46,140 [10] FATAL Program [(null)] - Fatal message (i = 0)
2009-11-04 09:36:46,140 [10] WARN Program [(null)] - Warn message (i = 0)
2009-11-04 09:36:46,140 [10] DEBUG Program [(null)] - In The loop (i = 1)
2009-11-04 09:36:46,140 [10] ERROR Program [(null)] - Error message (i = 1)
2009-11-04 09:36:46,140 [10] FATAL Program [(null)] - Fatal message (i = 1)
2009-11-04 09:36:46,140 [10] WARN Program [(null)] - Warn message (i = 1)
2009-11-04 09:36:46,140 [10] DEBUG Program [(null)] - In The loop (i = 2)
2009-11-04 09:36:46,140 [10] ERROR Program [(null)] - Error message (i = 2)
2009-11-04 09:36:46,140 [10] FATAL Program [(null)] - Fatal message (i = 2)
2009-11-04 09:36:46,140 [10] WARN Program [(null)] - Warn message (i = 2)
2009-11-04 09:36:46,140 [10] INFO Program [(null)] - Exiting application.



Download

Google Translator

by Ronnen

Today I had a great time discovering the Google-Translate-API and how easy it is for using. (those google guys are amazing)

Using that API, I wrote down a small C# app that translates text from English to Hebrew and vice versa.
The source can be used for your own purpose as well.

The studying process of C# and the Tech world is amazing,
I'm having great time on each step of the way. .

Merge Sort

by Ronnen

Sorting is a common activity in the programming work.
Merge-Sort is one of the best implementation for Sorting,
Since Its running on O(nlogn) - which is the best run-time available for Sorting.
On one of my projects I had a requirement to sort Objects.
After a short research, I decided to write a Generic Merge-Sort Class that will work for me.
During the work, I found few major points that should be considered while using that Generic Class for Sorting. (publication)

  • Preface
Merge Sort (pseudo-code in the picture) is a recursive algorithm, that Split an Array of the Objects to 2 sub Arrays - A,B (which initialize to infinite values), Sort this 2 sub-Arrays and finally merge them. (Therefore, 'merge' sort)
  • Using
For using the Generic Merge Sort, you should (probably) know what is the objects parameter that the objects will be sorted by.
Now, Since the 2 sub-Arrays should be initialized with an infinite value, You should use the default-ctor to make that initialization.
  • Example
For sorting an Array of 'Persons' Objects that will be sorted according to their ID number (from low to high), you should declare infinite ID-number (infinite int32 value) in the default-ctor.
(I know This is not an elegant implementation, but In that case there is no alternative.)
  • Points for attention
When writing a class for using Generic Merge Sort, you should implement the 'IComparable' interface since the code using the 'CompareTo' function - to compare objects.
You should also write your own 'overloading operators' that will be in use to compare objects: ==, !=, >, >=, <, <= plus 'Equals' and 'GetHashCode'. Please notice that 'GetHashCode' will be in use by the CLR implicitly - so you must write your own implementation that will return the same value for the same object.





class MergeSort { /// <summary> /// Sorts an array of Objects /// IComparable - use 'CompareTo' to compare objects /// where T : new() - need to create a new Type 'T' object inside the method /// </summary> /// <param name="X"></param> public static T[] Merge_Sort<T>(T[] X) where T : IComparable, new() { int n = X.Length; X = MegrgeSort_Internal(X, n); return X; } /// <summary> /// Internal method for sorting /// </summary> /// <param name="X"></param> /// <param name="n"></param> private static T[] MegrgeSort_Internal<T>(T[] X, int n) where T : IComparable, new() { // Define 2 aid Sub-Arrays T[] A = new T[(n / 2) + 2]; T[] B = new T[(n / 2) + 2]; // Initialize the 2 Sub-Arrays with an infinite 'sorting parameter' // Therefor, You must include a default ctor in your class // which will initialize an infinite value - To the sorting parameter // using 'where T : new()' here for (int i = 0; i < A.Length; i++) { A[i] = new T(); ; B[i] = new T(); } // Recursive Stop-Condition, Sorting a Basic Array (Size 2) if (n == 2) { int CompareValue = X[0].CompareTo(X[1]); if (CompareValue > 0) { T tempT = X[0]; X[0] = X[1]; X[1] = tempT; } } else { // The Sub-Arrays Size is Large than 2 if (n > 2) { int m = n / 2; // Initialize the 2 Sub-Arrays (The first relevant values) for (int i = 0; i < m; i = i + 1) { A[i] = X[i]; } for (int j = m; j < n; j++) { B[j - m] = X[j]; } // 2 Recursive Calling, Sorting Sub-Arrays A = MegrgeSort_Internal(A, m); B = MegrgeSort_Internal(B, n - m); // Merging the Sorted Sub-Arrays into the main Array int p = 0; int q = 0; for (int k = 0; k < n; k++) { { int CompareValure = A[p].CompareTo(B[q]); if (CompareValure == 0 || CompareValure == -1) { X[k] = A[p]; p = p + 1; } else { X[k] = B[q]; q = q + 1; } } } } // if } // else return X; } }
































































2 project attached:
  • A simple (int32) Implementation, that Sorts an Array of Integers
  • A Generic Merge Sort, Plus a simple 'Person' class that will demonstrate the using of the Generic code

Simple Merge Sort Download
Generic Merge Sort Download





Bookmark and Share

Password Generator

by Ronnen

CSince I'm not satisfied with all of the available 'Password-Generators', I decided t write one of my own. (in C#)

Its a Simple Password Generator that can
Generate a Password of any length.

You can download it at: Link


Regular Expressions Summary

by Ronnen

Regular expressions are a language of their own. When you learn a new programming language, they're this little sub-language that makes no sense at first glance. Many times you have to read another tutorial, article, or book just to understand the "simple" pattern described.
In computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters.

Here are some of the most useful Regular expressions Patterns.

  • Username

Pattern:

/^[a-z0-9_-]{3,16}$/

Description:

We begin by telling the parser to find the beginning of the string (^), followed by any lowercase letter (a-z), number (0-9), an underscore, or a hyphen. Next, {3,16} makes sure that are at least 3 of those characters, but no more than 16. Finally, we want the end of the string ($).

String that matches:

my-us3r_n4m3

String that doesn't match:

th1s1s-wayt00_l0ngt0beausername (too long)

  • Password

Pattern:

/^[a-z0-9_-]{6,18}$/

Description:

Matching a password is very similar to matching a username. The only difference is that instead of 3 to 16 letters, numbers, underscores, or hyphens, we want 6 to 18 of them ({6,18}).

String that matches:

myp4ssw0rd

String that doesn't match:

mypa$$w0rd (contains a dollar sign)

  • Email

Pattern:

/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/

Description:

We begin by telling the parser to find the beginning of the string (^). Inside the first group, we match one or more lowercase letters, numbers, underscores, dots, or hyphens. I have escaped the dot because a non-escaped dot means any character. Directly after that, there must be an at sign. Next is the domain name which must be: one or more lowercase letters, numbers, underscores, dots, or hyphens. Then another (escaped) dot, with the extension being two to six letters or dots. I have 2 to 6 because of the country specific TLD's (.ny.us or .co.uk). Finally, we want the end of the string ($).

String that matches:

john@doe.com

String that doesn't match:

john@doe.something (TLD is too long)

More reading\testing at:
  • regexlib.com
  • supercrumbly.com
  • txt2re.com