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

Generics and casting questions

Hello all,

First of all, sorry for multiple question per one thread. I have two
questions. First what I think might be the easier problem. I am
capturing data from an embedded device which I need to display data.
One particular piece of data is a 32 bit floating point value. The
problem I am having is converting the 4-byte value which is read via
an RS-232 link. How can I cast the 4-byte value to a value type float
correctly? I have tried placing shifting the bytes into either a int
or UInt32 (which is a class from what I understand) then casting that
value as a float, but it doesn't work correctly. This operation looks
similar to
int value = (int)((byteCode[0] << 24 ) |
(byteCode[1] << 16 ) |
(byteCode[2] << 8 ) |
(byteCode[3] ));

float valueF = (float)value;

This obviosuly doesn't appear to work correctly. The embedded device
stores the 32-bit float in the standard IEEE format from my current
understanding which should be compatable with the MS compiler. So how
would I get this conversion to work correctly?
The second question I have deals with using Generics. I have an
interface which looks like
interface IScriptVar<Twhere T : struct
{
string ToString();
string ToHexString( );
void SetValue( T value, UInt32 index );
void SetValue( T[] values, int index );
T GetValue( int index );
....
}

public class CScriptVar<T: IScriptVar<Twhere T : struct
{
... Implemented Interface methods and members
}

which I have also created a List for them

public class CScriptVarStringList<CScriptVarString:
IList<CScriptVarString>
{
....
}

Then in my code I have the following

private CScriptVarList<CScriptVar<byte> ScriptVarsBytes;
private CScriptVarList<CScriptVar<short> ScriptVarsShort;
private CScriptVarList<CScriptVar<int> ScriptVarsInts;
private CScriptVarList<CScriptVar<float> ScriptVarsFloat;

I have a method when passed in certain arguments it will return on the
of the objects. Since they all implement several interfaces I would
like to return the object and then cast it to the generic/general type
of IScriptVar<T>, but this does not work correctly. I have something
like the following currently, but I don't think it is right since I am
forced to qualify using a (struct or data types) byte, short, int or
float.

((IScriptVar<int>)scriptVar).ToHexString();

or

((CScriptVar<int>)scriptVar).ToHexString();
I was hoping to use a generic form, but perhaps this is not possible.
When I retrieve the object I could perform a typeof conditional then
switch between each one of the System.Type (struct or data types)
which would look like

if ( scriptVar.GetType() == typeof(IScriptVar<byte>))
{ ((CScriptVar<byte>)scriptVar).ToHexString(); }

if ( scriptVar.GetType() == typeof(IScriptVar<short>))
{ ((CScriptVar<short>)scriptVar).ToHexString(); }

if ( scriptVar.GetType() == typeof(IScriptVar<int>))
{ ((CScriptVar<int>)scriptVar).ToHexString(); }

if ( scriptVar.GetType() == typeof(IScriptVar<float>))
{ ((CScriptVar<float>)scriptVar).ToHexString(); }

Does anyone know of a elegant way of handling this or perhaps a better
approach.

Mark

Jul 18 '06 #1
3 1728
"LongBow" <pi***@mud.poola écrit dans le message de news:
qa********************************@4ax.com...

| Does anyone know of a elegant way of handling this or perhaps a better
| approach.

I would separate out the non-generic methods of the interface to a
non-generic base interface and then derive the generic interface from that :

interface IScriptVar
{
string ToString();
string ToHexString( );
}

interface IScriptVar<T: IScriptVar where T : struct
{
void SetValue( T value, UInt32 index );
void SetValue( T[] values, int index );
T GetValue( int index );
....
}

Then you can treat all instances the same as IScriptVar, allowing you to
access the ToString and ToHexString methods without having to know the exact
type.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jul 18 '06 #2
LongBow wrote:
First what I think might be the easier problem. I am
capturing data from an embedded device which I need to display data.
One particular piece of data is a 32 bit floating point value. The
problem I am having is converting the 4-byte value which is read via
an RS-232 link. How can I cast the 4-byte value to a value type float
correctly? I have tried placing shifting the bytes into either a int
or UInt32 (which is a class from what I understand) then casting that
value as a float, but it doesn't work correctly. This operation looks
similar to
int value = (int)((byteCode[0] << 24 ) |
(byteCode[1] << 16 ) |
(byteCode[2] << 8 ) |
(byteCode[3] ));

float valueF = (float)value;
This is at too high a level - this looks at the *number* in the int and
just makes valueF have the same *numerical* value. You want to be
working at the bit level.
>
This obviosuly doesn't appear to work correctly. The embedded device
stores the 32-bit float in the standard IEEE format from my current
understanding which should be compatable with the MS compiler. So how
would I get this conversion to work correctly?
Use BitConverter.ToSingle on the byte array.
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Jul 18 '06 #3
Larry Lard wrote:
LongBow wrote:
First what I think might be the easier problem. I am
capturing data from an embedded device which I need to display data.
One particular piece of data is a 32 bit floating point value. The
problem I am having is converting the 4-byte value which is read via
an RS-232 link. How can I cast the 4-byte value to a value type float
correctly? I have tried placing shifting the bytes into either a int
or UInt32 (which is a class from what I understand) then casting that
value as a float, but it doesn't work correctly. This operation looks
similar to
int value = (int)((byteCode[0] << 24 ) |
(byteCode[1] << 16 ) |
(byteCode[2] << 8 ) |
(byteCode[3] ));

float valueF = (float)value;

This is at too high a level - this looks at the *number* in the int and
just makes valueF have the same *numerical* value. You want to be
working at the bit level.

This obviosuly doesn't appear to work correctly. The embedded device
stores the 32-bit float in the standard IEEE format from my current
understanding which should be compatable with the MS compiler. So how
would I get this conversion to work correctly?

Use BitConverter.ToSingle on the byte array.
I had an additional question, why isn't type T allowed for the
BitConverter class? The Convert class allows type T to be used. Is
there a specific reason why? I have also noticed that bit wise
operations are not allowed for type T operation also. Why is the case?
I figure since the width of the data type is not known until run-time
so the bit wise operation doesn't know which opcode to use during
compile time.

Yes, thanks for both individuals who have replied as it has helped
greatly. I am still having problems the generics as bit-wise operations
are not allowed and I can't use the BitConverter class for some of the
operation I want.

Mark
>

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Jul 18 '06 #4

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

Similar topics

13
by: Anders Borum | last post by:
Hello! Now that generics are introduces with the next version of C#, I was wondering what kind of performance gains we're going to see, when switching from e.g. the general hashtable to a...
4
by: KC | last post by:
Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the...
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
4
by: krahenbuhl | last post by:
Dear All, I've a question related to Generics and casting in c# 2.0. I've a class called Client which implements interface IClient. I'd like to do: LinkedList<Client> clients; public...
5
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
7
by: Ajeet | last post by:
hi I am having some difficulty in casting using generics. These are the classes. public interface IProvider<PROF> where PROF : IProviderProfile { //Some properties/methods }
3
by: psyCK0 | last post by:
Hi all! I have a problem of casting generics to their base type. In the code below I first define a BaseList class that can hold items of any type that inherits from BaseItem. I then define a...
10
by: Frank Rizzo | last post by:
Given the inneficiencies of ArrayList and Hashtable on 64-bit systems, I am converting them to List<and Dictionary<respectively. It's a pretty massive system, so there are a lot of casts. For...
8
by: Tony Johansson | last post by:
Hello! I have read that in practice, casting proved to be several times faster than using a generic. So the main reason to use generics is not that the performance is better because that's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.