2 Replies - 824 Views - Last Post: 14 March 2016 - 10:36 AM

#1 JacobH  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 176
  • Joined: 07-September 15

What is something you had trouble grasping early on?

Posted 14 March 2016 - 09:46 AM

Hello,

I am interested in the idea of blogging on my site a little both as an exercise of thought and to be hopefully helpful to people who suck as bad I did and still do in many things. The idea is to take a topic people that people who are becoming genuinely interested in coding seriously for the first time such as myself often struggle to understand or implement, and then show them a small working example that clearly shows how it works. I included the "interested in coding seriously" bit because topics should be more advanced than "How to show a message box.". A great example would be something you struggled with despite a little google searching / MSDN browsing.

If you're curious about why I want to do it this way, take this tutorial for example:

http://www.dreaminco...-custom-events/

While I do like tutorial content like above, I learned/got the start I needed to be motivated to learn the topic much better by simply seeing small well written sample code in a single text block with at best a comment or two in areas that are confusing. Once you get something working you failed no matter how small gives a lot of people the motivation to keep going themselves with learning it. I really think a lot of people including myself can get very demotivated when unable to do something that every one thinks is so easy(often it is, but only in hindsight).

With that said, for anyone willing to share something you struggled with a lot very early on I'd love to hear it. As an example, I will go first :)/>.

I struggled with events for way longer than I care to admit. Even after reading the tutorial here linked above it was still weeks before I used anything useful with events. I didn't need to know all the details about using them, that existed every where. The details simply get overwhelming at times as a beginner.

I just needed to get that first example to compile and run and that was enough to spark my interest to learn more about them on my own past that, including starting to use them often even if in terribly poor at first. I am a fairly firm believer that personal trial and error is a crucial part of anyone's early learning of a new skill.

If I could go back and time and show myself a link for events, this is pretty close to what I would have loved to see.

using System;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
using static System.Console;

namespace SimpleWorkingCSharpExamples
{
    // EventArgs: Classes like below are used to pass data through the event, so other people who subscribe -
    // to the event can use that data for their own puproses.
    internal class WorkTimeArgs : EventArgs
    {
        internal DateTime WorkCompletedTime { get; set; }
        internal DateTime WorkStartedTime { get; set; }
        internal TimeSpan TimeTakenToCompleteWork { get; set; }
    }
	
    // A simple example of event being used. One reason to use an event is to allow many classes to react to your results in obtained from a method. 
	// This is what people often call 'subscribers.'
    internal class Worker
    {
        // EventHandler<T> lets you pass the data to subscribers for the purposes mentioned above.
        internal event EventHandler<WorkTimeArgs> WorkCompleted;
        internal bool IsWorkCompleted { get; private set; }

        // If actions are new to you, just think of them as a way to pass a method around for other objects to use.
        // Not really correct but thats all you need to know for now :)/>/>/>/>/>/>.
        private readonly Action _worker;
        internal Worker(Action workerAction)
        {
            _worker = workerAction;
        }

        internal void DoWork()
        {
            var workStartedTime = DateTime.Now;
            _worker?.Invoke();
            var workCompletedTime = DateTime.Now;
            IsWorkCompleted = true;

            // This is the object containing our data other event subscribers outside of this class will see shortly. 
            var workerTimeArgs = new WorkTimeArgs
            {
                WorkStartedTime = workStartedTime,
                WorkCompletedTime = workCompletedTime,
                TimeTakenToCompleteWork = workCompletedTime - workStartedTime
            };

            IsWorkCompleted = true;
            // this is the message sender, in this case it is this class.
            // workerTimeArgs is the data about to be visible to all subscribers..
            WorkCompleted?.Invoke(this,workerTimeArgs);
        }
    }

    internal class Program
    {
        internal static void Main()
        {
            var worker = new Worker(ReportRandomMessagesWorker);
            // += event_handler subscribes to an event. 
            // -= event_handler unsubscribes to an event.
            worker.WorkCompleted += Worker_WorkCompleted;
           
            const int timeOutReportWorkerMs = 3000;
            var stopWatch = new Stopwatch();
            var reportWorkerWorking = true;

            worker.DoWork();
            stopWatch.Start();
			
			// A simple loop to make sure the work does not take too long.
            while (reportWorkerWorking)
            {
                if (worker.IsWorkCompleted)
                {
                    reportWorkerWorking = false;
                }

                if (stopWatch.ElapsedMilliseconds > timeOutReportWorkerMs)
                {
                    throw new Exception($"The work could not be complete after {stopWatch.ElapsedMilliseconds}");
                }
            }
        }
		
