Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6591593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:26:40+00:00 2026-05-25T17:26:40+00:00

I am not sure if I am doing this right. I have a list

  • 0

I am not sure if I am doing this right. I have a list of objects in the listbox and need to use IndexOf to get an object’s index in the list.

if AlarmListBox.items.indexOf(alrm.Tagname) = -1 then

alrm is an object of TAlarm class.

Based on a StackOverflow C# question (How Can I Get the Index of An Item in a ListBox?), I try to override GetHashCode and Equals method, but still it doesn’t work right.

Overriden Method:

TAlarm = class(System.Object)
  TagName:string;
  private
  protected
  public
  method Equals(obj:System.Object):Boolean; override;
  method GetHashCode:Int32; Override;
end;

method TAlarm.Equals(obj: system.Object):Boolean;
begin
    result := TAlarm(obj).Tagname.Equals(self.Tagname);
end;

method TAlarm.GetHashCode:Int32;
begin
    result := self.GetHashCode;
end;

This is how I populate AlarmListBox:

AlmGrp:= new TAlarmGroup;
AlarmListBox.items.Add(AlmGrp);

Compiler compiles without any errors, but when I debug the program line by line it always returns -1 and these overridden methods are never called or fired.

Am I implementing these overrides correctly? If not, how should I override them?

Sample code or hints or clues will be appreciated. Thanks,

UPDATE: To David Heffernan and others who have commented or answered, I think the problem might be that I am passing in two different object as Rob’s last comment states. I do populate Listbox (UI) with TAlarmGroup but pass in TAlarm into IndexOf, although they both are identical classes. This is probably my problem. What I am really trying to do is populate Listbox with TAlarmGroup objects and through listbox.indexof by passing in the string (Tagname) I search for the object location. That’s how it is done on Delphi XE it works great. The code above is not the actual code. Once I clean up the confusion in my code, it will probably work without overriding the GetHashcode and Equals method.

UPDATE: I think, I have stumbled onto something here. On Delphi XE or below, ListBox (UI) provides a method called AddObject. It’s parameters are a string and an object respectively. So, when I populated objects into listbox I also provided the string to go along with it. When I searched I passed in a string or the alarm group name. IndexOf searched on this string against the string it had for each object I provided and not against the object’s field (TagName). In Delphi Prism, listbox doesn’t have a similar method as AddObject method but only Add that only accepts object as a parameter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:26:41+00:00Added an answer on May 25, 2026 at 5:26 pm

    Here’s an example of doing what you want with the base TAlarm class you provided. I’ve also provided implementations of the overloaded Equals and GetHashCode that seem to work. (Again, I’m not a Prism/.NET developer; just trying to help out here.)

    // In AlarmClass.pas
    type
      TAlarm = class(System.Object)
        TagName:string;
      private
      protected
      public
        constructor;
        method Equals(obj:System.Object): Boolean; override;
        method GetHashCode:Int32; Override;
        method ToString(): String; override;
    end;
    
    implementation
    
    method TAlarm.GetHashCode: Int32;
    begin
      if Self = nil then
        Result := inherited 
      else
        Result := Self.TagName.GetHashCode;
    end;
    
    constructor TAlarm;
    begin
      inherited;
    end;
    
    method TAlarm.Equals(obj: System.Object): Boolean;
    begin
      if  (obj = nil) or (GetType() <> obj.GetType()) then
        Exit(False);
      Result := TAlarm(obj).TagName.Equals(Self.TagName);
    end;
    
    method TAlarm.ToString(): String;
    begin
      Result := Self.TagName;
    end;
    
    // In MainForm.pas
    method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);
    var
      Idx: Integer;
    begin
      Idx := ComboBox1.SelectedIndex;
      if Idx <> -1 then
        ListBox1.SelectedIndex := ListBox1.Items.IndexOf(ComboBox1.Items[Idx]);
    end;
    
    method MainForm.MainForm_Load(sender: System.Object; e: System.EventArgs);
    var
      i, j: Integer;
      Alarm: TAlarm;
      aList: Array[0..4] of Object;
      aFind: Array[0..1] of Object;
    begin
      j := 0;
      for i := 0 to 4 do
      begin
        Alarm := new TAlarm;
        Alarm.TagName := String.Format('Alarm{0}', i);
        aList[i] := Alarm;
        // Place items 1 & 3 in another array of searchable items -
        // just for fun. Not suggesting implementing your app this way
        // by any means.
        if (i mod 2) > 0 then
        begin
          aFind[j] := Alarm;
          Inc(j);
        end;
      end;
      ListBox1.Items.AddRange(aList);
      ComboBox1.Items.AddRange(aFind);
    end;
    

    Here’s how it looks with an item selected in the ComboBox after clicking the Button:

    Screen capture of object found in ListBox

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im not sure if I'm doing this right. I have an action that I
Not too sure if I'm doing this right. It looks a bit more convoluted
Not sure whether this is the right place, but I have a question related
OK, first of all I'm not sure I'm doing this right so I will
2 Questions really, and I'm not sure if I'm doing this right or not...
I'm not sure how to do this, i thought i was doing it right,
Not sure if I am doing this correctly or not. Here is my .js
I'm not sure whether or not this is the appropriate way of doing this,
I know this probably really simple but Im not sure what im doing wrong...
I'm mot sure if I'm doing this wrong or not I don't know if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.