473,657 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arrays in structures

I understand that structures are value types and arrays and classes are
reference types. But what about arrays as members of structures i.e. as in
C

struct x
{
int n;
int a[100];
}

How is such a structure (e.g. declared in C++ as a field in a class)
accessed in C#?

Peter Seaman
Nov 15 '05 #1
33 3855
<"Peter Seaman" <Peter MS Seaman at StableSoftware. com>> wrote:
I understand that structures are value types and arrays and classes are
reference types. But what about arrays as members of structures i.e. as in
C

struct x
{
int n;
int a[100];
}

How is such a structure (e.g. declared in C++ as a field in a class)
accessed in C#?


You can't declare it like that in C# - you'd have to declare it as just

int[] a;

and initialise it in a constructor or similar. The struct itself is
still a value type, but the variable a would contain a reference. It's
exactly the same as if you have a struct containing a string reference
or similar.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
> and initialise it in a constructor or similar. The struct itself is
still a value type, but the variable a would contain a reference. It's
exactly the same as if you have a struct containing a string reference
or similar.


Value types live only on stack, but what with struct containing String
variable?
What will happen with this String when for example we leave function ?
Gawel
Nov 15 '05 #3
Gawelek <ga*****@NOSPAM EKpoczta.gazeta .pl> wrote:
and initialise it in a constructor or similar. The struct itself is
still a value type, but the variable a would contain a reference. It's
exactly the same as if you have a struct containing a string reference
or similar.
Value types live only on stack


No they don't. See
http://www.pobox.com/~skeet/csharp/memory.html
but what with struct containing String variable?
What will happen with this String when for example we leave function ?


It's the same as always: if there are no live references to the string,
it will eventually be garbage collected.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
> http://www.pobox.com/~skeet/csharp/memory.html

Thanks, now all is clear.

Gawel
Nov 15 '05 #5
Thanks, so structures containing directly embedded arrays are not possible
in C#. Such structures are possible in other languages such as C++ and I
often use them. What I am still uncertain about is this: if in C++ such a
structure is made a member of a class, can that member be accessed in C# and
if so how?

Peter Seaman
Nov 15 '05 #6
<"Peter Seaman" <Peter MS Seaman at StableSoftware. com>> wrote:
Thanks, so structures containing directly embedded arrays are not possible
in C#.
Not as far as I know, no.
Such structures are possible in other languages such as C++ and I
often use them. What I am still uncertain about is this: if in C++ such a
structure is made a member of a class, can that member be accessed in C# and
if so how?


Well, the first thing to do is test whether it is still possible in
*managed* C++ - and then run ildasm on the created assembly and see
what's in it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
> Well, the first thing to do is test whether it is still possible in
*managed* C++ - and then run ildasm on the created assembly and see
what's in it.


No doubt this is good advice but unfortunately I only have C# Standard.
Though this product does contain Visual Studio 2003 and compilers for
JScript .NET and VB .NET, it does not allow me to compile C++ .NET as far as
I can see.

It seems to me that arrays and structures and combinations of such
aggregates are pretty fundamental to programming, so on the face of this
inability for structures to contain embedded arrays is a severe restriction.
Is there an easy way round it? How does C# cope with the many such
structures in the WIN32 API e.g. LPWIN32_FIND_DA TA?

Peter Seaman

Nov 15 '05 #8
<"Peter Seaman" <Peter MS Seaman at StableSoftware. com>> wrote:
Well, the first thing to do is test whether it is still possible in
*managed* C++ - and then run ildasm on the created assembly and see
what's in it.
No doubt this is good advice but unfortunately I only have C# Standard.
Though this product does contain Visual Studio 2003 and compilers for
JScript .NET and VB .NET, it does not allow me to compile C++ .NET as far as
I can see.


Ah. I'm afraid I don't know much managed C++ at all - perhaps another
poster with more experience of it could help us?
It seems to me that arrays and structures and combinations of such
aggregates are pretty fundamental to programming, so on the face of this
inability for structures to contain embedded arrays is a severe restriction.
Is there an easy way round it? How does C# cope with the many such
structures in the WIN32 API e.g. LPWIN32_FIND_DA TA?


No idea, I'm afraid :(

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9

"Peter Seaman" <Peter MS Seaman at StableSoftware. com> wrote in message
news:uI******** *****@TK2MSFTNG P11.phx.gbl...
Well, the first thing to do is test whether it is still possible in
*managed* C++ - and then run ildasm on the created assembly and see
what's in it.


