Click here to Skip to main content
15,917,176 members
Home / Discussions / C#
   

C#

 
AnswerRe: Guid as a Stored Proc parameter Pin
Colin Angus Mackay4-Feb-07 3:55
Colin Angus Mackay4-Feb-07 3:55 
QuestionCopying part of byte array to another byte array Pin
yarns4-Feb-07 2:00
yarns4-Feb-07 2:00 
AnswerRe: Copying part of byte array to another byte array Pin
Stefan Troschuetz4-Feb-07 2:55
Stefan Troschuetz4-Feb-07 2:55 
QuestionProblem with struct - Pin
yarns4-Feb-07 1:25
yarns4-Feb-07 1:25 
AnswerRe: Problem with struct - Pin
Guffa4-Feb-07 1:37
Guffa4-Feb-07 1:37 
GeneralRe: Problem with struct - Pin
yarns4-Feb-07 1:57
yarns4-Feb-07 1:57 
GeneralRe: Problem with struct - Pin
Guffa4-Feb-07 6:17
Guffa4-Feb-07 6:17 
AnswerRe: Problem with struct - Pin
Luc Pattyn4-Feb-07 6:36
sitebuilderLuc Pattyn4-Feb-07 6:36 
There probably are two problems:

1)
when you write byte[] you are declaring a reference type, you can think of it as a pointer
to some (yet unallocated) piece of memory. Such pointer needs 4 or 8 bytes of storage
(depending on you using Win32 or Win64) and independent of the amount of data you expect
your array to hold; also the pointer must be properly aligned (the offset
must be a multiple of 4, maybe of 8). Some of your arrays do not satisfy the alignment
requirment, and that explains the error code you are getting (its an align error,
not an overlap error).

2)
I suspect you really want your struct to hold some 25 bytes, and its layout to be
fixed (either by some hardware, or by a file format). If so, you must do it
differently; I see basically two possible approaches:

a) replace the struct by a single byte array, and take care of the offsets within your
code. This is the unstructured approach.

b) replace the struct by a combination of simple variables and other structs (which
again are limited to value types such as ints and value-only structs); and you could
and should teach those sub-structs how to interpret the data; could be something like (not correct, just indicative):

// a string holding max 8 ASCII chars, without terminating null
public struct string8 {
	public byte b0, b1, b2, b3, b4, b5, b6, b7;
		public string8(string s) {
		b0=0; b1=0; b2=0; b3=0; b4=0; b5=0; b6=0; b7=0;
		if (s==null) return;
		if (s.Length>=1) b0=(byte)s[0];
		if (s.Length>=2) b1=(byte)s[1];
		if (s.Length>=3) b2=(byte)s[2];
		if (s.Length>=4) b3=(byte)s[3];
		if (s.Length>=5) b4=(byte)s[4];
		if (s.Length>=6) b5=(byte)s[5];
		if (s.Length>=7) b6=(byte)s[6];
		if (s.Length>=8) b7=(byte)s[7];
	}
 
	public override string ToString() {
		StringBuilder sb=new StringBuilder(8);
		if (b0!=0) sb.Append((char)b0);
		if (b1!=0) sb.Append((char)b1);
		if (b2!=0) sb.Append((char)b2);
		if (b3!=0) sb.Append((char)b3);
		if (b4!=0) sb.Append((char)b4);
		if (b5!=0) sb.Append((char)b5);
		if (b6!=0) sb.Append((char)b6);
		if (b7!=0) sb.Append((char)b7);
		return sb.ToString();
	}
}
 
[StructLayout(LayoutKind.Explicit)]
public struct EF {
	[FieldOffset(0)] public byte clockStop;
	[FieldOffset(1)] public string8 s1;
	[FieldOffset(9)] public string8 s2;
}	
 
public override void Run(int arg) {
	EF e=new EF();
	e.s1=new string8("jef");
	e.s2=new string8("jan");
	log("names= "+e.s1+" "+e.s2);
}


This code produces the line "names= jef jan"

BTW: the ToString() method for e.s1 and e.s2 is called implicitly, due to
the string+something expression

remark: there may be easier ways to do the constructor and the ToString(), but
I cannot immediately think of one.

Hope this helps.

Smile | :)





Luc Pattyn

GeneralRe: Problem with struct - Pin
yarns4-Feb-07 7:58
yarns4-Feb-07 7:58 
QuestionWrapper Problem Pin
snouto4-Feb-07 1:19
snouto4-Feb-07 1:19 
AnswerRe: Wrapper Problem Pin
Luc Pattyn4-Feb-07 6:43
sitebuilderLuc Pattyn4-Feb-07 6:43 
QuestionDeployment and Distribution in .NET 1.1 Pin
MacMohan4-Feb-07 0:31
MacMohan4-Feb-07 0:31 
AnswerRe: Deployment and Distribution in .NET 1.1 Pin
Christian Graus4-Feb-07 0:40
protectorChristian Graus4-Feb-07 0:40 
AnswerRe: Deployment and Distribution in .NET 1.1 Pin
PIEBALDconsult4-Feb-07 4:36
mvePIEBALDconsult4-Feb-07 4:36 
QuestionHow to capture whole space of child form Pin
sjs4u3-Feb-07 23:44
sjs4u3-Feb-07 23:44 
QuestionSQl Server SP errors in C# Pin
Mridang Agarwalla3-Feb-07 21:45
Mridang Agarwalla3-Feb-07 21:45 
AnswerRe: SQl Server SP errors in C# Pin
Christian Graus4-Feb-07 0:30
protectorChristian Graus4-Feb-07 0:30 
QuestionWhat is the best way to reading and analizing binary file? Pin
yarns3-Feb-07 21:19
yarns3-Feb-07 21:19 
AnswerRe: What is the best way to reading and analizing binary file? Pin
Christian Graus4-Feb-07 0:29
protectorChristian Graus4-Feb-07 0:29 
AnswerRe: What is the best way to reading and analizing binary file? Pin
User 66584-Feb-07 1:16
User 66584-Feb-07 1:16 
QuestionHow to call forms using label or linklabel control Pin
sjs4u3-Feb-07 19:04
sjs4u3-Feb-07 19:04 
AnswerRe: How to call forms using label or linklabel control Pin
Christian Graus3-Feb-07 20:07
protectorChristian Graus3-Feb-07 20:07 
QuestionCatch or trap a process kill signal? Pin
Zerox MXI3-Feb-07 18:47
Zerox MXI3-Feb-07 18:47 
AnswerRe: Catch or trap a process kill signal? Pin
Stefan Troschuetz3-Feb-07 21:09
Stefan Troschuetz3-Feb-07 21:09 
GeneralRe: Catch or trap a process kill signal? Pin
Zerox MXI3-Feb-07 21:17
Zerox MXI3-Feb-07 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.