Jump to content
Willicious

E2137 Method not used in base class

Recommended Posts

I'm getting the E2137 error when trying to add a new procedure. Google result says it's usually to do with misspelling, but I used Ctrl + Shift + C to auto-create the code block (and double-checked the spelling), so that isn't the problem.

 

Also, I would've thought that using Ctrl + Shift + C to auto-create the code block would put the block in the correct place in the unit, but apparently not. What other reasons could there be for this error?

 

I can't copy out the entire code because the unit is huge (more than 2000 lines), but I'll try to just paste the relevant parts here:

 

unit GameBaseSkillPanel;

interface

uses
	...

type
	...

  private
	...
  
  protected
	...

  public
    procedure PanelResize; override;

  end;

procedure TBaseSkillPanel.PanelResize;
begin
  inherited;
  Width := GameParams.MainForm.ClientWidth;
  Height := GameParams.MainForm.ClientHeight;

  ClientWidth := GameParams.MainForm.ClientWidth;
  ClientHeight := GameParams.MainForm.ClientHeight;

  fImage.Width := GameParams.MainForm.ClientWidth;
  fImage.Height := PanelHeight;
  fImage.Left := (Width - Image.Width) div 2;

  fMinimapImage.Width := MinimapWidth;
  fMinimapImage.Height := MinimapHeight;
  fMinimapImage.Left := MinimapRect.Left;
  fMinimapImage.Top := MinimapRect.Top;
end;

 

Share this post


Link to post

The interesting part would be the declaration of PanelResize in the base class.

Share this post


Link to post
5 hours ago, Willicious said:

I'm getting the E2137 error when trying to add a new procedure.

This error happens when you add override directive to the method and method with same name does not exist in the class hierarchy from which you are inheriting. If you just wanted to add new method which does not exists, then remove override.

 

The "check spelling" suggestion comes from the most common mistake when overriding existing method is that you mistype the name, so the name does not match to the name of existing method in base class which you want to override.

Edited by Dalija Prasnikar

Share this post


Link to post
On 3/23/2023 at 10:29 AM, Dalija Prasnikar said:

This error happens when you add override directive to the method and method with same name does not exist in the class hierarchy from which you are inheriting. If you just wanted to add new method which does not exists, then remove override.

Yes, this does remove the error, thanks 🙂

 

So, what exactly does override do? From what I understand, it applies changes to procedures/functions with the same name that also exist in another unit/class. If override is needed here, then how can I prevent the error in that case?

 

On 3/23/2023 at 1:50 PM, programmerdelphi2k said:

image.thumb.png.e2b9eb4bc6b8ed0af644cd9fc70210b3.png

Yes, I too found this Google result (as I stated in the OP). However, it doesn't resolve the issue because I haven't misspelled anything.

 

Apologies if I seem to be asking basic questions, I'm very new to both Delphi and programming in general. Being able to ask you guys questions is very helpful, so thank you.

Share this post


Link to post
6 hours ago, Willicious said:

So, what exactly does override do? From what I understand, it applies changes to procedures/functions with the same name that also exist in another unit/class. If override is needed here, then how can I prevent the error in that case?

Override is used when you want to override virtual or dynamic method from the base class and change its behavior https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Methods_(Delphi)#Virtual_and_Dynamic_Methods

Share this post


Link to post
On 3/23/2023 at 5:33 AM, Willicious said:

I'm getting the E2137 error when trying to add a new procedure. Google result says it's usually to do with misspelling, but I used Ctrl + Shift + C to auto-create the code block (and double-checked the spelling), so that isn't the problem. Also, I would've thought that using Ctrl + Shift + C to auto-create the code block would put the block in the correct place in the unit, but apparently not. What other reasons could there be for this error?

I can't copy out the entire code because the unit is huge (more than 2000 lines), but I'll try to just paste the relevant parts here:

 


procedure TBaseSkillPanel.PanelResize;
begin
  inherited;   <----------------------------
  Width := GameParams.MainForm.ClientWidth;
  Height := GameParams.MainForm.ClientHeight;
  .....
end;

 

Also "inherited" should be removed, as the method is not a "redeclaration" of an existing method.

Share this post


Link to post
12 hours ago, Willicious said:

Yes, this does remove the error, thanks 🙂

 

So, what exactly does override do? From what I understand, it applies changes to procedures/functions with the same name that also exist in another unit/class. If override is needed here, then how can I prevent the error in that case?

 

Yes, I too found this Google result (as I stated in the OP). However, it doesn't resolve the issue because I haven't misspelled anything.

 

Apologies if I seem to be asking basic questions, I'm very new to both Delphi and programming in general. Being able to ask you guys questions is very helpful, so thank you.

Object-Oriented Programming involves three distinct things: Polymorphism, Inheritance, and Encapsulation.

 

It seems you are not grounded in these yet. (Don't feel bad, none of us knew how to walk or talk when we were born, either. There's stuff in life that just needs to be learned. 🙂 )

 

You didn't show the declaration for the TBaseSkillPanel class, so it's unclear if you're inheriting from something else or not. I could guess it's several things, but I'll not go there.

 

It's either declared like this:  TBaseSkillPanel = class

 

or like this: TBaseSkillPanel = class( TForm )  -- or something other than TForm

 

The first one is a standalone class (which in Delphi's world is really implied as saying = class(TObject) ), while the second one is derived from some other class.

 

This is what inheritance refers to -- you're extending the base class by adding more stuff to it and giving that new combined class a different name

 

If you're inheriting from TForm or some other subclass that defines PanelResize, then it needs to have the virtual or dynamic attribute on its declaration to allow you to then say override but it's not required.

 

In this case, the error suggests that there either is NOT a PanelResize method in the base class, or it is not tagged with a virtual or dynamic attribute, meaning the override is misplaced -- in fact, it doesn't belong there.

 

The flip side of this would be where the base class DOES specify virtual and the derived class does NOT specify override on a method with the same name and signature. In this case, the compiler would warn you that the "method in the derived class hides the method of the same name in the base class". Sometimes you want that, in which case instead of override, you need to specify reintroduce to avoid the warning. (This is quite common when you want to use the same method name in the derived class but change its signature, usually by adding more parameters. It's most commonly encountered on constructor Create... methods.)

 

I suspect you think that adding methods in a derived class requires you to include the override attribute. It's only needed if there's a method in the base class with the same name and signature that has virtual or dynamic specified. That's not correct thinking.

 

Also, the use of virtual or dynamic (although dynamic is very rarely used) signifies the possibiity of polymorphism, which is an aspect of inheritance that gives you some rather magical abilities. 

 

I suggest you learn about these three topics; they'll help you understand and organize your code much better. They are fundamental to ALL aspects of object-oriented programming, which Delphi embraces quite extensively.

 

 

 

Edited by David Schwartz

Share this post


Link to post

@David Schwartz Thanks for your reply. I'd love to get into programming more and gain a better understanding of object-oriented.

 

I've been thinking I probably need a tutor. My thinking tends to be more abstract/ideas-oriented than logic-oriented, and this trips me up a lot when it comes to programming. I know exactly what I want to happen, but the "how/why" of it holds me back. Very frustrating.

Share this post


Link to post
4 hours ago, Willicious said:

I've been thinking I probably need a tutor. My thinking tends to be more abstract/ideas-oriented than logic-oriented, and this trips me up a lot when it comes to programming. I know exactly what I want to happen, but the "how/why" of it holds me back. Very frustrating.

How long have you been programming? Back when C++ was introduced, the general rule-of-thumb was that for people who had been doing regular procedural programming for a while, it would take about 6 months to really grok what OOP was about. I had to just keep writing code and writing more code until after 5 months or so, one day something in my head went "ping!" and all of a sudden I "got it". I've heard similar atuff from a lot of others as well.

 

Since the mid-90's or so, OOP has been taught in college courses right from the beginning, so there are people who never knew anything different. Once I'd learned OOP, working with a non-OOP language has been hard. There are still people who write php and refuse to use any of the class-related language features, and their code is ugly as hell, exactly what you'd expect without leveraging language features to at least manage encapsulation.

 

It would be nice if they'd teach Delphi's Object Pascal in colleges today, because it IS fully object-oriented, and it IS easy to learn and code with.

 

I don't think you need a tutor, you just need to keep writing code. Try translating some old C libraries into Delphi libs using classes. Or even old Pascal libraries. Part of what I did was took some C libraries I was using and turned them into OOP-based libs. I kept writing these huge, complicated classes and would inevitably code myself into a corner where I had nowhere to go, no way to access a piece of data I needed, and just couldn't figure out why? When the light bulb went off, I deleted all of that code and started over from scratch, and it ended up seemig really simple.

 

Somebody tried explaining it to me once before my brain had adjusted to working with OOP yet, so what they said went in one ear and out the other.

 

Just keep coding

just keep coding

code code code code

code code code code

 

One day you too will have the "OOP ephinany" and it will suddenly make perfect sense!

Share this post


Link to post
8 hours ago, David Schwartz said:

Somebody tried explaining it to me once before my brain had adjusted to working with OOP yet, so what they said went in one ear and out the other.

A musician friend of mine said of learning to play guitar, "play until your fingers bleed, and then play some more." I think it's good advice for anything you really want to learn to do well in life. 

  • Like 1

Share this post


Link to post

Yes, good advice. I can play piano and guitar (both of which took years to learn and master), so - in theory - a bit of programming shouldn't be too far beyond me. I am learning well, but some things just don't have answers without already knowing the codebase you're working with. Starting from scratch is so tempting, but the project I'm working on is already huge, it would likely take way longer to start it from scratch than it would to wrestle with the few features I'm trying to reverse-implement.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×