How to pass unknown datatypes to methods?

  • Thread starter Thread starter DrPete
  • Start date Start date
D

DrPete

Hi

How do you go about passing values to methods if you don't know what
the values datatype is?

For example:

myParm = sqlCommand.Parameters.Add("@Name", SqlDbType.VarChar, 30,
"Name");
myParm.Value = dataRow["NAME"].ToString();

I wish to have a method to validate a value instead of the second line,
e.g. the second line becomes

myParm.Value = validateParm(dataRow["NAME"].ToString());

private void validateParm(string theValue)
{
....
}

However, what happens if I want to pass in a datetime, or int? At the
moment i have seperate methods for each datatype but I know it's a bad
way of doing it!

Kind regards
Peter
 
DrPete said:
Hi

How do you go about passing values to methods if you don't know what
the values datatype is?

For example:

myParm = sqlCommand.Parameters.Add("@Name", SqlDbType.VarChar, 30,
"Name");
myParm.Value = dataRow["NAME"].ToString();

I wish to have a method to validate a value instead of the second line,
e.g. the second line becomes

myParm.Value = validateParm(dataRow["NAME"].ToString());

private void validateParm(string theValue)
{
...
}

However, what happens if I want to pass in a datetime, or int? At the
moment i have seperate methods for each datatype but I know it's a bad
way of doing it!

Kind regards
Peter

Hi,

If the types you want to pass in are your own you could derive them
from a base class or an interface and use that base type as the
parameter type. Otherwise pass them in as an object. Whichever way
you can then query the type and cast back.

If you want a more detailed explination let me know.

Nick
 
Hi Peter,

Rather than using different method, try overloading you validateParm()
method with serveral versions that each take a different parameter
datatype, e.g.

private void validateParm(string theValue) { ... }
private void validateParm(int theValue) { ... }
private void validateParm(DateTime theValue) { ... }
etc.

The correct method will be used automatically depending on the
parameter type that is passed.

If you want to do all your validation in a single method block then you
can use polymorphism, where your parameter type is a superclass of any
type that might be passed. In this case, use the object type, which is
the root of the class hierarchy and therefore a superclass of
everything. You can then check the parameter's datatype inside the
routine, and cast it to the type you want to work with:

private void validateParm(object theValue) {
if (obj is string)
{
if (Regex.Match((string)obj, "someValidationPattern")) { ... }
}
}

HTH,

Chris
 
Okay, first I need to know what type your "dataRow" variable represents. If
it is a System.Data.DataRow in a DataTable, and if the DataColumns in the
DataTable are typed, the type of the data in each cell is already
determined. The parameter itself will know what type it is expecting. But
whether you need to discover the type of data in the DataColumn (if that is
what it is) depends upon what the DataColumn knows about the type of data
contained in it. A DataColumn can contain any type, but it can only contain
one type. The type can be serialized into a string, but when it is in the
DataColumn, it is of the type in the DataColumn.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
Hello:

Can't we just pass the value as object and find the type information at
runtime?

For example:

MyMethod(10);
MyMethod("String");
MyMethod(DateTime.Now);

And MyMethod's implementation as:

private void MyMethod(object o)
{
}

In MyMethod, you can use the GetType() of the variable o, and write
your logic based on that.

HTH!
Sk&y;
 
Thanks for all your replies, I'll try out the polymorphist way using a
superclass.. ;)

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top