No doubt this is good advice but unfortunately I only have C# Standard.
Though this product does contain Visual Studio 2003 and compilers for
JScript .NET and VB .NET, it does not allow me to compile C++ .NET as far
as
I can see.

It seems to me that arrays and structures and combinations of such
aggregates are pretty fundamental to programming, so on the face of this
inability for structures to contain embedded arrays is a severe
restriction.
Is there an easy way round it? How does C# cope with the many such
structures in the WIN32 API e.g. LPWIN32_FIND_DA TA?

Peter Seaman


This is not such a problem in C#, following is a sample using FindNexttFile
and WIN32_FIND_DATA .

using System;
using System.Runtime. InteropServices ;
namespace Willys
{

[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Unicode )]
public struct _WIN32_FIND_DAT AW
{
public uint dwFileAttribute s;
public FILETIME ftCreationTime;
public FILETIME ftLastAccessTim e;
public FILETIME ftLastWriteTime ;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
// TCHAR array 260 (MAX_PATH) entries, 520 bytes in unicode
[MarshalAs(Unman agedType.ByValT Str, SizeConst=520)]
public string cFileName;
// TCHAR array 14 TCHAR's alternate filename 28 byes in unicode
[MarshalAs(Unman agedType.ByValT Str, SizeConst=28)]
public string cAlternateFileN ame;
}

[StructLayout(La youtKind.Sequen tial)]
public struct FILETIME
{
public uint dwLowDateTime;
public uint dwHighDateTime;
}
class Tester
{
[DllImport("kern el32.dll", EntryPoint="Fin dFirstFileW", SetLastError=tr ue,
CharSet = CharSet.Unicode )]
public static extern IntPtr FindFirstFileW( string lpFileName, ref
_WIN32_FIND_DAT AW lpFindFileData) ;
static void Main()
{
string file = "*.exe";
_WIN32_FIND_DAT AW fndData = new _WIN32_FIND_DAT AW();
IntPtr hFile = FindFirstFileW( file, ref fndData);
if(hFile.ToInt3 2() == -1)
Console.WriteLi ne("FindFirst: " + Marshal.GetLast Win32Error());
else
Console.WriteLi ne("Filename: " + fndData.cFileNa me_260);

}
}
}

Willy.
Nov 15 '05 #10

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

Similar topics

3
4841
by: Java script Dude | last post by:
Some programmers prefer to stay with native level data structures such as string arrays instead of using Object based data structures such as ArrayList. From and efficiency point of view. Are string arrays the most efficient way of dealing with static tabular data such as query returns? or is it more efficient to use a Recordset type data structure using say ArrayList with a Record objects? (Pardon my JDBC ignorance)
7
2453
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to *(MyArray+x), what statement is functionally identical to MyArray? I ask this question because when I create a dynamic array with one
1
1457
by: ORC | last post by:
Hi, I have posted almost the same message in the compact framework group, but it seems to got lost in a deep message structure :-) so I'll try again: I'll receive a byte array from a serial port. The byte arrays are actually serialized structures with alignments of 4 (pack = 4). The structures were originally written in C++ and looks e.g. like this: struct Parameters { double CreatedDate;
2
1822
by: Andrew Burlak | last post by:
Hi, Although Unsized Arrays inside structures is a non-standard feature, I like to use it :-) It seems that Unsized Arrays are broken when /Og (Global Optimization) is enabled. The problem occurs whenever the unsized array is declared as 'const'. Here is an example:
1
2521
by: Raj | last post by:
Hi, This is regarding using Arrays of UDT's in COM. The following is a short description: - We basically need a structure, struct MAIN_STRUCT which must contain a dynamic array of another struct, struct SUB_STRUCT. We have a interface function that is callable by the client, which takes a dynamic array of MAIN_STRUCT as parameter. So the problem is wrt passing around dynamic
1
1402
by: John Dann | last post by:
Is it possible in VB.net to have arrays of structures (ie UDTs) where the structure definition includes one or more arrays? Something like: Structure MyUDT ItemOne () as byte 'for example sub new(byval dimension as integer) redim itemone(dimension) end sub End structure
10
4984
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I get an error: ---------------- An unhandled exception of type 'System.NullReferenceException' occured in
0
1356
by: Art Cummings | last post by:
Good morning all. I just finished an assignment using structures. In this assignment I used an array of structures. What I would have liked was to use an array of structures with a function. The problem I ran into, was when the i tried to read the data in the structure I got garbage. Normally when I use arrays they retain the information that I enter once the function completes. I'm thinking that it has something to do with using the...
127
4861
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.