Click here to Skip to main content
15,904,935 members
Home / Discussions / C#
   

C#

 
GeneralRe: OPC client server Pin
Dave Kreskowiak5-Oct-18 2:16
mveDave Kreskowiak5-Oct-18 2:16 
GeneralRe: OPC client server Pin
Roberto64_Ge8-Oct-18 2:16
Roberto64_Ge8-Oct-18 2:16 
GeneralRe: OPC client server Pin
Dave Kreskowiak8-Oct-18 4:10
mveDave Kreskowiak8-Oct-18 4:10 
GeneralRe: OPC client server Pin
Roberto64_Ge8-Oct-18 4:45
Roberto64_Ge8-Oct-18 4:45 
GeneralRe: OPC client server Pin
Dave Kreskowiak8-Oct-18 8:21
mveDave Kreskowiak8-Oct-18 8:21 
QuestionActive touchscreen detection Pin
sarmadi2-Oct-18 19:43
sarmadi2-Oct-18 19:43 
QuestionHow should JSON Deserializer work ? (basic question). FIXED. Pin
Maximilien2-Oct-18 9:04
Maximilien2-Oct-18 9:04 
QuestionAsync Memory leak ... Pin
Super Lloyd1-Oct-18 21:13
Super Lloyd1-Oct-18 21:13 
AnswerRe: Async Memory leak ... Pin
Nathan Minier2-Oct-18 1:31
professionalNathan Minier2-Oct-18 1:31 
QuestionSqlite DB Verification Pin
Member 1394838230-Sep-18 20:24
Member 1394838230-Sep-18 20:24 
AnswerRe: Sqlite DB Verification Pin
Richard MacCutchan30-Sep-18 21:11
mveRichard MacCutchan30-Sep-18 21:11 
AnswerRe: Sqlite DB Verification Pin
OriginalGriff30-Sep-18 21:17
mveOriginalGriff30-Sep-18 21:17 
QuestionBeginning Student Array Question Pin
Member 1400299730-Sep-18 14:41
Member 1400299730-Sep-18 14:41 
AnswerRe: Beginning Student Array Question Pin
Dave Kreskowiak30-Sep-18 17:56
mveDave Kreskowiak30-Sep-18 17:56 
AnswerRe: Beginning Student Array Question Pin
OriginalGriff30-Sep-18 19:51
mveOriginalGriff30-Sep-18 19:51 
QuestionPython httpServ.request in C# - HttpWebRequest ? Pin
Member 1400288230-Sep-18 9:59
Member 1400288230-Sep-18 9:59 
AnswerRe: Python httpServ.request in C# - HttpWebRequest ? Pin
Richard MacCutchan30-Sep-18 21:08
mveRichard MacCutchan30-Sep-18 21:08 
GeneralRe: Python httpServ.request in C# - HttpWebRequest ? Pin
Member 140028821-Oct-18 11:26
Member 140028821-Oct-18 11:26 
QuestionSwitching 2 Variables Pin
Member 1400283730-Sep-18 8:19
Member 1400283730-Sep-18 8:19 
AnswerRe: Switching 2 Variables Pin
Dave Kreskowiak30-Sep-18 8:53
mveDave Kreskowiak30-Sep-18 8:53 
GeneralRe: Switching 2 Variables Pin
Member 1400283730-Sep-18 10:55
Member 1400283730-Sep-18 10:55 
AnswerRe: Switching 2 Variables Pin
BillWoodruff14-Oct-18 15:35
professionalBillWoodruff14-Oct-18 15:35 
QuestionFundamental misunderstanding of structs as "value" types Pin
bh_28-Sep-18 3:54
bh_28-Sep-18 3:54 
Dear fellow C# programmers.

I am trying to model a command that I will send to a product. The command payload consists of:

Header   (byte)
Command  (byte)
Length   (byte)
Data     (byte[])
Checksum (byte[2])


Now, the values for the header and command are always static for a particular function. The user only needs to change the data directly.
Indirectly, the Length and Checksum properties will change, but I have written methods which automatically calculate these upon any change of Data.

My idea is I can define a set of these commands as a template to accommodate all the different functions. The user can then use the commands, setting their own data as necessary.

When a user "uses" a command, I don't want them to change the Data property in the template commands. I want the user to create a clone of the command, such that when they change the Data property then these changes are only present in their local instance of the command.

I thought I understood that I could model the command as a struct, so that each command would be a "value type".
Then, when I do:

C#
Command DestroyProduct = new Command(byte header, byte command, byte[] data);
...
...
Command LetOutMagicSmoke = new Command();
LetOutMagicSmoke = DestroyProduct;
LetOutMagicSmoke.Data = new byte[] {0x00, 0x01, etc};


Then the changes to Data are only present in LetOutMagicSmoke and not in DestroyProduct.

This is not how the code is behaving. Instead, changes made to LetOutMagicSmoke.Data are also made to the template command.

The only explanation I can think of is that since byte[] is a reference type, then when I say LetOutMagicSmoke = DestroyProduct; the byte arrays are copied by reference.

This is not the behaviour I want!

My next idea is to put some code in the 'getter' for the Command which iterates over the byte array, extracting out all the bytes, and then putting them into a new byte array, before returning the command to the user.

I haven't implemented that yet but I would like to get my understanding of this issue hashed out first.
Have I understood what is going on correctly? Is my next idea the best way forwards?

Many thanks.
AnswerRe: Fundamental misunderstanding of structs as "value" types Pin
OriginalGriff28-Sep-18 4:12
mveOriginalGriff28-Sep-18 4:12 
GeneralRe: Fundamental misunderstanding of structs as "value" types Pin
bh_28-Sep-18 4:49
bh_28-Sep-18 4:49 

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.