473,466 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Array of structs

2 New Member
I'm an engineer with a C background that is trying to do my first c# project.

I created a struct:
public struct MyStruct {
public static UInt32 bUsed;
public static UInt32 nCFreqHz;
public static UInt32 nDemIdx;
public static UInt32 nDemBWIdx;
public static UInt32 nVBFBWHz;
public static int nVBFFcHz;
public static long aTime;
public static char version;
public static char[] extDC;
public static char [] rsvd;
public static char [] desc;
} }

and then an array of structs with:
MyStruct[] MemoryBank = new MyStruct[100];

My code then does:
MemoryBank[MemoryIndex].bUsed = (r.ReadUInt32());

but the compiler objects with:
'WindowsFormsApplication1.Form1.My Struct.bUsed' cannot be accessed with an instance reference; qualify it with a type name instead

I take this to mean my array of structs is being treated as a class, not an array of structs. I've tried to Google this with no real luck and also tried a few fixes. Could someone educate me?

Thanks -

Chuck
Jul 15 '09 #1
3 6238
tlhintoq
3,525 Recognized Expert Specialist
No need for them to be static.

Might I suggest a "less C" and "more C#" way?

Expand|Select|Wrap|Line Numbers
  1. public class MyStruct 
  2. {
  3. public UInt32 bUsed; 
  4. public UInt32 nCFreqHz; 
  5. public UInt32 nDemIdx; 
  6. public UInt32 nDemBWIdx; 
  7. public UInt32 nVBFBWHz; 
  8. public int nVBFFcHz; 
  9. public long aTime; // This could probably be a DateTime instead of a long
  10. public char version; 
  11. public char[] extDC; 
  12. public char [] rsvd; 
  13. public char [] desc; 
  14. }
  15. }
  16.  
  17. List<MyStruct> MemoryBank = new List<MyStruct>();
  18.  
By making it a class rather than a struct you gain the capability of including methods inside the class. A class can then have constructor methods with parameters so you can have a lot of your initialization work take place as soon as it is created.

By use a List<> you gain several built-in methods that don't exist in an array such as RemoveAt(int Index). You also don't *have* to declare a size for the list as it will dynamically grow as needed.
Jul 15 '09 #2
charlesh3
2 New Member
Thanks for the pointer. The compiler doesn't like your class definition - on the first element (which is bUsed) it tells me "A get or set accessor expected".

And yes, I'd happily convert the struct to a class to make it work. I used a struct only because my data is just a bunch of numbers and I really don't have a need for the elegance of a class. But I have no moral ojection to wasting capabilities in orderto get the show on the road.


Chuck

P.S.

Here's the entire class definition that the compiler objects to:

public Class CUserMem {
public UInt32 bUsed; // supposed to be a Boolean but occupies 4 bytes
public UInt32 nCFreqHz; // Center Frequency
public UInt32 nDemIdx; // demod index: 4 or 5
public UInt32 nDemBWIdx; // normally 3 for 6 kHz
public UInt32 nVBFBWHz; // user selected filter BW (3.63 is a normal choice)
public int nVBFFcHz; // frequency offset of filter
// time_t aTime;
public long aTime; // long (epoch time when memory was created)
public char version; // 0 currently
public char[] extDC; // 3 bytes of external downconverter info
public char [] rsvd; // 4 bytes reserved
public char [] desc; // memory tag
}
Jul 15 '09 #3
Christian Binder
218 Recognized Expert New Member
@tlhintoq
As far as I know, also structs can have methods and Constructors (except the Standard-Constructor with no params)

The usage of structs could in some situations be quite useful, e.g. if passing the whole thing by value without having to fear, that your passed object would be changed as it could be when passing the object reference ...

But I agree with your opinion, that structs might not be used as often as classes.
Jul 16 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
1
by: mrhicks | last post by:
Hello all, I need some advice/help on a particular problem I am having. I have a basic struct called "indv_rpt_rply" that holds information for a particular device in our system which I will...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
5
by: dev_15 | last post by:
Hi, I'm going through some code and thought that this allocates an array of structs but its supposed according to comments to allocate an array of pointer to structs. What does it actually do ...
2
by: jonpb | last post by:
Using .NET 3.5, I need to pass an array of structs as parameter to a C++ unmanaged function. The C++ dll stores some data in an unmanaged cache, the function writes the values into the array of...
2
by: hal | last post by:
Hi, I'm trying to make an array of pointers to 'TwoCounts' structs, where the size of the array is arraySize. Right now I'm just mallocing enough space for all the pointers to the structs, and...
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...
1
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.