• (3 Pages)
  • +
  • 1
  • 2
  • 3

C# Learning Series - Properties

#16 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Posted 07 April 2013 - 04:43 PM

View PostacProgrammer, on 07 April 2013 - 04:58 PM, said:

I am taking a college course - I am able to throw basic programs together pretty quick - or at least I am getting quicker


Hate to burst your bubble - but no. If this stuff is a mystery, then you're not really programming. Dragging a button to a form, then double-clicking it to make your event handler is not programming. But i admire that you are wanting to learn at a pace faster than (glacial pace) of the course.
Was This Post Helpful? 1
  • +
  • -

#17 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Posted 07 April 2013 - 04:48 PM

View PostacProgrammer, on 07 April 2013 - 05:17 PM, said:

So - I am still confused cuz I don't even see the event anywhere else - how would the code that referenced this event look & what is the purpose of this code?


The event is not in the arguments. When you drag-n-drop a button to a form, then double-click it, Visual Studio creates all your handler code for you. Look at the second argument in the signature: Its an instance of the EventArgs class. This instance is called e by default.

Specific event arguments contain specific information. MouseMoveEventArgs contains the x and y position as well as the delta from the previous position, for example.

In this case our TextArgs only contain a string called Message. It lets us raise an event that contains a message. We can raise and event where the message is "Learning is fun."
Was This Post Helpful? 1
  • +
  • -

#18 acProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 23-March 13

Posted 08 April 2013 - 07:21 PM

Just curious - We just took the midterm about a week or two ago...is this the normal pace or do most classes start without the buttons? just curious & guessing I'm not gonna like the answer but appreciate the honesty.
Was This Post Helpful? 0
  • +
  • -

#19 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Posted 08 April 2013 - 07:38 PM

What is the norm for anything? Maybe there is a norm for making donuts. But even then Krispy Kreme is so much better than most.

The norm as most of us would probably agree is for the university course to hold you back as much as possible. Remember they aren't in the business of educating you or getting you ready for work. They are in the business of teaching courses. They sell credit hours for a living. The more they can stretch out your education the more money they make. If you were able to be job ready in one year their income would be cut to 25%.

Following the course gets you a certificate. Following a few teach yourself C# books and some on-line tutorials will get you some skills and theoretical understanding. THEN, start building the hell out of everything you can think of. Weather station controllers, DVD library programs, anything and everything you can think of that you always wished someone would write, because now you *are* that someone. Building is the only way you will every actually gain comprehension of the engineering of software. But you need to work through those books and courses first so you know at least some of the tools in your toolbox.
Was This Post Helpful? 2
  • +
  • -

#20 acProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 23-March 13

Posted 08 April 2013 - 07:45 PM

perspective & logic both appreciated - thanks and will do :)
Was This Post Helpful? 0
  • +
  • -

#21 AndrewMack  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 45
  • Joined: 12-April 13

Posted 18 April 2013 - 11:12 AM

