Results 1 to 11 of 11

Thread: MyBase equivalent in C#? and others?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    MyBase equivalent in C#? and others?

    well I get a better response in the Vb.net section hence I posted this here. It's a question that the vb people shuold answer anyways

    so what is MyBase in C#?I wanna use MyBase.New...
    Another q, what's MyClass in C#?

    also, if I wanna call an overloaded new constructor from another constructor, how do I do it? (ie, new (blah, blah) )
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    The MyBase equivalent in C# is base.

    You would do constructor chaining like this...
    Code:
    	public class Test
    	{
    		public Test(): this(1)
    		{
    			//...
    		}
    
    		public Test(int x)
    		{
    			//...
    		}
    	}
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by crptcblade
    The MyBase equivalent in C# is base.

    You would do constructor chaining like this...
    Code:
    	public class Test
    	{
    		public Test(): this(1)
    		{
    			//...
    		}
    
    		public Test(int x)
    		{
    			//...
    		}
    	}
    hmm thanks... what is that : this(1) doing in front of the constructor ?
    yeah apparently base "acts" differently from MyBase of VB. You can use MyBase.NEW in vb anyways here's how I'm calling the constructor withing another constructor and it apparently works. It this the right way though?:
    Public Test()
    {
    new Text (3);
    }


    edit:hmm I'm gussing this(1) is calling the constructor, uuh but it's weird because when I do it the stupid Intellisense doesnt show up, but when I use new() it does show up
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by MrPolite
    It this the right way though?:
    Public Test()
    {
    new Text (3);
    }
    That doesn't work for me. Try it...
    Code:
    	/* ... Form code... */
    	{
    
    		private void Form1_Load(object sender, System.EventArgs e)
    		{
    			Test t = new Test();
    
    			MessageBox.Show(t.X.ToString());
    		}
    	}
    
    	public class Test
    	{
    		private int _x;
    
    		public int X
    		{
    			get { return _x; }
    		}
    
    		public Test()
    		{
    			new Test(1);
    		}
    
    		public Test(int x)
    		{
    			_x = x;
    		}
    	}
    Instead of returning 1 like you would think, it returns 0. That's because

    public Test()
    {
    new Test(1);
    }

    simple creates a new Test object and it simpley goes away.

    The first way is the correct way.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by crptcblade
    That doesn't work for me. Try it...
    Code:
    	/* ... Form code... */
    	{
    
    		private void Form1_Load(object sender, System.EventArgs e)
    		{
    			Test t = new Test();
    
    			MessageBox.Show(t.X.ToString());
    		}
    	}
    
    	public class Test
    	{
    		private int _x;
    
    		public int X
    		{
    			get { return _x; }
    		}
    
    		public Test()
    		{
    			new Test(1);
    		}
    
    		public Test(int x)
    		{
    			_x = x;
    		}
    	}
    Instead of returning 1 like you would think, it returns 0. That's because

    public Test()
    {
    new Test(1);
    }

    simple creates a new Test object and it simpley goes away.

    The first way is the correct way.
    I look funny trying to program in C/C++ style hehe thank you
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    hmm so one more question. Does the constructor chaining occure AFTER you are done with the current constructor?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Code:
    		public Test() : this(1)
    		{
    			System.Diagnostics.Debug.WriteLine("1");
    		}
    
    		public Test(int x)
    		{
    			System.Diagnostics.Debug.WriteLine("2");
    			_x = x;	
    		}
    Results in :

    2
    1

    So the constructor that is called from the constructor executes first, then the original constructor's code is executed.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    hmm interesting. So I'm puzzled, what if I want to do something like this!

    public Test()
    {
    int someNum;
    // compute someNum ....

    // NOW call the other constructor with the someNum value
    // Test (someNum)
    }
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  9. #9

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    ok apparently what I asked in the last post isnt possible. But is this possible:
    public Test():base (99999)
    {}

    I want to call this (1) after calling the base constructor. How would I do that?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  10. #10
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    You can't make two constructor calls. You either deall with the call, pass the call to another overload of your constructor, or pass the call to the base class.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  11. #11

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    aah hehe vb seems to be more flexible in this case... but maybe that's just a bad way of coding
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

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



Click Here to Expand Forum to Full Width