|
-
Jul 3rd, 2007, 11:19 PM
#1
Thread Starter
Addicted Member
define and cast to a generic object...
Hello all,
Ok, basically, I have a generic DataParameter class, defined as below...
Code:
class DataParameter<T> {
private string _name;
private T _value;
private SqlDbType _dataType;
public string Name {
//stuff
}
public T Value {
//stuff
}
public SqlDbType DataType {
//stuff
}
public DataParameter(string ParameterName, T ParameterValue, SqlDbType ParameterDataType) {
//stuff;
}
so basically, how I have structured my data class, is that you define a parameter of what datatype u need.. 'DataParameter<string>' for instance. You add the name, value, and datatype using the constructer. Then you add the parameter object to a hashtable.
My problem is getting it back out of the hashtable. I am unsure as to how i define the class, as to what datatype to put in the <>...
Is there possibly another collection that i should use, that will hold the type of the generic class?
Cheers,
Justin
-
Jul 4th, 2007, 06:29 AM
#2
Re: define and cast to a generic object...
I am not quite sure what your question is. If you want a generic hash table use Dictionary<K,V>.
-
Jul 4th, 2007, 06:00 PM
#3
Thread Starter
Addicted Member
Re: define and cast to a generic object...
 Originally Posted by penagate
I am not quite sure what your question is. If you want a generic hash table use Dictionary<K,V>.
Hi Penegate,
I read up on the Dictionary<K,V>, but thats not exactly what im after...i dont think...
the Key of the hashtable does not matter, as i am simply using an integer...
but the value is where the problem lies.
Say, for instance, i have 3 Parameters that i need to pass to a stored procedure. Each DataParameter object consists of three member variables. Name , Value & DataType.
Code:
// DataParameter(string Name, object Value, SqlDbType DataType)...
DataParameter _pId = new DataParameter("@contact_id", _contactId, SqlDbType.Int);
DataParameter _pName = new DataParameter("@contact_name", _contactName, SqlDbType.VarChar);
DataParameter _pDate = new DataParameter("@contact_date", _contactDate, SqlDbType.DateTime);
Hashtable _params = new Hashtable();
_params.Add(0, _pId);
_params.Add(1, _pName);
_params.Add(2, _pDate);
as it stands, i use the base object 'object', as the default for the Value parameter... however i feel that this is just taking a shortcut, and would like to be able to use multiple object types...hence Generics...
the problem comes with generics when trying to retrieve the Object DataParameter from the Value part of the hashtable...
My retrieval code uses...
Code:
foreach (DictionaryEntry de in Parameters) {
DataParameter _parameter = (DataParameter)de.Value;
_cmd.Parameters.Add(_parameter.Name, _parameter.DataType).Value = _parameter.Value;
}
so, as you can see, how i do define the generic type when pulling it out of the hashtable?
Cheers,
Justin
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|