Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oops1
{
    class Program
    {
        static void Main(string[] args)
        {

            string strdetail = "Anurag Nayak 8585916787";
            B b1 = new B();
            object obj= b1.Split(strdetail);

        }
    }
    class B
    {

        public object Split(string str)
        {

            String[] strarray = new String[3];
            strarray = str.Split(' ');

            API p = new API();
            p.firstname = strarray[0];
            p.lastname = strarray[1];
            p.mobilenumber = strarray[2];
            return p;

        }


    }
}



---I return an object from Split function.
--- object obj= b1.Split(strdetail);

How to iterate through the obj.
-- I was trying foreach. But foreach cannot be used with objects. 

Kindly suggest
.
Posted

Try this


C#
string strdetail = "Anurag Nayak 8585916787";
           B b1 = new B();
           object obj = b1.Split(strdetail);
           API api = obj as API; // casting the object to Type API
           string firstname = api.firstname;
           string lastname = api.lastname;
           string mobieno = api.mobilenumber;
 
Share this answer
 
v2
Comments
anurag19289 18-Jan-14 6:40am    
:) that is superb...

So is the only way to tackle such scenario.
Karthik_Mahalingam 18-Jan-14 8:27am    
Thanks Anurag :)
Class B seems redundant, you could simplify this to something like:

C#
namespace oops1
{
    class Program
    {
        static void Main(string[] args)
        {

            string strdetail = "Anurag Nayak 8585916787";
            string[] details = strdetail.Split(' ');
            API p = new API(details);
        }
    }


   public class API
   {
       public string Firstname
       {
           get;
           set;
       }
       public string Lastname;
       {
           get;
           set;
       }
       public string Mobilenum;
       {
           get;
           set;
       }

       public API(string [] details)
       {
           Firstname = details[0];
           Lastname = details[1];
           Mobilenum = details[2];
       }
    }
}

You can now print any of the fields with a statement such as
C#
Console.WriteLine("name is: {0} {1}", p.Firstname, p.Lastname);
 
Share this answer
 
v2
Comments
anurag19289 18-Jan-14 7:09am    
That is really cool. But i have one doubt. As you said i corrected in this way.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oops1
{
class Program
{
static void Main(string[] args)
{

string strdetail = "Anurag Nayak 8585916787";
string[] details = strdetail.Split(' ');
API p = new API(details);
}
}

}

----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oops1
{
public class API
{
string _firstname;
string _lastname;
string _mobilenum;

string[] words;
public API(string [] details)
{
_firstname = details[0];
_lastname = details[1];
_mobilenum = details[2];

}
}
}
--------------------------------
In the Main method now after this line how to retrieve here:

API p = new API(details);

--i i put a breakpoint and reach till the above line(over p) ...i could see the details.
now i want to print in the console all the details.
Richard MacCutchan 18-Jan-14 7:22am    
See my updated solution.
anurag19289 18-Jan-14 8:26am    
Got it ... Thank you :)
Why do you need to convert it into object when the split returns array string.

Try this

string strdetail = "Anurag Nayak 8585916787";
B b1 = new B();
string[] str= b1.Split(strdetail);


then to use iteration use like this:-

for(int i=0;i<str.length;i++)>
{
    //do your work
}
 
Share this answer
 
Comments
anurag19289 18-Jan-14 6:45am    
Actually i knew the above method...but i was exploring something new... like how to make use of object and is it possible to iterate..
Karthik explained a nice way

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900