Using Visual Studio, c#, How do I assign to a generic object and see the properties after assignment?

Shermak, Carl (C.E.) 20 Reputation points
2023-05-16T15:40:38.7433333+00:00

I have an object that when it changes it will fire the event below. There are two types of objects that have similar properties that could trigger the event. I want to check to see which type and assign that to the "tag" object. I thought by putting the object type in parenthesis before the object that it would convert and give me properties. When I type "tag." I just get generic properties and not that of the object that I was trying to convert.

What am I doing wrong?



Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Davin Mickelson 121 Reputation points
    2023-05-16T15:54:15.74+00:00

    Hi.

    I cannot see any included source code in your initial question so I cannot give you a great answer.

    My "guess" is that you need to look at the sender property to see which object raised the event. However, if both objects are just sending "this" as an object type, then perhaps you can inspect the tag (message) you are sending in the event args. Once you know the object type, you can cast the sender object as the original type to extract the unique properties.

    I don't have much respect for answers that tell the OPs to go somewhere else for answer, but I did write up a useful blog article explaining how to use delegates and events. You can read it here:

    https://www.intertech.com/c-sharp-tutorial-understanding-c-events/

    Best wishes and good luck.


  2. Davin Mickelson 121 Reputation points
    2023-05-16T16:17:40.1533333+00:00
    private void tagChanged(object sender, EventArgs e)      
    {           
      Logix.Tag lgxTag;
      Logix.DataChangeEventArgs lgxDataChangeEventArgs;
      ABLink.Tag ablTag;
      ABLink.DataChangeEventArgs ablDataChangeEventArgs;
    
      try          
      {              
        if (bProgramClosing)              
        {                  
          return;              
        }               
        if (sPLCType == "Logix")              
        {                  
          lgxTag = (Logix.Tag)sender;
          lgxDataChangeEventArgs = (Logix.DataChangeEventArgs)e;
    	// lgxTag.Prop1 - Use the props for the lgxTag object
    	// lgxDataChangeEventArgs.Prop2 - Use the props for the lgxDataChangeEventArgs object
        }              
        else              
        {                  
          ablTag = (ABLink.Tag)sender;
          ablDataChangeEventArgs = (ABLink.DataChangeEventArgs)e;
    	// ablTag.Prop1 - Use the props for the ablTag object
    	// ablDataChangeEventArgs.Prop2 - Use the props for the ablDataChangeEventArgs object
        }
      }
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.