tody4

What I want to accomplish is creating a class that is not creatable and is static, but the members do not have to deal completely with static data. What I want is a utility class that converts one object to another, such as DataTable to Object [,], but this example would not use static members, the data in the datatable does not change, but the object [,] does during it's creation. The size is defined at the beginning of the class, but the data within the array is given the data in the datatable. I want it to be called without having to create a new instance of the class. The way that I have it now is where it's a regular class but the user has to create an instance of the class to use the conversion methods. I have one for File -> StringCollection, DataTable to Object [,] (for use in the XLClass handler that I wrote, and vice versa. That's all that I have right now for this class, but i have a file handler object that I want to write the same way. Below is the code that I have so far for the conversion class, some specifics are renamed. as you can see it's just a generic conversion to different types.

[code]

#region Using Statements

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

#endregion

namespace Utilities

{

#region Class Utilities.Conversion

/// <summary>

/// Conversion Utilities

/// </summary>

public class Conversion

{

#region Methods

/// <summary>

/// Creates a 2 dimensional array with data from a datatable

/// </summary>

/// <param name="_dt">DataTable</param>

/// <returns>2 dimensional array</returns>

public object[,] DataTable2ObjArray(DataTable _dt)

{

object[,] _objArr = new object[_dt.Rows.Count, _dt.Columns.Count];

for (int rowNo = 0; rowNo < _objArr.GetUpperBound(0); rowNo++)

for (int colNo = 0; colNo < _objArr.GetUpperBound(1); rowNo++)

_objArr[rowNo, colNo] = _dt.Rows[rowNo][colNo];

return _objArr;

}

/// <summary>

/// Creates a datatable given a 2 dimensional array

/// </summary>

/// <param name="_objArr">Object Array</param>

/// <returns>DataTable Created</returns>

public DataTable ObjArray2DataTable(object[,] _objArr)

{

DataTable _dt = new DataTable();

for (int colNo = 0; colNo < _objArr.GetUpperBound(1); colNo++)

_dt.Columns.Add();

for (int rowNo = 0; rowNo < _objArr.GetUpperBound(0); rowNo++)

{

for (int colNo = 0; colNo < _objArr.GetUpperBound(1); colNo++)

_dt.Rows[rowNo][colNo] = _objArr[rowNo, colNo].ToString();

if (rowNo != _objArr.GetUpperBound(0))

_dt.Rows.Add(_dt.NewRow());

}

return _dt;

}

/// <summary>

/// Converts a Data Reader to a DataTable

/// </summary>

/// <param name="_dr">Data Reader</param>

/// <returns>Data Table</returns>

public System.Data.DataTable DataReader2DataTable(System.Data.Common.DbDataReader dr)

{

int maxField = dr.FieldCount;

System.Data.DataTable dt = new System.Data.DataTable();

for (int fieldCt = 0; fieldCt < maxField; fieldCt++)

dt.Columns.Add(dr.GetName(fieldCt));

while (dr.Read())

{

System.Data.DataRow _data = dt.NewRow();

for (int fieldCt = 0; fieldCt < maxField; fieldCt++)

_data[fieldCt] = dr[fieldCt];

dt.Rows.Add(_data);

}

dr.Close();

return dt;

}

#endregion

}

#endregion

}[/code]



Re: Visual C# General where to find static definitions

theTroll527

Your methods must be static also, so;

public object[,] DataTable2ObjArray(DataTable _dt) should be

public static object[,] DataTable2ObjArray(DataTable _dt)

That should fix your problems.

theTroll






Re: Visual C# General where to find static definitions

Christian Liensberger - MSP

You only need to mark the class and methods as static. I changed your class to that (see below). Then you can use it like: Utilities.Conversion.DataReader2DataTable(...);

/// <summary>

/// Conversion Utilities

/// </summary>

public static class Conversion

{

#region Methods

/// <summary>

/// Creates a 2 dimensional array with data from a datatable

/// </summary>

/// <param name="_dt">DataTable</param>

/// <returns>2 dimensional array</returns>

public static object[,] DataTable2ObjArray(DataTable _dt)

{

object[,] _objArr = new object[_dt.Rows.Count, _dt.Columns.Count];

for (int rowNo = 0; rowNo < _objArr.GetUpperBound(0); rowNo++)

for (int colNo = 0; colNo < _objArr.GetUpperBound(1); rowNo++)

_objArr[rowNo, colNo] = _dt.Rows[rowNo][colNo];

return _objArr;

}

/// <summary>

/// Creates a datatable given a 2 dimensional array

/// </summary>

/// <param name="_objArr">Object Array</param>

/// <returns>DataTable Created</returns>

public static DataTable ObjArray2DataTable(object[,] _objArr)

{

DataTable _dt = new DataTable();

for (int colNo = 0; colNo < _objArr.GetUpperBound(1); colNo++)

_dt.Columns.Add();

for (int rowNo = 0; rowNo < _objArr.GetUpperBound(0); rowNo++)

{

for (int colNo = 0; colNo < _objArr.GetUpperBound(1); colNo++)

_dt.Rows[rowNo][colNo] = _objArr[rowNo, colNo].ToString();

if (rowNo != _objArr.GetUpperBound(0))

_dt.Rows.Add(_dt.NewRow());

}

return _dt;

}

/// <summary>

/// Converts a Data Reader to a DataTable

/// </summary>

/// <param name="_dr">Data Reader</param>

/// <returns>Data Table</returns>

public static System.Data.DataTable DataReader2DataTable(System.Data.Common.DbDataReader dr)

{

int maxField = dr.FieldCount;

System.Data.DataTable dt = new System.Data.DataTable();

for (int fieldCt = 0; fieldCt < maxField; fieldCt++)

dt.Columns.Add(dr.GetName(fieldCt));

while (dr.Read())

{

System.Data.DataRow _data = dt.NewRow();

for (int fieldCt = 0; fieldCt < maxField; fieldCt++)

_data[fieldCt] = dr[fieldCt];

dt.Rows.Add(_data);

}

dr.Close();

return dt;

}

#endregion

}






Re: Visual C# General where to find static definitions

tody4

I thought that when you marked a method and class as static that all the objects that the class and methods use have to also be static. Is this not the case



Re: Visual C# General where to find static definitions

CalvinR

static methods can use objects that aren't static as long as they are passed in to the method, they can't use non-static members of classes.