473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Urgent question w/ boxing... please help

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
3 2057
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SK | last post by:
What is the real use of boxing and unboxing in what situations will it be used. Since all types are utlmately dereived from object..what is the need for boxing and unboxing.....help
4
by: Alistair Welchman | last post by:
I have a Hashtable of ints keyed on Guids, and I want to do the following: foreach ( DataRow row in dsWorkDays.Tables.Rows ) { Guid PersonId = (Guid)row; DateTime Day = (DateTime)row;
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
6
by: Justine | last post by:
Hi All, I need a small clarification with regard to Boxing. struct Currency { ......... } Currency Bal = new Currency; Object Obj = Bal;
1
by: Tom | last post by:
Couple of questions relating to boxing. Firstly, I already know that boxing is the processing of temporarily copying a ValueType (e.g. struct, enum) to the heap so that the system can treat a...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
17
by: Saps | last post by:
Hi all. Can anyone help me here. I have loads of .sql files and i need a way to call these from my asp page so the user can run them from the browser. Meaning i have a page with a list of all...
19
by: ahjiang | last post by:
hi there,, what is the real advantage of boxing and unboxing operations in csharp? tried looking ard the internet but couldnt find any articles on it. appreciate any help
161
by: Peter Olcott | last post by:
According to Troelsen in "C# and the .NET Platform" "Boxing can be formally defined as the process of explicitly converting a value type into a corresponding reference type." I think that my...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.