Handlling different Data types

J

Jayaram Vytla

I have a requirement to handle different data types in the value
variable. I don't want to use 'object' and type casting each time.
What is the best way to do this?

-----------------------------------------
My hashtable contains
-> Key
-> Pointclass
-----------------------------------
class Pointclass
{
string pointname;

pointvalue;

DataType pointvalueDataType;

DateTime pointtimestamp;

}
-------------------------------------

My pointvalue will be string or int or float or double or long or any
DataType

Thanks in advance.
Jayaram
 
I

Ignacio Machin ( .NET/ C# MVP )

I have a requirement to handle different data types in the value
variable. I don't want to use 'object' and type casting each time.
What is the best way to do this?

-----------------------------------------
My hashtable contains
-> Key
-> Pointclass
-----------------------------------
class Pointclass
{
string pointname;

pointvalue;

DataType pointvalueDataType;

DateTime pointtimestamp;

}

-------------------------------------

My pointvalue will be string or int or float or double or long or any
DataType

Thanks in advance.
Jayaram

you have two options, either you turn generic the class or you use
object
generic:
class Pointclass<T> {
string pointname;
public T PointvalueDataType{get;set;}
DateTime pointtimestamp;

}
 
P

Peter Duniho

I have a requirement to handle different data types in the value
variable. I don't want to use 'object' and type casting each time.
What is the best way to do this?

That depends on what your _actual_ requirement is. If you want a single
variable or property to store and retrieve unreleated data types, then you
MUST use "object" and cast the value back as appropriate, whether you want
to or not.

If your requirement isn't literally that, then other alternatives include:

-- declaring multiple variables or properties, each typed with the
specific type you want and using it as the circumstances require
-- making the class generic, and using the type parameter to define
the type of the variable
-- in .NET 4.0, using the "dynamic" type

And maybe some others that don't come to mind immediately. Note that with
each of those alternatives, they have certain prerequisites with respect
to the design of your code which you may or may not be able to meet.

Unfortunately, your question does not include enough detail for anyone to
provide a specific suggestion.

Pete
 
G

Gregory A. Beamer

I have a requirement to handle different data types in the value
variable. I don't want to use 'object' and type casting each time.
What is the best way to do this?

-----------------------------------------
My hashtable contains
-> Key
-> Pointclass
-----------------------------------
class Pointclass
{
string pointname;

pointvalue;

DataType pointvalueDataType;

DateTime pointtimestamp;

}
-------------------------------------

My pointvalue will be string or int or float or double or long or any
DataType

Thanks in advance.
Jayaram

I would recommend an object here, as it is easier to code. You can cast
everything to string, but then you have to maintain type, whereas
GetType() can be used on an object.

If you are using .NET 2.0, I would move to generics so you can strongly
type the key and value types.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
J

Jayaram Vytla

(e-mail address removed):









I would recommend an object here, as it is easier to code. You can cast
everything to string, but then you have to maintain type, whereas
GetType() can be used on an object.

If you are using .NET 2.0, I would move to generics so you can strongly
type the key and value types.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog:http://gregorybeamer.spaces.live.com

*******************************************
|      Think outside the box!             |
*******************************************

Thanks a lot.

I am using .net 2.0 and I think generics are best way of doing. I
heard that casting may eating up more process time.

Can I have small example on generics to fit this problem... I did lot
of search in the google but that makes me bit confusion:(

Thanks again for responses.

Regards,
Jayaram
 
P

Peter Duniho

[...]
Can I have small example on generics to fit this problem... I did lot
of search in the google but that makes me bit confusion:(

Again, without a more specific problem description, it's not possible to
know for sure what would actually work for you. But, an example of
modifying your class so that it uses generics would be something like this:

class Pointclass<T>
{
public string pointname { get; set };

public T pointvalue { get; set };

public DataType pointvalueDataType { get; set };

public DateTime pointtimestamp { get; set };
}

Then you can have code like this:

Pointclass<int> pc1 = new Pointclass<int>();
Pointclass<string> pc2 = new Pointclass<string>();

pc1.pointvalue = 10;
pc2.pointvalue = "Foo";

Pete
 
J

Jayaram Vytla

[...]
Can I have small example on generics to fit this problem... I did lot
of search in the google but that makes me bit confusion:(

Again, without a more specific problem description, it's not possible to  
know for sure what would actually work for you.  But, an example of  
modifying your class so that it uses generics would be something like this:

     class Pointclass<T>
     {
         public string pointname { get; set };

         public T pointvalue { get; set };

         public DataType pointvalueDataType { get; set };

         public DateTime pointtimestamp { get; set };
     }

Then you can have code like this:

     Pointclass<int> pc1 = new Pointclass<int>();
     Pointclass<string> pc2 = new Pointclass<string>();

     pc1.pointvalue = 10;
     pc2.pointvalue = "Foo";

Pete

Thanks a lot Pete.

Again here the problem is i need to keep pc1 for int pc2 for strings
like that....

Ok here my problem description:

I am writing centralized interface for my product. OPC Client is one
of the Module in my project. OPC Client thread always connected to the
remote OPC Server.

OPC Server contains list of points and each point having
Name
Value
Data Type
Quality
Latest Time Stamp

One of my centralized module use hashtable(I will make it thread safe
and this will be add to another static class) to store the data.
My Hashtable contains:
Key=PointName or Unique ID
Object=Pointclass

For every defined cycle OPC client module used to update the value,
quality, time stamp in the hashtable.

Same hash table I will use to store the values in the SQL database,
flat files and same time I will supply the update values to some other
socket servers.

Is it making something sense?

Sorry about pore description.

Thanks in advance.

Jayaram
 
P

Peter Duniho

[...]
Same hash table I will use to store the values in the SQL database,
flat files and same time I will supply the update values to some other
socket servers.

Is it making something sense?

Yes and no. That is, I believe I understand the general description. But
there's nothing in that description that helps explain _how_ you might use
an instance of "Pointclass".

You can make the individual instances of the class type-safe by using
generics. But, if you want a collection of different concrete versions of
"Pointclass<T>", you'll need some common base class as the collection
member type (e.g. "object"). And you'll still need to cast to the
specific concrete "Pointclass<int>", "Pointclass<string>", etc. In that
case, the generic simply removes the type-casting one level (but that may
be helpful enough, depending on your specific usage).

As long as your requirement continues to include a need to treat your
disparate types as a single type, the requirement to cast back to the
actual type in order to access the specific typed value of "Value" will
remain. There's really no way around that.

Pete
 
G

Gregory A. Beamer

Thanks a lot.

I am using .net 2.0 and I think generics are best way of doing. I
heard that casting may eating up more process time.

Can I have small example on generics to fit this problem... I did lot
of search in the google but that makes me bit confusion:(

Thanks again for responses.

Peter has already covered creating a generic class, so I will avoid that
subject. What is not covered is a generic lookup class, so I will spend a
few paragraphs on that.

Here is a dictionary class, which is one form of lookup object.

Dictionary<int, PointClass> dict = new Dictionary<int, PointClass>();

If the key is not a numeric, you can switch the declaration of the first
"generic":

Dictionary<string, PointClass> dict = new Dictionary<string, PointClass>();

You then load like so:

dict.Add(myPointClass.Id, myPointClass);

And pull like so:

PointClass p = dict[id];

The dictionary may not be the best choice, but you do end up needing a
key/value pair.

For example of other classes, if you need efficient lookups, based on
something that can be sorted, there is a SortedList.

SortedList<int, PointClass> sl = new SortedList<int, PointClass>();

Retrieval is basically identical.

Hope this helps!


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

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

Top