Tech Off Thread

14 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

abstract members in a Base Class? how?

Back to Forum: Tech Off
  • User profile image
    Jeff Spicolli

    How do I define an abstract method in a Base Class that must be implemented in concrete representations of the class?

    The rest of the class should not be abstract, just the method.

    I know I have done this before in VB.NET, but the runtime is giving me grief when I try to implement this in C# 2.0.

    thnx

  • User profile image
    Sven Groot

    public abstract class Foo
    {
       public abstract void Bar();

       // Other members, need not be abstract
    }

    This isn't working for you?

  • User profile image
    Jeff Spicolli

    Sven Groot wrote:
    

    public abstract class Foo
    {
       public abstract void Bar();

       // Other members, need not be abstract
    }

    This isn't working for you?



    No, it is. I just had a momentary brain freeze.

    My base class wasn't marked as abstract.

    So my solution was to mark my base class as abstract, declare my abstract method, inherit that base class into a general Class, and then inherit the general class for all my concrete representations.

    thnx.

  • User profile image
    Jeff Spicolli

    As long as I've got you here Sven, perhaps Ican exploit your knowledge in my favor?

    Can you tell me why this piece of code fires more than 20 times each time the control is loaded?

    private void lblTitle_Paint(object sender, PaintEventArgs e)
    {
       this.lblTitle.Text = Title;
    }

  • User profile image
    Sven Groot

    I'm surprised that ever stops firing at all. Smiley

    The Paint event is fired when the control needs to be redrawn. Setting the Text property is one of the things that will cause the control to be redrawn. So the control gets drawn the first time, firing the Paint event, then you set the text, which causes it to be redrawn, firing the Paint event, then you set the text, which causes it to be redrawn, firing the Paint event, then you set the text, which causes it to be redrawn, firing the Paint event, then you set the text...

    Better set the Text property somewhere else.

  • User profile image
    Jeff Spicolli

    Wow, thanks. Big Smile

  • User profile image
    eddwo

    Is there a timeline for the events/methods that get called on a control? I'm trying to write one that wil take some time to initialise, I wanted to move the slow running code out of the controls constructor and into an event/method that occurs once the rest of the form is fully loaded.

    I can't seem to find any reference on what events to expect.

  • User profile image
    Human​Compiler

    Is it work that's done on the UI thread or is it just background stuff being processed?  If the later, you could just spin up another thread to do that work in Load or wherever so it wouldn't hold up the drawing of the form.  If it's doing UI work, then you'll probably have to do some other tricks like put it in the Activate event (sorry, it's been a long time since I've done Winforms work, sorry if this is totally wrong...going from memory) and check a variable so it only happens once and use Application.DoEvents() in between the work being done...blah...it depends on what you're trying to do.

  • User profile image
    MartinNL

    What about?

    private void lblTitle_Paint(object sender, PaintEventArgs e)
    {
       if(this.lblTitle.Text == Title)
          this.lblTitle.Text = Title;
    }

  • User profile image
    Sven Groot

    MartinNL wrote:
    What about?

    private void lblTitle_Paint(object sender, PaintEventArgs e)
    {
       if(this.lblTitle.Text == Title)
          this.lblTitle.Text = Title;
    }


    Assuming you meant != and not ==, it would solve the problem, but I still don't think it's a good idea. It's abusing the Paint event for something it isn't meant for. Only actual painting code should go there (code that needs to use the PaintEventArgs.Graphics property), anything else should not go in the Paint event.

    From the looks of it, the Load event would be a better location for this assignment.

  • User profile image
    TommyCarlier

    Assuming that Title is a property, wouldn't it be better to put the assignment in the setter of the Title property?

  • User profile image
    littleguru

    public class Foo
    {
        public Foo()
        {
            this.Text = text;
        }
    }

    Why is nobody using the constructor? Setting it in the paint event makes the whole thing slow. Each time the control is repainted the check with the two strings is done (= slow).

    If you'd like to have a public property Title, why aren't you doing this:

    public string Title

        get { return _title; }
        set
       {
            bool changed = (_title != value);
            _title = value; 
            if (changed)
                Invalidate(); // Force a redraw.
       }
    }

  • User profile image
    ElucidWeb

    Why do you want to put a value in the control during the paint event?  That makes no sense to me at all and I cannot think of a single scenario that I would do that in, in fact I would go as far as to say that I would NEVER set a value inside a pain event.  In fact IMO doing anything in the paint event unless you are creating a custom control should be avoided.

  • User profile image
    Jeff Spicolli

    ElucidWeb wrote:
    

    Why do you want to put a value in the control during the paint event?  That makes no sense to me at all and I cannot think of a single scenario that I would do that in, in fact I would go as far as to say that I would NEVER set a value inside a pain event.  In fact IMO doing anything in the paint event unless you are creating a custom control should be avoided.



    Mainly because I am ignorant.
    It is a custom control, that gets manufactured by a controlFactory.

    I have been a middleware C++/Java/VB6 developer for 7 years, this is my first C#/WinForms project, so I am not familiar with the event model intracies of GUI design/development.

    Thanks for being so positive.
    Expressionless

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.