Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

One Array Multiple Types

Status
Not open for further replies.

4ks

Programmer
Jan 27, 2007
5
AU
Trying to store multiple types in an Array, i know it’s not possible but I am looking for a solution. What is the best way to get around this issue?
 
try an arraylist - it is the most flexible.

using System.Collections;
 
What i mean is i am trying to store 2 different types of custom objects in 1 array. I have tried and array of type Object but run into problems when i then try to access methods of the individual objects as there type is not know.
 
almost sounds like it's time for an interface :)

Why are you storing different types in an array? do they all do similar things? If this is the case then you will want to google interfaces in c#. This will let you work with an object regardless of its type, as long as it implements the interface.

If this isn't the case, then I suggest you re-think why you are storing 2 types in one array. I would instead create 2 separate arrays based on type.

If you absolutely must have both in one array, then check their type with the "is" keyword.

if (myarray[0] is MyObject)
{
//dosomething
}

The last method involves casting (internally) which tends to be slow. The interface method is probably the best.
 
I have 2 types of questions both have similar properties, and reach use an interface to define the methods which are common to both. My array needs to contain various question type 1 and type 2 which forms the quiz. The program moves through the array of questions displaying each one based on its type, it then stores the user’s responses. So I need everything in one array. How would this usually be achieved?
 
well I don't know what your questions are, but let's say in this case we have multiple choice and true/false.

the common parts of our classes are:

public interface IQuestion
{
string Question {get;set;}
string Answer {set;}
bool Evaluate(string answer);
}


so let's create an actual classe

public class MultipleChoiceQuestion : IQuestion
{
private string answer = null;
private string question = null;

public MultipleChoiceQuestion()
{
}

public string Question
{
get
{
return question;
}
set
{
question = value;
}
}

public string Answer
{
set
{
answer = value;
}
}

public bool Evaluate(string response)
{
return (response.ToUpper().Equals(answer.ToUpper()));
}
}


create another class the same way - implementing the interface.


then you can use those classes like so:

ArrayList questions = new ArrayList();

public void DoQuiz()
{
MultipleChoiceQuestion q1 = new MultipleChoiceQuestion();
q1.Question = "Insert my question here with options";
q1.Answer = "C";

AskQuestions();
}

public void AskQuestions()
{
foreach (IQuestion q in questions)
{
Console.WriteLine(q.Question);
Console.Write("Answer: ");
string answer = Console.ReadLine();

if (q.Evaluate(answer))
{
Console.WriteLine("correct");
}
else
{
Console.WriteLine("incorrect");
}
}
}


Now you can ask any kind of question as long as it is an IQuestion.
 
Thanks, yea that’s what I’ve done except i was adding additional methods into each question which weren't part of the interface so they weren’t exposed when i added the questions to the list.
Thanks for the help learning slowly.
 
Create a container class that aggregates your two (odd) objects. Add that class to your array.

BTW, like everyone's been telling you, it's a bad idea to mix-n-match different types in the same container. Don't do this.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top