mixed struct with constant and variable data

  • Thread starter markus wolfgart
  • Start date
M

markus wolfgart

Hi NG,

I would like to create a struct of mixed content
of dynamic data and constant data like below:

public struct PFD_Order_T
{

// key names of order_file // constant content
public const string key_order_id = "order_id";

// content of order file // dynymic content
public string order_id;

}

When compiling this structure with vs2005 I get no errors.
When defining a class with a structured data like above

public partial class Form1 : Form
{

PFD_Order_T PfdOrder;
....
}

and creating some functions/procedures which use PFD_Order_T as
input Parameters (second function)

public partial class Form1 : Form
{

PFD_Order_T PfdOrder;

private string extract_from_order_line(string line, string key)
{
if (line.Contains(key))
{
int index = line.IndexOf(key);
index++; // skip "="
return line.Substring(index, line.Length - index);
}
return null;
}

private void parse_order_file(string filename, PFD_Order_T pfdorder)
{
string[] fileLines = SplitFileByLine(filename,"\r\n");
string fileLine = "";
for (int index = 0; index < fileLines.Length; index++)
{
fileLine = fileLines[index].Trim();
pfdorder.order_id =
extract_from_order_line(fileLine,pfdorder.key_order_id); <=== My problem
...
}
}
}

I could not select my constant part of my struct like I demonstrate
above. Means pfdorder.key_order_id is for me not visible in vs2005 where
as pfdorder.order_id could be selected.

Any hints whats wrong with my definition of the struct PFD_Order_T.

The idea behind this constellation of abstract data is to have a
unchanged part of data like an item and the corresponding dynamic
data. I will use this for parsing a file in order to extract a value
located behind the item defined in .key_order_id.

Many thanks in advance for your help

Markus Wolfgart
 
A

Alberto Poblacion

markus wolfgart said:
public struct PFD_Order_T
{
[...]
public const string key_order_id = "order_id";
[...]
PFD_Order_T PfdOrder;
[...]
pfdorder.order_id =
extract_from_order_line(fileLine,pfdorder.key_order_id); <=== My problem
[...]
I could not select my constant part of my struct like I demonstrate above.
Means pfdorder.key_order_id is for me not visible in vs2005 where
as pfdorder.order_id could be selected.

Any hints whats wrong with my definition of the struct PFD_Order_T.

Nothing wrong with the struct, but with the code that accesses the
constant. The "const" is automatically considered "static", which means that
you have to access it like a static member, using the name of the struct
instead of the instance:

PFD_Order_T.key_order_id instead of pfdorder.key_order_id.
 
M

markus wolfgart

Alberto said:
markus wolfgart said:
public struct PFD_Order_T
{
[...]
public const string key_order_id = "order_id";
[...]
PFD_Order_T PfdOrder;
[...]
pfdorder.order_id =
extract_from_order_line(fileLine,pfdorder.key_order_id); <=== My problem
[...]
I could not select my constant part of my struct like I demonstrate
above. Means pfdorder.key_order_id is for me not visible in vs2005 where
as pfdorder.order_id could be selected.

Any hints whats wrong with my definition of the struct PFD_Order_T.

Nothing wrong with the struct, but with the code that accesses the
constant. The "const" is automatically considered "static", which means
that you have to access it like a static member, using the name of the
struct instead of the instance:

PFD_Order_T.key_order_id instead of pfdorder.key_order_id.
Thanks Alberto for your reply.

My understanding was that every object of type PFD_Order_T created by
the new operator is a clone of my struct 'PFD_Order_T' placed in memory.
Is in this case every constant part of my struct only one time allocated
in the memory, whereas the dynamic part will be created every time I use
the new operator?

My intension was to bundle the keys for data extraction with the
structure where my dynamic data will be stored.

But when I have to use an other name to access my keys then to access my
data, which sense makes it to encapsulate my keys and my data in one
structure.

Should I separate the constant keys (patterns) from my extracted data?


Bye

Markus
 
A

Alberto Poblacion

markus wolfgart said:
My understanding was that every object of type PFD_Order_T created by
the new operator is a clone of my struct 'PFD_Order_T' placed in memory.
Is in this case every constant part of my struct only one time allocated
in the memory, whereas the dynamic part will be created every time I use
the new operator?

Yes, the dynamic parts are allocated each time, but since the "const" is
invariable, it is treated as "static", that is, it is allocated a single
time.
My intension was to bundle the keys for data extraction with the
structure where my dynamic data will be stored.

But when I have to use an other name to access my keys then to access my
data, which sense makes it to encapsulate my keys and my data in one
structure.

It would make sense if you wanted to encapsulate the whole functionality
together: the data, the keys, and the method that operate on the data and
keys. However, this is more typically done with a class instead of a struct.
Should I separate the constant keys (patterns) from my extracted data?

Possibly, depending on how your program is intended to operate. It may
make sense to create a "decoder" class which has the const keys and the
methods that access the data by means of those keys, and then create a very
simple separate struct to contain only the data values upon which the class
methods operate. These stucts would not contain any methods or constants.
The class could be entirely static, if it doesn't need to maintain any
state.
 
M

markus wolfgart

Alberto said:
Yes, the dynamic parts are allocated each time, but since the "const"
is invariable, it is treated as "static", that is, it is allocated a
single time.


It would make sense if you wanted to encapsulate the whole
functionality together: the data, the keys, and the method that operate
on the data and keys. However, this is more typically done with a class
instead of a struct.


Possibly, depending on how your program is intended to operate. It may
make sense to create a "decoder" class which has the const keys and the
methods that access the data by means of those keys, and then create a
very simple separate struct to contain only the data values upon which
the class methods operate. These stucts would not contain any methods or
constants. The class could be entirely static, if it doesn't need to
maintain any state.

Tanks Alberto for your opinion and hints.

My intension was to create a list of objects, representing orders
in a production system with different stati named
new,running,finished,failed, so my struct is chained to this list and
I has to maintain the status of each order according to the real status
in this system.

I will now design a constant static class or struct with my extraction
keys and the extraction methods and embed this class in a outer class
which store the dynamic data and provide the methods to read them.

This outer class encapsulate the status of each order as a read/write
variable and the corresponding method for manipulating it.


Bye

Markus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top