Get an object type and assign values ​​accordingly

I have an arraylist that gets different types of values , the first value is → string , the second value is → datetime , 3rd value is → boolean , and the fourth value is int , how can I find its type and assign these values ​​accordingly, any help would be appreciated :)

here is my code:

foreach (object obj in lstTop)
            {

              if(obj.GetType() == string)
                {do this...)
              else if(obj.GetType() == DateTime)
                {do this....}
              else if(obj.GetType() == bool)
                {do this....}
              else if(obj.GetType() == Int)
                {do this....}
            }

Thanks to everyone, my final code is:

string Subscription = "";
        DateTime issueFirst;
        DateTime issueEnd;

        foreach (object obj in lstTop)
        {
            ///Type t = obj.GetType();
            if (obj is string)
                Subscription += obj + ",";
            else if (obj is DateTime)
            {
               Subscription += Convert.ToDateTime(obj).ToShortDateString() + ",";
            }
           /// else if (t == typeof(DateTime))                
        }
    return ("User Authenticated user name: " + userName + ", Subscription: " + Subscription);
+5
source share
7 answers
foreach (object obj in lstTop)
        {

          if(obj is string)
            {do this.....}
          else if(obj is DateTime)
            {do this.....}
          else if(obj is bool)
            {do this.....}
          else if(obj is Int)
            {do this.....}
          else
          {
              // always have an else in case it falls through
              throw new Exception();
          }
        }
+7
source

ArrayLists .Net 2.0 . , , List<Object>, , - .Net 1.1.

, is , :

if (obj is string)
    // do this
else if (obj is DateTime)
    // do this
// ...

. , , . , , - :

void DoSomething(string value) { /* ... */ }
void DoSomething(DateTime value) { /* ... */ }

DoSomething(obj);
+2

- , , .

string   myString = (string)   lstTop[0];
DateTime myDate   = (DateTime) lstTop[1];
bool     myBool   = (bool)     lstTop[2];
int      myInt    = (int)      lstTop[3];
+1

, Dictionary ( ArrayList ) :

private Dictionary<Type, Object> data = GetDataList();
string myString = (string)data[typeof(string)];
int myInt = (int)data[typeof(int)];

, , .

ArrayList :

ArrayList data = new ArrayList();
data.Add(1);
data.Add("a string");
data.Add(DateTime.Now);

Dictionary<Type, Object> dataDictionary = new Dictionary<Type, object>();
for (int i = 0; i < data.Count; i++)
{
    dataDictionary.Add(data[i].GetType(), data[i]);
}
+1

, . .

foreach( MyBaseData data in lstData )
{
    data.DoTheRightThing();
}

, , , - , , , .

, , - .

+1

:

foreach (object obj in lstTop)
        {

          if(obj is string)
            {do this...)
          else if(obj is DateTime)
            {do this....}
          else if(obj is bool)
            {do this....}
          else if(obj is int)
            {do this....}
        }

, .

0
        foreach (object obj in lstTop)
        {

          if(obj.GetType() == typeof(string))
            {do this...)
          else if(obj.GetType() == typeof(DateTime))
            {do this....}
          else if(obj.GetType() == typeof(bool))
            {do this....}
          else if(obj.GetType() == typeof(int))
            {do this....}
        }

GetType System.Type . System.Type, typeof.

0

All Articles