        // Event handler used to do stuff with the data the other class produced for you.
        private static void Worker_WorkCompleted(object sender, WorkTimeArgs e)
        {
           WriteLine(@"Message Reports finished. Report worker Details below.");
           WriteLine($"Started at: {e.WorkStartedTime}");
           WriteLine($"Ended at: {e.WorkStartedTime}");
           WriteLine($"Completed at: {e.WorkCompletedTime}");
           WriteLine($"Report time span taken to complete: {e.TimeTakenToCompleteWork}");
        }
        
	 // This will be our example "Worker" action.
        private static void ReportRandomMessagesWorker()
        {
            var randomReportMessages = new List<string>();
            var reportsCount = Randomizer.GenerateNumber(minValue: 100, maxValue: 500);

            for (var i = 0; i < reportsCount; i++)
            {
                var reportMessage = Randomizer.GenerateString(minSize: 10, maxSize: 15);
                randomReportMessages.Add(reportMessage);
            }

            foreach (var message in randomReportMessages)
            {
                WriteLine(message);
            }
        }
    }
	
	// For this test, this class is just used to make things easier. I included it in order to allow a working code example.
	internal static class Randomizer
    {
        private static readonly Random Random = new Random();
        private static readonly char[] AllowedChars =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray();

        internal static int GenerateNumber(int minValue, int maxValue)
        {
            return Random.Next(minValue, maxValue);
        }

        internal static int GenerateNumber(int maxValue)
        {
            return Random.Next(maxValue);
        }

        internal static int GenerateNumber()
        {
            return Random.Next();
        }

        internal static string GenerateString(int minSize = 40, int maxSize = 40)
        {
            // Create the string builder with a specific capacity
            var builder = new StringBuilder(GenerateNumber(minSize, maxSize));
            for (var i = 0; i < builder.Capacity; i++)
            {
                builder.Append(AllowedChars[GenerateNumber(AllowedChars.Length - 1)]);
            }
            return builder.ToString();
        }

        internal static Guid GenerateGuid()
        {
            return Guid.NewGuid();
        }
    }
}

This post has been edited by JacobH: 14 March 2016 - 09:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: What is something you had trouble grasping early on?

#2 andrewsw  Icon User is online

  • lashings of ginger beer
  • member icon

Reputation: 6333
  • View blog
  • Posts: 25,534
  • Joined: 12-December 12

Re: What is something you had trouble grasping early on?

Posted 14 March 2016 - 10:13 AM

I'll point out the C# Snippets section as well as worth exploring. Obviously they aren't intended as tutorials, but often there is enough of a description to learn something from a snippet, and one can be a kicking-off point for further investigation.
Was This Post Helpful? 1
  • +
  • -

#3 JacobH  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 176
  • Joined: 07-September 15

Re: What is something you had trouble grasping early on?

Posted 14 March 2016 - 10:36 AM

Indeed that is a nice section with a lot of gems. However, from personal experience I think some people would take some good value and motivation out of being able to copy a text block of code and just having it work. It is an adrenaline rush almost, and while useless by it's self I know for me the examples I did find like I described for a topic I had some issues with, that I really started hammering away with the trial and error on my own.

I feel often people miss-read others who are trying to learn for not actually trying. Often this is true, but I know in my case when I ask/asked dumb questions that typically it is considered a waste of every ones time it was not because I wanted to. It was because I simply did not know how, (and still am trying to get rid of a little of the poor habits one day at a time) to approach learning/becoming skilled at anything in general until very late in my life, for a variety of reasons some in and some out of my control.

In fact, I'd go as far as to say that if things did not go in the very specific way that they did for me by over the past 3-4 years I'd still be hopping from subject to subject giving up after a short time and being the 'toxic' person people hate. When you're unaware of basic education practices and have never experienced becoming pretty good at something, it is very easy to get stuck into an endless loop and even worse people have a lack of understanding of their point of view just every bit as extreme as their lack of understanding of your point of view. I want to try and post at least a little content in a way I feel I would have gave me a chance to keep learning just that much longer.

This post has been edited by JacobH: 14 March 2016 - 11:39 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1