[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Adding Different Item types to a C# Collection

Posted on 2004-10-29
3
Medium Priority
?
191 Views
Last Modified: 2010-04-15
I have a class in C# that inherits the Collectionbase with appropriate routines (code snippet below,

public class MyClass : System.Collections.CollectionBase
{
  //Add a Standard Segment Object
  public void Add(Segment xSegment)
  {
     List.Add(xSegment);
   }

  //Add a Loop Object
  public void Add(Loop xLoop)
  {
     List.Add(xLoop);
  }
            
 public Loop LoopItem(int Index)
 {
 }

 public Segment Item(int Index)
 {
 }
      
}

I have encountered an instance however, where I think it will be most efficient if I can have a collection class that can hold two different type of Objects.  I was able to override the add routine easily enough.  But as you can see I cannot override the "Get" item routine.  I have to have LoopItem and Item routines which causes problems later on.

Any ideas?

Mandalorian

0
Comment
Question by:mandalorian4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 15

Accepted Solution

by:
SRigney earned 500 total points
ID: 12446550
return an object

public object Item(int Index)
 {
 }


Then after you have it returned you can see what type of object it is and use it accordingly.
0
 
LVL 3

Author Comment

by:mandalorian4
ID: 12446656
SRigney,

Thank you! It was right in front of me!

0
 
LVL 9

Expert Comment

by:tinchos
ID: 12446686
Hi Mandalorian

The problem you're experiencing is the following.....

every method in C# has a method name, its parameters and the return type.

Unfortunately, if the name and the parameters are the same, the compiler doesn't know which method the programmer is calling so as to know the type of the object to return..... it wouldn't know until runtime.

Thats why methods called the same cannot differ only in the return type.

Therefore as SRigney you should return an object and evaluate after which type of object you're receiving.

Hope this helps

Tincho
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question