473,378 Members | 1,146 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,378 software developers and data experts.

How to pass unknown datatypes to methods?

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

Apr 9 '06 #1
5 8679

DrPete wrote:
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

Apr 9 '06 #2
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

Apr 9 '06 #3
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.
"DrPete" <p.*******@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
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

Apr 9 '06 #4
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;

Apr 9 '06 #5
Thanks for all your replies, I'll try out the polymorphist way using a
superclass.. ;)

Peter

Apr 11 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
1
by: Jan Agermose | last post by:
Im writing information into an existing excel document using a connection string like: strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Filename + ";Extended Properties=\"Excel...
3
by: Brett | last post by:
I have several classes that create arrays of data and have certain properties. Call them A thru D classes, which means there are four. I can call certain methods in each class and get back an...
3
by: Ed L. | last post by:
On 7.4.6, is there any problem with defining one column of a view to be a string literal? For example ... $ psql -c "create view fooview as select 'bar' as footype" WARNING: column "footype"...
3
by: Eric | last post by:
Hi, I wonder if it is possible to import and use an unknown dll in a C# program? I.e. get to know what public variables, methods, properties etc. the dll contains and get to know what parameters to...
7
by: John | last post by:
Hi Everyone, I'm having this extremely annoying problem with Internet Explorer 6, giving me an error message saying "unknown runtime error" whenever I try to alter the contents of a <divelement...
5
by: Angus | last post by:
Hello I have an enum in a class like this: enum MessageType { GET, //get message (get SAF sequence no.) RESET, //reset sequence no. UNKNOWN //unrecognised...
13
by: Francois Appert | last post by:
This post was originally in the C# Corner site, but their server is down. I'd like to see if this group can answer. I program in C++ and am learning C#. The issue is: why should anybody...
1
by: echochrome | last post by:
I was wondering if there was a quick and easy way to expose methods of an unknown DLL? is there a program that would dump a list? The reason that I am asking is that I have a few DLLs I'd like to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.