473,396 Members | 2,013 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.

Fixed-length arrays as struct member

Hi NG,
is it possible for struct to have a fixed-length array of primitive
types as member variable as in C++, such that the array doesn't need
to be allocated explicitly?

Eg.:
--------
public const int MAX_VALUES = 4;

public struct Weight
{
int indexes[MAX_VALUES];
float weights[MAX_VALUES];
}

Weight[] weights = new Weight[10];

// Access the array
for(int i = 0; i < MAX_VALUES; i++)
weights[0].indexes[i] = -1;

--------
Rather than:

Weight[] weights = new Weight[10];

foreach(Weight w in weights)
w.indexes = new int[MAX_VALUES];
... and so on ...

// Access the array

The latter is very cumbersome method in my opinion.
Nov 16 '05 #1
3 5287

In v2 you'll be able to write something like

unsafe struct Weight
{
fixed int indexes[MAX_VALUES];
fixed float weights[MAX_VALUES];
}

But since it only works with unsafe code enabled, I think it's rarely
worth using.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
On Mon, 21 Jun 2004 01:09:14 +0200, Mattias Sjögren
<ma********************@mvps.org> wrote:

In v2 you'll be able to write something like

unsafe struct Weight
{
fixed int indexes[MAX_VALUES];
fixed float weights[MAX_VALUES];
}

But since it only works with unsafe code enabled, I think it's rarely
worth using.

Mattias


Actually, I need to use unsafe code in other places too, so it's not a
problem. Does v2 mean .NET Framework v1.1?
Nov 16 '05 #3
Actually, I need to use unsafe code in other places too, so it's not a
problem. Does v2 mean .NET Framework v1.1?


No, it means the upcoming v2.0. But it's just a syntactic shortcut for
the following, which you can write in current versions:

[StructLayout(LayoutKind.Auto, Size=4*4+4*4)]
unsafe struct Weight
{
public int GetIndex(int i)
{
fixed (Weight* pw = &this)
return ((int*)pw)[i];
}

public void SetIndex(int i, int val)
{
fixed (Weight* pw = &this)
((int*)pw)[i] = val;
}

public float GetWeight(int i)
{
fixed (Weight* pw = &this)
return ((float*)((byte*)pw + 16))[i];
}

public void SetWeight(int i, float val)
{
fixed (Weight* pw = &this)
((float*)((byte*)pw + 16))[i] = val;
}
}

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #4

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

Similar topics

1
by: Jaz | last post by:
Trying to use a fixed layer for a couple of NAV buttons. I found this code, but the IE part is commented, and I don't understand the IF statement. It works great on Moz/Firebird and Opera BUT...
179
by: SoloCDM | last post by:
How do I keep my entire web page at a fixed width? ********************************************************************* Signed, SoloCDM
9
by: Paul Trautwein | last post by:
I'm trying to get an image to float in a window despite scrolling. I've gotten it to work on my Mac using IE 5.2, Netscape, and Safari, but it goes wonky when I test it on a PC. (testing with IE...
6
by: Mason A. Clark | last post by:
Masters: On two or three-column layouts, one column often has a list of links. Scrolling the page hides them. I'm aware there's supposed to be the ability to fix the column (frame-like). I...
4
by: Peter Fjelsten | last post by:
Guys at comp.infosystems.www.authoring.stylesheets, I have designed a page in (x)HTML transitional that I am happy with in (close to) standard compliant browsers (i.e. Firebird/Opera), but IE...
5
by: Ted Mayett | last post by:
I've read a lot through the google archives, but I cannot seem to find an example... Has someone successfully made css code that allows for position: fixed to work in IE 6.0.2800? What is the...
9
by: pout | last post by:
What are the purposes of fixed-point? When should it be used? I read: #define Int2Fixed(x) (((long)(short)x) << 16) and the fixed-point in 16.16 format. Does the 16 in the MACRO refer to...
4
by: Otie | last post by:
Hello, I am using the MSFlexGrd Control in VB5. I have 1 fixed row and one fixed column. I am trying to do a sort when the user clicks a column in the FIXED ROW. But when I capture the row...
1
by: O.B. | last post by:
In the example below, I'm trying to convert a fixed byte array to a string. I get an error about needing to use "fixed" but I have no clue where to apply it. Help? using System; using...
4
by: Jeff | last post by:
Hey I'm wondering how the Fixed-Width Text Format is What I know is that the top line in this text format will contain column names. and each row beneath the top line represent for example a...
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.