By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,759 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,759 IT Pros & Developers. It's quick & easy.

Urgent question w/ boxing... please help

P: n/a
Hi everyone,

I have a problem that i have been trying to solve for awhile. I'm
given a code where the purpose is to create a general dataset mapper.
Given any dataset, i have a class, "Mapper.cs" that's supposed to map
objects from any type of dataset to any type of object.

Inside Mapper.cs, there's a method,

object[] Map(Type typeOfObjectToMap, DataSet theDataSet)
{
//iterate DataSet();

}

which contains implementation to iterate through the dataset rows and
converts each column in the row to the appropriate type in the class
typeOfObjectToMap.

For instance:

DataSet=

FirstName | LastName | Age
John Doe 35
Rachel Gates 40

typeOfObjectToMap =

Class peopleInfo
{
public string FirstName;
public string LastName;
public string Age;

get...
set..
}

so in Map method, it would iterate thru the dataset and find the
corresponding column value in the peopleInfo class to identify what
type to convert the dataset value to. Thus, it will convert the John
value to a string, Doe value to a string and Age value to an int32.

I already have implementation in the code to iterate and convert each
value to the appropriate type and return each value in the column as an
object.

The problem is at the end of the Map method, I must return an object[]
.. Each object in the object[] must contain the boxed object of the
type passed in the Map argument: typeOfObjectToMap which essentially
contains a single row info. from the dataset. Can someone please tell
me how I can accomplish this?
Thanks so much, Sharon

Jun 17 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Create an Object array inside the method, assign the object to generic
Object array and return it. Have a try. It is good to attach your cs files.

chanmm

<ri*******@gmail.com> wrote in message
news:11**********************@y41g2000cwy.googlegr oups.com...
Hi everyone,

I have a problem that i have been trying to solve for awhile. I'm
given a code where the purpose is to create a general dataset mapper.
Given any dataset, i have a class, "Mapper.cs" that's supposed to map
objects from any type of dataset to any type of object.

Inside Mapper.cs, there's a method,

object[] Map(Type typeOfObjectToMap, DataSet theDataSet)
{
//iterate DataSet();

}

which contains implementation to iterate through the dataset rows and
converts each column in the row to the appropriate type in the class
typeOfObjectToMap.

For instance:

DataSet=

FirstName | LastName | Age
John Doe 35
Rachel Gates 40

typeOfObjectToMap =

Class peopleInfo
{
public string FirstName;
public string LastName;
public string Age;

get...
set..
}

so in Map method, it would iterate thru the dataset and find the
corresponding column value in the peopleInfo class to identify what
type to convert the dataset value to. Thus, it will convert the John
value to a string, Doe value to a string and Age value to an int32.

I already have implementation in the code to iterate and convert each
value to the appropriate type and return each value in the column as an
object.

The problem is at the end of the Map method, I must return an object[]
. Each object in the object[] must contain the boxed object of the
type passed in the Map argument: typeOfObjectToMap which essentially
contains a single row info. from the dataset. Can someone please tell
me how I can accomplish this?
Thanks so much, Sharon

Jun 17 '06 #2

P: n/a
Thanks for the response chanmm.

I guess i didn't really explain it that well in my posting. Here's the
code from the map method:
public object[] Map(Type type, DataSet ds)
{

// get the public properties from the type class so i can cross ref. w/
the dataset
PropertyInfo[] propertyInfoFromType =
type.GetProperties(BindingFlags.Public|BindingFlag s.Instance);

if(ds.Tables[0].Columns.Count != propertyInfoFromType.Length)
{
throw new ApplicationException("Number of Columns from DataSet does
not match number of properties from Type : " + type.FullName);
}

/*iterate thru each item in dataset and convert to appropriate type*/
foreach (DataRow r in ds.Tables[0].Rows)
{
foreach(PropertyInfo propertyInfo in propertyInfoFromType)
{

//1 converted parameter
object [] array = basicPropertyArgumentConverter.GetValue
(propertyInfo.PropertyType.Name, r[propertyInfo.Name]);
}
}

}

Here's a sample of a Type being passed as a parameter into the map:

public class CustomerTarget
{
private string firstName = String.Empty;
private string lastName = String.Empty;
private int age = 0;
private DateTime birthDate = DateTime.MinValue;

public CustomerTarget() {}

public string FirstName
{
get { return firstName; }
set { firstName = value; }
}

public string LastName
{
get { return lastName; }
set { lastName = value; }
}

public int Age
{
get { return age; }
set { age = value; }
}

public DateTime BirthDate
{
get { return birthDate; }
set { birthDate = value; }
}
}

I have two questions:
How do I create the type objects passed into the method and how do I
assign the properties of those objects that I created to the array
objects passed back from basicPropertyArgumentConverter.GetValue()???
The idea is this mapper is intended to take in any type of class type
to map to any corresponding dataset.

The goal for me is to pass back an array of objects, each object can be
unboxed back to the class object.

please this is really urgent for me, any help would be appreciated!
Thank you

Sharon

chanmm wrote:
Create an Object array inside the method, assign the object to generic
Object array and return it. Have a try. It is good to attach your cs files.

chanmm

<ri*******@gmail.com> wrote in message
news:11**********************@y41g2000cwy.googlegr oups.com...
Hi everyone,

I have a problem that i have been trying to solve for awhile. I'm
given a code where the purpose is to create a general dataset mapper.
Given any dataset, i have a class, "Mapper.cs" that's supposed to map
objects from any type of dataset to any type of object.

Inside Mapper.cs, there's a method,

object[] Map(Type typeOfObjectToMap, DataSet theDataSet)
{
//iterate DataSet();

}

which contains implementation to iterate through the dataset rows and
converts each column in the row to the appropriate type in the class
typeOfObjectToMap.

For instance:

DataSet=

FirstName | LastName | Age
John Doe 35
Rachel Gates 40

typeOfObjectToMap =

Class peopleInfo
{
public string FirstName;
public string LastName;
public string Age;

get...
set..
}

so in Map method, it would iterate thru the dataset and find the
corresponding column value in the peopleInfo class to identify what
type to convert the dataset value to. Thus, it will convert the John
value to a string, Doe value to a string and Age value to an int32.

I already have implementation in the code to iterate and convert each
value to the appropriate type and return each value in the column as an
object.

The problem is at the end of the Map method, I must return an object[]
. Each object in the object[] must contain the boxed object of the
type passed in the Map argument: typeOfObjectToMap which essentially
contains a single row info. from the dataset. Can someone please tell
me how I can accomplish this?
Thanks so much, Sharon


Jun 17 '06 #3

P: n/a
Sharon,

You can use the System.Activator.CreateInstance(Type type, params
object[] args) method to create the type of object of your choice. The
params object[] args parameter will take an array of arguments that
will most closely match a constructor of the object you are trying to
create. For example:

public class Person
{
private string name;
private string address;

public Person()
{
}

public Person (string name, string address)
{
// Implementation
}
// Properties...
}

Activator.CreateInstance(typeof(Person), new object[] {"Steven", "21b
Baker Street"})
or
Activator.CreateInstance(typeof(Person), "Steven", "21b Baker Street")

Either will return an object that can be cast to a Person object with
their fields filled for name and address.

Hope this helps!

ri*******@gmail.com wrote:
Thanks for the response chanmm.

I guess i didn't really explain it that well in my posting. Here's the
code from the map method:
public object[] Map(Type type, DataSet ds)
{

// get the public properties from the type class so i can cross ref. w/
the dataset
PropertyInfo[] propertyInfoFromType =
type.GetProperties(BindingFlags.Public|BindingFlag s.Instance);

if(ds.Tables[0].Columns.Count != propertyInfoFromType.Length)
{
throw new ApplicationException("Number of Columns from DataSet does
not match number of properties from Type : " + type.FullName);
}

/*iterate thru each item in dataset and convert to appropriate type*/
foreach (DataRow r in ds.Tables[0].Rows)
{
foreach(PropertyInfo propertyInfo in propertyInfoFromType)
{

//1 converted parameter
object [] array = basicPropertyArgumentConverter.GetValue
(propertyInfo.PropertyType.Name, r[propertyInfo.Name]);
}
}

}

Here's a sample of a Type being passed as a parameter into the map:

public class CustomerTarget
{
private string firstName = String.Empty;
private string lastName = String.Empty;
private int age = 0;
private DateTime birthDate = DateTime.MinValue;

public CustomerTarget() {}

public string FirstName
{
get { return firstName; }
set { firstName = value; }
}

public string LastName
{
get { return lastName; }
set { lastName = value; }
}

public int Age
{
get { return age; }
set { age = value; }
}

public DateTime BirthDate
{
get { return birthDate; }
set { birthDate = value; }
}
}

I have two questions:
How do I create the type objects passed into the method and how do I
assign the properties of those objects that I created to the array
objects passed back from basicPropertyArgumentConverter.GetValue()???
The idea is this mapper is intended to take in any type of class type
to map to any corresponding dataset.

The goal for me is to pass back an array of objects, each object can be
unboxed back to the class object.

please this is really urgent for me, any help would be appreciated!
Thank you

Sharon

chanmm wrote:
Create an Object array inside the method, assign the object to generic
Object array and return it. Have a try. It is good to attach your cs files.

chanmm

<ri*******@gmail.com> wrote in message
news:11**********************@y41g2000cwy.googlegr oups.com...
Hi everyone,

I have a problem that i have been trying to solve for awhile. I'm
given a code where the purpose is to create a general dataset mapper.
Given any dataset, i have a class, "Mapper.cs" that's supposed to map
objects from any type of dataset to any type of object.

Inside Mapper.cs, there's a method,

object[] Map(Type typeOfObjectToMap, DataSet theDataSet)
{
//iterate DataSet();

}

which contains implementation to iterate through the dataset rows and
converts each column in the row to the appropriate type in the class
typeOfObjectToMap.

For instance:

DataSet=

FirstName | LastName | Age
John Doe 35
Rachel Gates 40

typeOfObjectToMap =

Class peopleInfo
{
public string FirstName;
public string LastName;
public string Age;

get...
set..
}

so in Map method, it would iterate thru the dataset and find the
corresponding column value in the peopleInfo class to identify what
type to convert the dataset value to. Thus, it will convert the John
value to a string, Doe value to a string and Age value to an int32.

I already have implementation in the code to iterate and convert each
value to the appropriate type and return each value in the column as an
object.

The problem is at the end of the Map method, I must return an object[]
. Each object in the object[] must contain the boxed object of the
type passed in the Map argument: typeOfObjectToMap which essentially
contains a single row info. from the dataset. Can someone please tell
me how I can accomplish this?
Thanks so much, Sharon


Jun 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.