This is phenomenal - thank you very much for this, tlhIn`toq!

I'm in the process of relearning the basics of C# and, as a result, OOP. I develop with WinDev at work which is geared towards procedural programming so I feel like quite the beginner again.

I'm learning by reading books/tutorials and following examples - DiC seems to be an incredible resource! I've got a project in mind that I'd like to kind of build towards but I know I'm a ways off still before I think I will do a good job on it.


Anyways - thanks! I really appreciate you taking the time and effort to do what you and the rest of the leaders here do!
Was This Post Helpful? 1
  • +
  • -

#22 LuigiMan  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 1
  • Joined: 01-May 13

Posted 01 May 2013 - 09:15 AM

Just came across this tutorial (nice one, by the way) and had to make a small contribution.

I think you can add a final section on your tutorial that takes advantage of a new functionality of .NET 4.5. The functionality is

private void RaisePropertyChanged([CallerMemberName]string prop = "")
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(prop));
}


As you can see, you have that CallerMemberName in the parameter. This allows for you to call the RaisePropertyChanged method without the string name in the argument place, which avoids small bugs to appear to the client like having the wrong property name passed.

private int age;
public int Age
{
    get { return age; }
    set
    {
        if (age != value)
        {
            age = value;
            RaisePropertyChanged();
        }
    }
}


Example retrieved from here.
Was This Post Helpful? 3
  • +
  • -

#23 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Posted 01 May 2013 - 09:57 AM

Absolutely agree. As you have just pointed it out it *is* now part of the tutorial, as we can't edit them after x amount of time.

It is worth noting for those maintaining backward compatibility, this is a .NET 4.5 feature. So it isn't going to be available for those still supporting WinXP for example.
Was This Post Helpful? 0
  • +
  • -

#24 RaminK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-September 13

Posted 21 September 2013 - 12:49 PM

after all this time i put today on my little game i was making, i fainally realized that i did it all wrong :/ but i still am not sure how to handle events within classes, or how this info will apply to my situation, that has always been my problem with programming tutorials, i always have lots of questions on how i can implement this on my program, but no one to ask those questions to :/ but thanks for the tutorial :)
Was This Post Helpful? 0
  • +
  • -

#25 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Posted 21 September 2013 - 04:55 PM

RaminK: I have no idea what your situation is. You're talking like we all know what you're working on. With 500,000 members and a couple hundred posts a day, I don't try to remember what everyone is doing on some other thread in some other area.

Quote

but i still am not sure how to handle events within classes,

This is a tutorial on Properties, not events. But I did write a tutorial or two for those as well. They are linked in my signature block.

Whenever I hear "I don't know how to apply xyz to the program I'm writing." it is almost the same answer: Stop trying to write a program.

The simple fact is that you shouldn't be trying to architect and code a full program yet if you are still learning what properties and events are. You should just be working through your text book or other self-teaching book (IE: "Learn C# in 30 days") from cover to cover and not trying to build anything from scratch.

There are several good tutorials linked in my sigture block: They link to the entire C# learning series. Plus my blog has a lot of good articles on how to plan, plan and plan some more before the first keystroke. Trust the senior staff here when they say 50 hours spent on tutorials will save you 5,000 hours of heartache and pulling your hair out.

Basically you're trying to write an opera while you are still learning the scales and how to read sheet music. It just doesn't work.

Or another example would be that you are just starting to learn architecture and think you're going to learn more by building a skyscaper as your first project. It just doesn't work. You have to LEARN before you can DESIGN. Then you design a dog house, then an out house, then a garage, then a cottage, then a single story house [...]

The same goes for programs. Just because its all virtual doesn't make it any less complex or require an less learning of foundation principle before you can know what tools you have at your disposal, how to architect the overall design taking into account the requirements of all the fixtures (GUI elements) and plumbing (events).
Was This Post Helpful? 2
  • +
  • -

#26 RaminK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-September 13

Posted 21 September 2013 - 05:51 PM

thank you for this, ive treid and stoped programming numerous times in the past 2 years, on various different platforms (C++ and Java Mainly) but i never got past making simple programs like a number guessing game. recently as i am in gr 10, i was introduced to VB in my computer science class. so since i had some background, i shot ahead of the class instantly, since everyhting wwas way to easy, like the first week was based on things like "hello world". so i started making a more advanced and complex number guessing game, involving money, and so on, but i soon realized it wasnt the right way to code in OOP, i had no knowledge on how to use classes or objects,although i knew i was supposed to use them. either way, i wont be able to find too much time to set aside for learning a whole new language in the middle of my studies, although, im doing extreamly well anyways. in the end i guess i needed to hear this out befor i got into a real mess. ill try and find some tutorials, or use the ones you mentioned to get a better grasp at things. thank you again

This post has been edited by tlhIn`toq: 21 September 2013 - 06:40 PM
Reason for edit:: No need to quote previous posts in their entirety: We all see it

Was This Post Helpful? 0
  • +
  • -

#27 neo112  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 24
  • Joined: 23-November 12

Posted 06 October 2013 - 12:58 AM

Exceptional tutorial and very good insights for starting programmers. Thank you so much!
Was This Post Helpful? 0
  • +
  • -

#28 nighttrain  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 354
  • Joined: 22-September 10

Posted 13 March 2014 - 01:38 AM

Very good tutorial - I have one question if I would like to implement property inside base class for all derived once how this should be implemented then assuming I don't know from beginning that either all or not all classes woud like to control the value or just use it. Could you give example please?
Was This Post Helpful? 0
  • +
  • -

#29 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Posted 13 March 2014 - 05:14 AM

I'm afraid I'm not following about half of what you're asking. Maybe its lost in translations into English.

I did write another tutorial on classes if that helps. Its linked in my signature block.
Was This Post Helpful? 0
  • +
  • -

#30 Gergan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-April 14

Posted 25 April 2014 - 02:09 PM

Hey,

before I start to write, I would like to apologize because of my English skills, my English is not so great, but I hope that you'll understand me. I am C# begginer and I'm trying to learn as much as possible,Before I ask you my question I want to thank you so much because you'r writing so nice tutorials, they'r really great.

Well, my question is next:
I've red about properties, but I'm confused ..
I am testing a lot seriously, trying this, trying that etc.. and I found lost myself, my code is here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class Program
    {
        public struct MutableStruct
        {
            public int Value { get; set; }

            public void SetValue(int newValue)
            {
                Value = newValue;
            }
        }

        public class MutableStructHolder
        {
            
            public MutableStruct Field;
            public  MutableStruct Property { get; set; }
        }

        static void Main(string[] args)
        {
            MutableStructHolder holder = new MutableStructHolder();
            // Affects the value of holder.Field
            holder.Field.SetValue(10);
            // Retrieves holder.Property as a copy and changes the copy
            holder.Property.SetValue(10);

            Console.WriteLine("Field:");
            Console.WriteLine(holder.Field.Value);
            Console.WriteLine("Property");
            Console.WriteLine(holder.Property.Value);
        }
        
}

}





Results I get is: value of field is 10, and value of property is 0.. but I dont understand why,
something is about that {get;set;}

I red about getters and setters, I know that they'r used mostly because of encapsulation of behavior associated with getting or setting the property - this allows additional functionality (like validation) to be added more easily later..
different access levels - for example the get may be public, but the set could be protected... and many more..

But in this example I am confused, I learned about them today, maybe I didn't gave enought time to myself to understand them, I don't know .. All I know is that I am confused.

So please could anyone answer me , why do I get these results : 10 and 0..and why not 10 and 10..?

Thank you very much!
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3