Page 3 of 3 FirstFirst 123
Results 21 to 29 of 29

Thread: Nooby To C#

  1. #21
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Some More on interface.

    Java's event handling is built on interface, C# has removed that type of event handling and when for even objects and delegates. Which is better? Personally I would say each has it advantages and disadvantages and that is for a completely different thread.

    Anyways it is easy to simulate java's event handling using interfaces. This is how.

    A button on a form fires an event when a user clicks the button on screen. In Java Classes wishing to be notified (subscribed) to the event has to agree to support (implement) a specified interface. The button class will maintain a list of listeners (Classes subscribed to the button press event) and will notify each class in the list when the event occurs. This is a simulation in C#.

    Code:
    using System;
    using System.Collections;
    
    
    	class Button
    	{
    		//the list of classes subscribed to the button's events
    		ArrayList listeners = new ArrayList(); 
    			
    		
    		public void Click() {
    			foreach(ButtonListener b in listeners) {
    				b.ButtonClick();
    			}
    		}
    			
    		//Method to subscribe to the button's events
    		public void AddButtonEventListener(ButtonListener b) {
    			listeners.Add(b);
    		}
    		
    		//Metnod to unsubscribe from the button's event
    		public void RemoveButtonListener(ButtonListener b) {
    			int indx = listeners.IndexOf(b);
    			if(indx != -1) {
    				listeners.Remove(b);
    			}
    		}
    		
    		
    		static public int Main(string[] args)
    		{
    			Button b = new Button();
    			b.AddButtonEventListener( new ButtonEventHandler() );
    			b.Click();
    			return 0;
    		}
    	}
    	
    
    	interface ButtonListener {
    		void ButtonClick();
    	}
    	
    	
    	class ButtonEventHandler : ButtonListener {
    		
    		public ButtonEventHandler() { }
    		
    		public void ButtonClick() {
    			Console.WriteLine("The button was clicked");
    		}
    	}
    Before any class can receive events on the button being clicked it has to support the ButtonListener interface. The button provides two methods for registering and unregistering Event handlers, the AddButtonEventListener(ButtonListener b) methods only accepts ButtonListeners, which means the class must implement the interface before it can be registered. When the button is clicked the button will notify each registered class by calling the ButtonClick(); method on the interface.

    This is a very simple illustration event handling usually gets more complicated than this. normally the methods for registering and unregistering are synchronized. This is the very same method used in warning systems to trigger a series of events when a certain critical situation occurs.

    As for game programming I not sure how much interfaces are really used but im sure if i get into it I could find many uses for it.

  2. #22
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Forget about game programming just now.

    Just to confirm my understanding of the code:

    Class button has a list of listeners.

    However, listeners list is actually a list of different objects' interfaces 'to listen'.

    This list is populated by taking the interfaces of other objects. Objects can be from any class that supports the listen interface.

    Finally, on the click event of the button class, all interfaces of the added objects are used to execute the methods in the interface (or class).

    Therefore, my layman term for interface is a structure which contains function pointers, and can be mapped to different classes' methods, if the class has the interface. This is not be technically correct but behaviourwise it will help me to remember interfaces.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  3. #23
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Quote Originally Posted by crosswire
    Therefore, my layman term for interface is a structure which contains function pointers, and can be mapped to different classes' methods, if the class has the interface. This is not be technically correct but behaviourwise it will help me to remember interfaces.
    yes if u would like to think about it like that. The name you are looking for is dynamic binding, I'm sure you have heard the term before.

  4. #24
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    The name you are looking for is dynamic binding, I'm sure you have heard the term before.
    No sirre.

    Did the google on it.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  5. #25
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Created my first web application today, Yeah.

    All it did was provide a button, that opens a file when you click it

    It was easy with VC# in VSNet2002, all I did was:

    Open -> New C# proj -> web app proj -> save to web directory -> htp:/Mycomputer/WebApp1
    In html view, drap a button from the tool box to the web page.
    Double click the button, then edit the on button click function.
    Add the System.IO namespace at the top of the C# file
    Add the code that open the file and build and run
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  6. #26
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Quote Originally Posted by crosswire
    Found this great link on C# and .Net
    http://samples.gotdotnet.com/quickstart/howto/
    I know this was mentioned before on some other post, but I recommend this site for real lazy beginners. There are short samples on any topic.

    I use this as well as other ebooks for theoretical reference
    There is a way to load this site onto your computer if you use VSNet, I think, I have not tried it, nor will try it

    Go to Start Menu -> Program Files -> Microsoft .NET Framework SDK -> Samples and QuickStart Tutorials

    You will have to read the instructions there but in the end you will get to go to samples on the local computer
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  7. #27
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    uhmm, those samples are installed along with asp.net.

  8. #28
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Did my second windows controls today, feel good but this is strictly newby stuff.

    I used mostly the properties window and toolbox in VSNet. The dev program is alot better than I thought.

    This is what I did:

    Created a translucent windows form with blue background, added a button and some check boxes, added events namely mouse events for the controls such as mouseover and mousedown, and add a text box.


    After creating a new C# (or VB) project -> windows application

    In design view of the window, use the tool box to add these windows control to the win form by dragging them of the toolbox.

    Then with the properties pane click on the window form and set the properties of the win form like colour and caption, also use the properties pane to set events like mousedown, doubleclick mousedown on the evnt section of the toolbox, there are various events on this pane, then edit the event handler function that is automatically created.

    eg
    Code:
    	//This is call when the button is clicked
    	private void button1_Click(object sender, System.EventArgs e)
    	{
    		MessageBox.Show("Boop");
    	}
    
    	//... when the mouse right clicks the win form and not the controls
    	private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    	{
    		MessageBox.Show("The mouse is down");
    	}
    
    	//... when the mouse moves over the datagrid control
    	private void dataGrid1_MouseHover(object sender, System.EventArgs e)
    	{
    		//'this' refers to the winform and 'text' is its caption
    		this.Text	=	("hovered me");
    	}
    
    	private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    	{
    		//the event args hold the position of the mouse pointer, X and Y, relative to the control of form that it is called on
    		MessageBox.Show("mouse x pos " + e.X + "mouse x pos " + e.Y);
    	}
    
    	//... When the text in the text box control changes
    	private void textBox1_TextChanged(object sender, System.EventArgs e)
    	{
    		//'this' refers to the win form
    		//'textBox1' refers to the text box control on the win form
    		//'this.textBox1.Text' reform to the text in the text box
    		MessageBox.Show(this.textBox1.Text);
    	}
    About 90% of the code was auto-generated; each function head was generated; after typing every dot (.) a listbox appear with options of what to type next, and it is easy to use the toolbox and properties. Hope to use datagrid control soon and the other form controls.

    There is also a data section in the tool box, with stuff dealing with sqlcomnands and adapters, and there is a component section with stuff like timers and messagequeue. I will try to learn those in time too.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  9. #29
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    interface is like a class with only pure virtual functions ( from c++ )... except you dont need to make the function virtual (explicitly).

    you create it just like a class. it has methods like a class but no member data. you can inherit from it like a class.

    it forces all derived types to have certain methods available.
    you however have to define these methods in the derived class since the interface only defines the prototypes and not the actual definition.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •