2

I have been playing a lot with WPF applications in C# and there are a lot of things that are not really clear to me, I have been trying to look it up and play around with it to figure it out but without much success since english is my second tongue and I am still not that good with terminology nor with programming...

1: What is "this" in the main class? When I create the new WPF application in XAML I get window and a grid. However, I dislike XAML greatly and like to write code for all the elements and objects that I need so I delete that first grid, make a class, define my grid and to add it I have to write something like

   this.AddChild(myGrid);

which is fine, but if I want to use "this" from my main class in other classes, it becomes a bit complicated to me. So, which UIElement or Object is "this"? How do I define it so it can be used in methods? "this", I suppose refers to the Window that is created at beginning, but what UIElement or Object is that Window?

2: Extended classes?? I have been watching a lot of java tutorials lately, simply to learn more about programming. There, to use the objects from other class you can simply write:

   public class class1 extends class2{}

and everything is perfect, I have found out that I can mimic that same thing in C# WPF unless it's the main class, since main class extends :Window and I guess since it's defined as partial class... Is there a way to "extend" multiple classes or go around this?

Any help on clearing this up would be great :)

Antonio Teh Sumtin
  • 498
  • 4
  • 12
  • 26
  • 6
    You would have to read a book on object-oriented programming, than one on C#, finally one on WPF. Don't try to write a WPF program without this minimal background. – Clemens Jul 30 '13 at 11:18
  • `this` is the current class – Fendy Jul 30 '13 at 11:19
  • 5
    I do agree on getting a book. But I most definitly dont agree on that he/she shouldn't write a program with this knowledge. If you face a problem now, you are bound to look it up and learn as you go. Next to that you should learn from the book.. – Bart Teunissen Jul 30 '13 at 11:20
  • this represents the current class Object – Vishal Jul 30 '13 at 11:22
  • For the part with extending multiple classes: http://stackoverflow.com/questions/3951709/c-extending-from-more-than-1-class – Coral Doe Jul 30 '13 at 11:33
  • It sounds like you did understand WPF a bit wrong. While it is not required to use XAML using it or Blend/Visual Studio Designer is greatly encouraged. As coding everything by hand is probably not the best idea. – Samuel Jul 30 '13 at 11:47
  • 1
    Clemens I appritiate your advice, but I like to code while learning too, simply to try stuff out, just as Bart suggested. Coral Doe thank you, it's valuable information, I appreciate it. @Samuel I like adding Menu bars before starting the application itself simply to choose some options, load some input or create a new file/sheet/document to start working on, also coding my custom buttons. Using XAML in those cases just complicates my work and I personally dislike it :P – Antonio Teh Sumtin Jul 30 '13 at 19:54

4 Answers4

3

You should learn Object Oriented Programming in C#

  1. this means the current instance of the class. So in each class this refers to a different object. this can usually be omitted and just AddChild(myGrid) can be used.
  2. extends (or : in C#) means that the first class (class1) inherits from the second (class2) thus having access to methods and variables that are defined in class2 that are not marked private.
juhan_h
  • 3,965
  • 4
  • 29
  • 35
2

For the part about 'this' and its identity, the Window sits in a hierarchy of classes and can assume the identity of any of its ancestors. For example...

    public MainWindow()
    {
        InitializeComponent();
        var contentControl = this as ContentControl;
        var control = this as Control;
        var frameworkElement = this as FrameworkElement;
        var uiElement = this as UIElement;
        var dependencyObject = this as DependencyObject;
        var dispatcher = this as DispatcherObject;
    }

...all of the assignments in this snippet are legal. Also, there are more exotic assignments such as

var x = this as IInputElement;

The key here is to examine the framework and the various assignments available to each class. As others have pointed out, offline reading is essential to a quick learning curve.

The etymology of 'this' as a keyword in an object oriented context extends back to the late 1970's when it first appeared in an early specification for C++.

Finally, Xaml is one of the most attractive features of WPF for lots of reasons, and if Xaml isn't compatible with your approach, you MIGHT be better off in WinForms or Swing or similar tightly bound framework.

Gayot Fow
  • 8,710
  • 1
  • 35
  • 48
  • Thank you for such a great answer, you only did answer first half of my answer so I can't accept your comment for solution, it's great though! It helped a lot :) – Antonio Teh Sumtin Jul 30 '13 at 19:47
  • 1
    The practice at Stack Overflow is to pose a SINGLE question per topic and I observe and respect it. Sorry that didn't work out for you. Good luck on getting your answer. – Gayot Fow Jul 30 '13 at 20:02
  • I will keep that in mind next time, I was not aware of it. Thank you for everything :) – Antonio Teh Sumtin Jul 31 '13 at 13:40
  • 1
    @AntonioTehSumtin, I'll keep an eye out for any future questions you have and try to post a relevant answer. – Gayot Fow Jul 31 '13 at 15:47
  • That is very nice of you indeed, but... I feel like I am getting too much from you that way, I'm afraid you will be waiting in vain... I don't often ask questions, I try to find stuff out on my own so I don't bother people here too often :D – Antonio Teh Sumtin Aug 01 '13 at 07:07
1

Simply said this is the class you are in.

For an example

class dim
{
    int sum = 0;
    public void num(int sum){
        this.sum = sum; //Places the sum from num to the sum in dim
    }
}
Thomas Andreè Wang
  • 3,379
  • 6
  • 37
  • 53
1

Extending a class is basically termed as Inheritance in Object Oriented Programming. There are several types of inheritance like single,multiple,multi-level,hierarchial,hybrid.But C# and also Java doesn't support inhertance from more than one class, because multiple inheritance creates a lot of ambiguity.

A feature that replaces multiple inheritance is the use of interfaces. Instead of 'extending from a class' we 'implement an interface' using the keyword 'implements'.An interface is just a skeleton class where you declare method signatures and the interface will be implemented in the class where you 'implement the interface'.The important point is you can implement more than one interface

To get an overview about Inheritance and Interfaces,the following link would be helpful:

http://msdn.microsoft.com/en-us/library/ms228387(v=vs.80).aspx

Maria
  • 77
  • 5
  • 13