Source file with repetitive data
-
I want to have a bunch of byte arrays. They are commands which will be sent to the external device; generally across some sort of serial port (for now, at least). I want to put them in their own file. This is to keep the source files manageable from a human point of view. Pretty much, 300 lines has shown itself to be a very useful generally defining limit as one of the things that helps source code make sense. How do I put them in a file of their own which still allows them to be seen and used by the methods in the other files ? I'm getting errors like, "The name Such_And_So does not exist in the current context". My search for the answer lead me to a page on MSDN about "Scopes" (Found ^Here^ ) That page has what appears to be 15 rules on the scope of anything. I tried reading a couple of those rules. In order to understand the first thing about any one of those rules, you have to already be an expert in C# to begin with (in which case, you wouldn't need the rules in front of you in the first place). I just want to keep my source files orderly. How do I let the methods in one file see the data packets in another file ? The arrays will look something like this...
byte[] Command_Number___1 = new byte[] {
0xFF, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01
}
;byte[] Command_Number___2 = new byte[] {
0x0E, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02
}
;byte[] Command_Number___3 = new byte[] {
0xFD, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03
0x03
}
;byte[] Command_Number___4 = new byte[] {
0xFC, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04,
0x04, -
I want to have a bunch of byte arrays. They are commands which will be sent to the external device; generally across some sort of serial port (for now, at least). I want to put them in their own file. This is to keep the source files manageable from a human point of view. Pretty much, 300 lines has shown itself to be a very useful generally defining limit as one of the things that helps source code make sense. How do I put them in a file of their own which still allows them to be seen and used by the methods in the other files ? I'm getting errors like, "The name Such_And_So does not exist in the current context". My search for the answer lead me to a page on MSDN about "Scopes" (Found ^Here^ ) That page has what appears to be 15 rules on the scope of anything. I tried reading a couple of those rules. In order to understand the first thing about any one of those rules, you have to already be an expert in C# to begin with (in which case, you wouldn't need the rules in front of you in the first place). I just want to keep my source files orderly. How do I let the methods in one file see the data packets in another file ? The arrays will look something like this...
byte[] Command_Number___1 = new byte[] {
0xFF, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01
}
;byte[] Command_Number___2 = new byte[] {
0x0E, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02
}
;byte[] Command_Number___3 = new byte[] {
0xFD, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03
0x03
}
;byte[] Command_Number___4 = new byte[] {
0xFC, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04,
0x04, -
I want to have a bunch of byte arrays. They are commands which will be sent to the external device; generally across some sort of serial port (for now, at least). I want to put them in their own file. This is to keep the source files manageable from a human point of view. Pretty much, 300 lines has shown itself to be a very useful generally defining limit as one of the things that helps source code make sense. How do I put them in a file of their own which still allows them to be seen and used by the methods in the other files ? I'm getting errors like, "The name Such_And_So does not exist in the current context". My search for the answer lead me to a page on MSDN about "Scopes" (Found ^Here^ ) That page has what appears to be 15 rules on the scope of anything. I tried reading a couple of those rules. In order to understand the first thing about any one of those rules, you have to already be an expert in C# to begin with (in which case, you wouldn't need the rules in front of you in the first place). I just want to keep my source files orderly. How do I let the methods in one file see the data packets in another file ? The arrays will look something like this...
byte[] Command_Number___1 = new byte[] {
0xFF, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01
}
;byte[] Command_Number___2 = new byte[] {
0x0E, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02
}
;byte[] Command_Number___3 = new byte[] {
0xFD, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03
0x03
}
;byte[] Command_Number___4 = new byte[] {
0xFC, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04,
0x04,Not quite an answer to your question, but expanding on Richard's suggestion[^] of a static class, I also recommend the getter methods generate the command, rather than hard-coding them all. Of course, this depends on how your commands are structured. For instance, if each command includes the length of the data, or ends with a checksum or CRC, it's typically best to calculate that value and add it to the command, rather than hard-coding it. If all of your commands are completely different and don't have any common fields like this, then ignore this post :)
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
-
Not quite an answer to your question, but expanding on Richard's suggestion[^] of a static class, I also recommend the getter methods generate the command, rather than hard-coding them all. Of course, this depends on how your commands are structured. For instance, if each command includes the length of the data, or ends with a checksum or CRC, it's typically best to calculate that value and add it to the command, rather than hard-coding it. If all of your commands are completely different and don't have any common fields like this, then ignore this post :)
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
dybs wrote:
If all of your commands are completely different and don't have any common fields like this, then ignore this post
Really, I don't want the send routine to be locked into sort of any formatted thing of any kind. I want the user to click one button, and then I'll handle it from there; e.g., the number of bytes, when they are sent, etc. The commands will be who-knows-what size, and the routine should just send out that many bytes; plus, they can change in the next version, so, I don't want format to be an issue. I just want to do something along this line of thinking
private void Button1\_Click(object sender, EventArgs e) { try { Send\_This\_Out\_Our\_Chosen\_Serial\_Port(The\_Data\_For\_Button1); return; } catch (Exception ex) { MessageBox.Show(ex.ToString()); }
-
Put them in a static class[^] of their own with static getters, and ensure they are part of the namespace of the main application.
One of these days I'm going to think of a really clever signature.
Richard MacCutchan wrote:
Put them in a static class of their own with static getters, and ensure they are part of the namespace of the main application.
I'm trying to get an idea of all this. Tried the following, and the code in other files still can't see the names of these groups of data bytes; no matter if I declare them as public or static...
using System;
namespace The_Same_Namespace_As_The_Other_Files
{
public partial class Form1 : Form
{
public byte[] This_Does_Not_Work = new byte[] { 0x01, 0x02 };
}static class Class1 { static Class1(); public byte\[\] The\_Command\_Set\_01 = new byte\[\] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 } ; static byte\[\] Just\_A\_Test\_Set = new byte\[\] { 0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8, 0xF7, 0xF6, 0xF5 } ; }
}
What do I need to do to let my methods in other files see the group called The_Command_Set_01 which is in this file ?
-
Richard MacCutchan wrote:
Put them in a static class of their own with static getters, and ensure they are part of the namespace of the main application.
I'm trying to get an idea of all this. Tried the following, and the code in other files still can't see the names of these groups of data bytes; no matter if I declare them as public or static...
using System;
namespace The_Same_Namespace_As_The_Other_Files
{
public partial class Form1 : Form
{
public byte[] This_Does_Not_Work = new byte[] { 0x01, 0x02 };
}static class Class1 { static Class1(); public byte\[\] The\_Command\_Set\_01 = new byte\[\] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 } ; static byte\[\] Just\_A\_Test\_Set = new byte\[\] { 0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8, 0xF7, 0xF6, 0xF5 } ; }
}
What do I need to do to let my methods in other files see the group called The_Command_Set_01 which is in this file ?
Your code is not addressing the values in the class. Try this:
public partial class Form1 : Form { MessageBox.Show("Value is: {0}", Class1.Just\_A\_Test\_Set\[1\]); } static class Class1 { public static byte\[\] The\_Command\_Set\_01 = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }; public static byte\[\] Just\_A\_Test\_Set = { 0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8, 0xF7, 0xF6, 0xF5 }; }
One of these days I'm going to think of a really clever signature.
-
Your code is not addressing the values in the class. Try this:
public partial class Form1 : Form { MessageBox.Show("Value is: {0}", Class1.Just\_A\_Test\_Set\[1\]); } static class Class1 { public static byte\[\] The\_Command\_Set\_01 = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }; public static byte\[\] Just\_A\_Test\_Set = { 0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8, 0xF7, 0xF6, 0xF5 }; }
One of these days I'm going to think of a really clever signature.
Aha, so the word "public" can precede the word "static" on the same line. My education continues. Obfuscation rules the world. I just tried your idea. In my external file which holds the command bytes (Protocol_Stuff.cs) I have these two groups...
static class Class1 { static Class1(); public static byte\[\] BARN\_Command\_Hello = new byte\[\] { 0xFF, 0x00, 0x0B, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 } ; public static byte\[\] BARN\_Test = new byte\[\] { 0xFF, 0x00, 0x0B, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 } ; }
In the file "Form1.cs" I have this...
private void button2\_Click(object sender, EventArgs e) { MessageBox.Show("Value is: {0}", Class1.BARN\_Test\[1\]); }
When I choose "Build/Build", C# gives me this response...
Error 1 The name 'Class1' does not exist in the current context E:\YahYah\SoAndSo\Etc\Form1.cs 277 46 TheProgramName_03
So I say: DUH ! This is the problem that I've been trying to overcome for several days. How do I associate the stuff in one file with the stuff in another file ? e.g., in old-fashioned low-tech anachronistic assembly language (none dare mention speed or size) I would just put...
Extern: BARN_Test
...in the file that uses it and...
Public BARN_Test
...in the file that actually holds the label. The assembler would make the space for it and the linker would put them together. How do I do that same sort of procedure in C# with separate files ?
-
Aha, so the word "public" can precede the word "static" on the same line. My education continues. Obfuscation rules the world. I just tried your idea. In my external file which holds the command bytes (Protocol_Stuff.cs) I have these two groups...
static class Class1 { static Class1(); public static byte\[\] BARN\_Command\_Hello = new byte\[\] { 0xFF, 0x00, 0x0B, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 } ; public static byte\[\] BARN\_Test = new byte\[\] { 0xFF, 0x00, 0x0B, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 } ; }
In the file "Form1.cs" I have this...
private void button2\_Click(object sender, EventArgs e) { MessageBox.Show("Value is: {0}", Class1.BARN\_Test\[1\]); }
When I choose "Build/Build", C# gives me this response...
Error 1 The name 'Class1' does not exist in the current context E:\YahYah\SoAndSo\Etc\Form1.cs 277 46 TheProgramName_03
So I say: DUH ! This is the problem that I've been trying to overcome for several days. How do I associate the stuff in one file with the stuff in another file ? e.g., in old-fashioned low-tech anachronistic assembly language (none dare mention speed or size) I would just put...
Extern: BARN_Test
...in the file that uses it and...
Public BARN_Test
...in the file that actually holds the label. The assembler would make the space for it and the linker would put them together. How do I do that same sort of procedure in C# with separate files ?
-
It works fine for me. Did you put the correct
namespace
clause in yourClass1
file?One of these days I'm going to think of a really clever signature.
Cancel all questions. This post here [^] should detail the idiocy behind all this mystery. Hopefully, I'm going to write real code now. Thank you for loaning some brains on my behalf. I will try my best to avoid such a blunder in the future.