Struct
-
I'm trying something new, but not quite sure how to do it. I have a class for modeling data that contains three types of data - persistant data, data that is semi-persistant, and data that is updated constantly. I would like to have each defined in a struct defined inside my data model class, like so: (This is obviously incorrect syntax, but I can't figure out how do it correctly)
public class DataModel { private struct PersistantData { int m_int1; int m_int2; }m_PersistantData; // I WOULD LIKE TO DEFINE AND INSTANTIATE THE STRUCT RIGHT HERE, INSIDE THE CLASS. IS THIS POSSIBLE? private struct SemiPersistantData { int m_int3; int m_int4; }m_SemiPersistantData; private struct DynamicData { int m_int5; int m_int6; } public UpdatePersistantData(int int1, int int2) { m_PersistantData.m_int1 = int1; m_PersistantData.m_int2 = int2; } ... }
Obviously, thats not the actual program, but it shows how I would like to declare and use the structs. Is this possible? -
I'm trying something new, but not quite sure how to do it. I have a class for modeling data that contains three types of data - persistant data, data that is semi-persistant, and data that is updated constantly. I would like to have each defined in a struct defined inside my data model class, like so: (This is obviously incorrect syntax, but I can't figure out how do it correctly)
public class DataModel { private struct PersistantData { int m_int1; int m_int2; }m_PersistantData; // I WOULD LIKE TO DEFINE AND INSTANTIATE THE STRUCT RIGHT HERE, INSIDE THE CLASS. IS THIS POSSIBLE? private struct SemiPersistantData { int m_int3; int m_int4; }m_SemiPersistantData; private struct DynamicData { int m_int5; int m_int6; } public UpdatePersistantData(int int1, int int2) { m_PersistantData.m_int1 = int1; m_PersistantData.m_int2 = int2; } ... }
Obviously, thats not the actual program, but it shows how I would like to declare and use the structs. Is this possible? -
I'm trying something new, but not quite sure how to do it. I have a class for modeling data that contains three types of data - persistant data, data that is semi-persistant, and data that is updated constantly. I would like to have each defined in a struct defined inside my data model class, like so: (This is obviously incorrect syntax, but I can't figure out how do it correctly)
public class DataModel { private struct PersistantData { int m_int1; int m_int2; }m_PersistantData; // I WOULD LIKE TO DEFINE AND INSTANTIATE THE STRUCT RIGHT HERE, INSIDE THE CLASS. IS THIS POSSIBLE? private struct SemiPersistantData { int m_int3; int m_int4; }m_SemiPersistantData; private struct DynamicData { int m_int5; int m_int6; } public UpdatePersistantData(int int1, int int2) { m_PersistantData.m_int1 = int1; m_PersistantData.m_int2 = int2; } ... }
Obviously, thats not the actual program, but it shows how I would like to declare and use the structs. Is this possible?budidharma wrote:
private struct PersistantData { int m_int1; int m_int2; }m_PersistantData; // I WOULD LIKE TO DEFINE AND INSTANTIATE THE STRUCT RIGHT HERE, INSIDE THE CLASS. IS THIS POSSIBLE?
You're doing two things at once. private struct PersistantData { int m_int1; int m_int2; } PersistantData m_PersistantData; One more thing, unlike C++, C# structs have default access as private, so you'd need to make these int's public if you wanted to use them for anything :-) Christian Graus - Microsoft MVP - C++
-
Plainly because they are for use only by the class, in it's internal workings. Christian Graus - Microsoft MVP - C++
-
budidharma wrote:
private struct PersistantData { int m_int1; int m_int2; }m_PersistantData; // I WOULD LIKE TO DEFINE AND INSTANTIATE THE STRUCT RIGHT HERE, INSIDE THE CLASS. IS THIS POSSIBLE?
You're doing two things at once. private struct PersistantData { int m_int1; int m_int2; } PersistantData m_PersistantData; One more thing, unlike C++, C# structs have default access as private, so you'd need to make these int's public if you wanted to use them for anything :-) Christian Graus - Microsoft MVP - C++
Thanks. I knew I could declare them like that, which is the way it's currently written, I was hoping for a shortcut to define and declare a single instance, since each class simply needs a single instance of each. Not necessary, just a little elegant. And of course, you're correct - they are ONLY used within the internal structure of the class. I'm simply separating the data into catagories that they belong in.
-
I'm trying something new, but not quite sure how to do it. I have a class for modeling data that contains three types of data - persistant data, data that is semi-persistant, and data that is updated constantly. I would like to have each defined in a struct defined inside my data model class, like so: (This is obviously incorrect syntax, but I can't figure out how do it correctly)
public class DataModel { private struct PersistantData { int m_int1; int m_int2; }m_PersistantData; // I WOULD LIKE TO DEFINE AND INSTANTIATE THE STRUCT RIGHT HERE, INSIDE THE CLASS. IS THIS POSSIBLE? private struct SemiPersistantData { int m_int3; int m_int4; }m_SemiPersistantData; private struct DynamicData { int m_int5; int m_int6; } public UpdatePersistantData(int int1, int int2) { m_PersistantData.m_int1 = int1; m_PersistantData.m_int2 = int2; } ... }
Obviously, thats not the actual program, but it shows how I would like to declare and use the structs. Is this possible? -
You can't declare a struct inside a class, the same way you can't declare a struct inside a struct or a class inside a class. --- b { font-weight: normal; }
:omg: You can declare classes and structs inside classes, as well as classes and structs inside structs. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
:omg: You can declare classes and structs inside classes, as well as classes and structs inside structs. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
You can't declare a struct inside a class, the same way you can't declare a struct inside a struct or a class inside a class. --- b { font-weight: normal; }
... Yes, you can definately declare a struct inside a class.
-
... Yes, you can definately declare a struct inside a class.