🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.csharpbuilder.language.csharp
5 messages
5 total messages Started by Marko Binic Sun, 29 Oct 2006 10:45
Converting from Delphi (sets and streams)
#186
Author: Marko Binic
Date: Sun, 29 Oct 2006 10:45
22 lines
1661 bytes
Hi guys,
 I'm trying to convert a personal app from Delphi to C# but I'm having some
trouble finding a C# equivalent for sets. I've been paging through all C#
books I have here and just can't find it. What I want to do is declare a set
of f.ex. characters:

  const WhiteSpace = [#32, #10, #13, #9];

and check memership like this:

  if c in WhiteSpace then...

but I don't know how, using arrays is an alternative, but I need something
faster and easier to play with. Any ideas?

 Another problem I'm having is reading from a stream (System.IO.FileStream).
The BeginRead method takes a byte array, but I need to read words (2 bytes)
in order to compare some values, so is there something like BlockRead from
Delphi?

Regards
Marko
Re: Converting from Delphi (sets and streams)
#187
Author: Francois PIETTE [ICS - MidWare]
Date: Sun, 29 Oct 2006 12:35
39 lines
2572 bytes
> I'm trying to convert a personal app from Delphi to C# but I'm having some
> trouble finding a C# equivalent for sets. I've been paging through all C#
> books I have here and just can't find it. What I want to do is declare a 
> set
> of f.ex. characters:
>
>  const WhiteSpace = [#32, #10, #13, #9];
>
> and check memership like this:
>
>  if c in WhiteSpace then...
>
> but I don't know how, using arrays is an alternative, but I need something
> faster and easier to play with. Any ideas?

For enumerations, use an integer type and the bit wise operators.

> Another problem I'm having is reading from a stream 
> (System.IO.FileStream).
> The BeginRead method takes a byte array, but I need to read words (2 
> bytes)
> in order to compare some values, so is there something like BlockRead from
> Delphi?

There isn't. C#, and the whole dotnet, is a type safe environment. You have 
to rebuild each data type from the array of byte. In you example, to rebuild 
a word, you read the low byte, then the high vyte (assuming words are 
written in that order which is the native order for Intel processors), your 
word is the high byte multiplyed by 256 and added to the low byte.

You can derive from FileStream and implement methods to read various 
datatypes you need.

Contribute to the SSL Effort. Visit http://www.overbyte.be/eng/ssl.html
--
francois.piette@overbyte.be
The author for the freeware multi-tier middleware MidWare
The author of the freeware Internet Component Suite (ICS)
http://www.overbyte.be
Re: Converting from Delphi (sets and streams)
#188
Author: Marko Binic
Date: Sun, 29 Oct 2006 13:53
19 lines
1884 bytes
> For enumerations, use an integer type and the bit wise operators.

I know of that approach, but bitwise operations work with powers of 2, how 
would you do it with numbers like 9, 10, 13 and 32 - they have to be like 
that cause they're character codes ('\t', '\n', '\r', ' ') and using some 
constants would just reduce the speed of the program. Or am I wrong about 
this? How do Delphi sets work anyway (I mean internally, I know how to use 
them)?

> There isn't. C#, and the whole dotnet, is a type safe environment. You 
> have to rebuild each data type from the array of byte. In you example, to 
> rebuild a word, you read the low byte, then the high vyte (assuming words 
> are written in that order which is the native order for Intel processors), 
> your word is the high byte multiplyed by 256 and added to the low byte.

I thought so, thanks anyway.

Regards
Marko 
Re: Converting from Delphi (sets and streams)
#189
Author: Francois PIETTE [ICS - MidWare]
Date: Sun, 29 Oct 2006 15:15
18 lines
1760 bytes
>> For enumerations, use an integer type and the bit wise operators.
>
> I know of that approach, but bitwise operations work with powers of 2, how 
> would you do it with numbers like 9, 10, 13 and 32 - they have to be like 
> that cause they're character codes ('\t', '\n', '\r', ' ') and using some 
> constants would just reduce the speed of the program. Or am I wrong about 
> this? How do Delphi sets work anyway (I mean internally, I know how to use 
> them)?

For set of ordinal type, I think Delphi use the equivalent "case-of". Have a 
look at the assembly code generated by Delphi.


-- 
francois.piette@overbyte.be
The author for the freeware multi-tier middleware MidWare
The author of the freeware Internet Component Suite (ICS)
http://www.overbyte.be
Re: Converting from Delphi (sets and streams)
#190
Author: David Clegg
Date: Sun, 29 Oct 2006 19:06
21 lines
1635 bytes
Marko Binic wrote:

> Another problem I'm having is reading from a stream
> (System.IO.FileStream).  The BeginRead method takes a byte array, but
> I need to read words (2 bytes)

It sounds like you need the System.BitConverter class, in particular
the BitConverter.ToUInt16(byte[] value, int startIndex) method.

-- 
Cheers,
David Clegg
dclegg@gmail.com
http://cc.borland.com/Author.aspx?ID=72299

QualityCentral. The best way to bug Borland about bugs.
http://qc.borland.com

"I'm not a bad guy! I work hard, and I love my kids. So why should I
spend half my Sunday hearing about how I'm going to Hell?" - Homer
Simpson
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads