Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array declarations

Status
Not open for further replies.

xwb

Programmer
Jul 11, 2002
6,828
GB
I've looked on the net and in several books and I still can't figure out how to do this. In C, I would have done
Code:
#define OVERALL_SIZE 1400
#define ONE_SIZE (OVERALL_SIZE - 8)
struct One
{
   int onea;
   int oneb;
   unsigned char mdata[ONE_SIZE];
};
#define TWO_SIZE (OVERALL_SIZE - 4)
struct Two
{
   float twoa;
   unsigned char mdata[TWO_SIZE];
};
The best I've come up with is
Code:
static int OVERALL_SIZE = 1400;
static int ONE_SIZE = OVERALL_SIZE - 8;
static int TWO_SIZE = OVERALL_SIZE - 4;
class One
{
   public int onea;
   public int oneb;
   public byte mdata[ONE_SIZE];
};
class Two
{
   public float twoa;
   public byte mdata[TWO_SIZE];
};
Only problem is the static decls have to be in a class. If I put them in the classes, then the classes can't refer to each other. Any suggestions would be helpful.
 
Not sure waht you are trying to do with this?

You could always create a third class for the statics and make it
Code:
public class StaticInts

Are you asking how to declare an array?
public int[] VariableName;
 
I don't have C# now, but IMHO most of C# is similar to Java.
So I would first create a class with global constants and then use this class as parent class for structures-classes and the main public class. See here the Java example:

MyStructures.java
Code:
[COLOR=#a020f0]package[/color] examples;

[COLOR=#2e8b57][b]class[/b][/color] GlobalConstants{
  [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] OVERALL_SIZE = [COLOR=#ff00ff]15[/color];
  [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] ONE_SIZE = OVERALL_SIZE - [COLOR=#ff00ff]8[/color];
  [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] TWO_SIZE = OVERALL_SIZE - [COLOR=#ff00ff]4[/color];
}

[COLOR=#2e8b57][b]class[/b][/color] One [COLOR=#2e8b57][b]extends[/b][/color] GlobalConstants{
  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] onea;
  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] oneb;
  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]byte[/b][/color][] mdata = [COLOR=#804040][b]new[/b][/color] [COLOR=#2e8b57][b]byte[/b][/color][ONE_SIZE];
}

[COLOR=#2e8b57][b]class[/b][/color] Two [COLOR=#2e8b57][b]extends[/b][/color] GlobalConstants{
  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]float[/b][/color] twoa;
  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]byte[/b][/color][] mdata = [COLOR=#804040][b]new[/b][/color] [COLOR=#2e8b57][b]byte[/b][/color][TWO_SIZE];
}

[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] MyStructures [COLOR=#2e8b57][b]extends[/b][/color] GlobalConstants{
  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] main(String args[]) {
    [COLOR=#0000ff]// Create Instance of structure One[/color]
    One structOne = [COLOR=#804040][b]new[/b][/color] One();
    [COLOR=#0000ff]// Filling Values[/color]
    structOne.onea = [COLOR=#ff00ff]1[/color];
    structOne.oneb = [COLOR=#ff00ff]2[/color];
    [COLOR=#804040][b]for[/b][/color] ([COLOR=#2e8b57][b]int[/b][/color] i=[COLOR=#ff00ff]0[/color]; i<=ONE_SIZE-[COLOR=#ff00ff]1[/color]; i++){
      structOne.mdata[i]=([COLOR=#2e8b57][b]byte[/b][/color])(Math.random()*[COLOR=#ff00ff]255[/color]);
    }
    [COLOR=#0000ff]// Printing Values[/color]
    System.out.println([COLOR=#ff00ff]"Components of StructOne:"[/color]);
    System.out.format([COLOR=#ff00ff]"structOne.onea = %2d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], structOne.onea);
    System.out.format([COLOR=#ff00ff]"structOne.oneb = %2d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], structOne.oneb);
    System.out.format([COLOR=#ff00ff]"ONE_SIZE       = %2d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], ONE_SIZE);
    [COLOR=#804040][b]for[/b][/color] ([COLOR=#2e8b57][b]int[/b][/color] i=[COLOR=#ff00ff]0[/color]; i<=ONE_SIZE-[COLOR=#ff00ff]1[/color]; i++){
      System.out.format([COLOR=#ff00ff]"structOne.mdata[%02d] = %3d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], i, structOne.mdata[i]);
    }

    [COLOR=#0000ff]// Create Instance of the structure Two[/color]
    Two structTwo = [COLOR=#804040][b]new[/b][/color] Two();
    [COLOR=#0000ff]// Filling Values[/color]
    structTwo.twoa = [COLOR=#ff00ff]3[/color];
    [COLOR=#804040][b]for[/b][/color] ([COLOR=#2e8b57][b]int[/b][/color] i=[COLOR=#ff00ff]0[/color]; i<=TWO_SIZE-[COLOR=#ff00ff]1[/color]; i++){
      structTwo.mdata[i]=([COLOR=#2e8b57][b]byte[/b][/color])([COLOR=#2e8b57][b]byte[/b][/color])(Math.random()*[COLOR=#ff00ff]255[/color]);
    }
    [COLOR=#0000ff]// Printing Values[/color]
    System.out.println([COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]Components of StructTwo:"[/color]);
    System.out.format([COLOR=#ff00ff]"structTwo.twoa = %f[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], structTwo.twoa);
    System.out.format([COLOR=#ff00ff]"TWO_SIZE       = %2d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], TWO_SIZE);
    [COLOR=#804040][b]for[/b][/color] ([COLOR=#2e8b57][b]int[/b][/color] i=[COLOR=#ff00ff]0[/color]; i<=TWO_SIZE-[COLOR=#ff00ff]1[/color]; i++){
      System.out.format([COLOR=#ff00ff]"structTwo.mdata[%02d] = %3d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color], i, structTwo.mdata[i]);
    }

  }
}
Output:
Code:
Components of StructOne:
structOne.onea =  1
structOne.oneb =  2
ONE_SIZE       =  7
structOne.mdata[00] = -28
structOne.mdata[01] = -62
structOne.mdata[02] = -106
structOne.mdata[03] = 100
structOne.mdata[04] = -76
structOne.mdata[05] = -62
structOne.mdata[06] = 124

Components of StructTwo:
structTwo.twoa = 3,000000
TWO_SIZE       = 11
structTwo.mdata[00] = -82
structTwo.mdata[01] = -35
structTwo.mdata[02] = 100
structTwo.mdata[03] =  24
structTwo.mdata[04] = -59
structTwo.mdata[05] = -40
structTwo.mdata[06] =   2
structTwo.mdata[07] =  69
structTwo.mdata[08] = -93
structTwo.mdata[09] = -54
structTwo.mdata[10] =   8
So I guess, that if you change main() to Main() the code will probably work in C# too. But I'm not sure at 100%, because I don't have C# installed on this computer.
:)
 
The other thing to translate the above Java example in C# is to change System.out.println to System.Console.Write
 
Thanks - got one further problem: Marshalling. I'm importing some of these from C so I need to do something like
Code:
[StructLayout(LayoutKind.Sequential)]
public class One 
{
  public int onea;
  public int oneb;
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = ONE_SIZE)]
  public byte[] mdata = new byte[ONE_SIZE];
}

[StructLayout(LayoutKind.Sequential)]
public class Two 
{
  static int TWO_SIZE = OVERALL_SIZE - 4;
  public float twoa;
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = TWO_SIZE)]
  public byte[] mdata = new byte[TWO_SIZE];
}
I tried the inheritence thing but I can't use it with StructLayout.
It keeps on moaning about the SizeConst bit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top