-1

I stuck converting class..

I have an arraylist that contains 4 different class which are not releated each other.

for example when user choose one like mylist[i], i should convert it, it's certain class and after can use .. i mean like

var type = mylist[i].GetType();

so now I have item's type. and when I create a variable as this type i mean like

var newItem = (type)mylist[i]

but type is not a class so then i cant convert it.

I am not sure if i am clear..

unbalanced
  • 1,192
  • 5
  • 19
  • 44
  • 3
    This is why you should *never* use `ArrayList`. `List` was introduced in .NET 2.0 for a reason; why aren't you using it? By the way; that cast is impossible. – BradleyDotNET Nov 10 '14 at 23:56
  • What Bradley said. If each of the different classes are different types but have the same functionality consider implementing an interface and create an arraylist of the interface type u created – DarkBee Nov 10 '14 at 23:57
  • 4 classes are different than each other. But some properties are same. So, List is useable in this case? – unbalanced Nov 11 '14 at 00:00
  • U can extend them from the same abstract class if they share same properties – DarkBee Nov 11 '14 at 00:01
  • The problem is, these come from webservice, then i can change it.. – unbalanced Nov 11 '14 at 00:02

2 Answers2

3

You can't cast to a variable - you have to explicitly state the type. You'll need to check for each class separately:

if(mylist[i] is FirstType)
{
    //do something
}
else if (mylist[i] is SecondType)
{
    //do something
}
//etc

A possibly better alternative would be to create 4 separate lists using List<T> and hold each type separately:

var firstList = new List<FirstType>();
var secondList = new List<SecondType>();
//etc

Or, if possible, create a base class if the data is similar enough and use a List<T>.

public class MyBase
{
    public int Id { get; set; }
}

public class FirstType : MyBase
{
}

public class SecondType : MyBase
{
}

var list = new List<MyBase>();
list.Add(new FirstType());
list.Add(new SecondType());

As mentioned in the comments, you can use partial classes to do this as well. They allow you to define a class in 2 or more files. For example, see how the Profile class you posted is defined as public partial class Profile : ProfileBase? You can create your own file and define it again, with other interface implementations. For instance:

public interface IMyInterface
{
    public int Id { get; set; }
}

public partial class Profile : IMyInterface
{
    public int Id { get; set; }
}

This is a good overview on partial classes: http://msdn.microsoft.com/en-us/library/wa80x488.aspx

Dave Zych
  • 21,581
  • 7
  • 51
  • 66
  • i know to do that, but i need a variable in that type. – unbalanced Nov 10 '14 at 23:59
  • @Dave u can cast with `Type t = field as Type` and check if t is not null then the cast succeeded? – DarkBee Nov 11 '14 at 00:00
  • The classes come from webservice, so I cant change it. – unbalanced Nov 11 '14 at 00:02
  • Then I would use 4 different lists if possible. They're inherently different things - there's no need to store them together. – Dave Zych Nov 11 '14 at 00:04
  • I am developing an ios project in C#. When the user select the row from table, an event triggers, and it gives me an index that selected row's, so if i have 4 list, how i can know which index belongs to ? If I collect all items in one generic list, and i can use this index like mylist[i] , with that i can reach the item. – unbalanced Nov 11 '14 at 00:07
  • But now problem, i should use it as a new variable as wrote on question – unbalanced Nov 11 '14 at 00:08
  • @unbalanced U can change them by using `partial` classes – DarkBee Nov 11 '14 at 00:08
  • I dont understand what you mean? For example, this is from reference.cs (actually service created) public partial class DynamicViewSearch { so its already partial class – unbalanced Nov 11 '14 at 00:12
  • and this another class that i am using----- public partial class Profile : ProfileBase { – unbalanced Nov 11 '14 at 00:13
  • @unbalanced I updated my answer with specifics about partial classes. – Dave Zych Nov 11 '14 at 15:09
3

You can't cat to type defined at run time.

Assuming types you have are similar, but not related you can

  • use dynamic to switch to late binding. It will allow you to call methods you want at cost of compile type safety. "Duck typing" sample can be found in Duck type testing with C# 4 for dynamic objects
  • dynamically create methods that will do what you want with types defined at runtime via reflection or expression.
Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179