class question
-
public class NewMonitorDetails { public static int MonNumber; public static string EDID_Info; public NewMonitorDetails(){} }
I have the above class...I want to create an array of the above class to store different values in MonNumber and EDID_Info, so that I can access those values in other calsses where ever I want. How do I do this. Thanks. -
public class NewMonitorDetails { public static int MonNumber; public static string EDID_Info; public NewMonitorDetails(){} }
I have the above class...I want to create an array of the above class to store different values in MonNumber and EDID_Info, so that I can access those values in other calsses where ever I want. How do I do this. Thanks.Manu_81 wrote:
I want to create an array of the above class to store different values in MonNumber and EDID_Info, so that I can access those values in other calsses where ever I want
First, if you declare the fields as
static
you will only get one for all instances of the class. So, if you want to create multiple instances of the class you cannot create these asstatic
for the purpose you want. Second, declaring a field as public is bad form. You should declare them as private or protected and access them through properties. Read this article if you want to know why you should make the fields private and access them through a public property[^] Finally, you can make the array global by adding it as astatic
field to the class (because you only want one instance of the array made available globally across your application (right?) So your class will look something like this:public class NewMonitorDetails
{
// The static array of all NewMonitorDetails objects to be made available globally
private static ArrayList newMonitorDetailsArray = new ArrayList();// The instance fields (I corrected the names to fit the .NET naming guidelines) private int monNumber; private string edid\_Info; // An empty constructor - not strictly required as the compiler will create // a default empty constructor for you. public NewMonitorDetails() { } // public property backing the private field. public int MonNumber { get { return this.monNumber; } set { this.monNumber = value; } } // public property backing the private field. public string EDID\_Info { get { return this.edid\_Info; } set { this.edid\_Info = value; } } // public property backing the private field. public static ArrayList NewMonitorDetailsArray { get { return NewMonitorDetails.newMonitorDetailsArray; } // No setter required - you can just modify the existing array // rather than create a new one. }
}
Does this help?
-
public class NewMonitorDetails { public static int MonNumber; public static string EDID_Info; public NewMonitorDetails(){} }
I have the above class...I want to create an array of the above class to store different values in MonNumber and EDID_Info, so that I can access those values in other calsses where ever I want. How do I do this. Thanks.If you wan't to store any data in the objects, you have to have any non-static members. If you create an object from that class, the object will contain no data what so ever. If you create an array, you can access it from where ever you like, as long as you have a reference to it. How to best handle that reference is hard to say, as you have given so very little information on what you are trying to accomplish. --- b { font-weight: normal; }
-
Manu_81 wrote:
I want to create an array of the above class to store different values in MonNumber and EDID_Info, so that I can access those values in other calsses where ever I want
First, if you declare the fields as
static
you will only get one for all instances of the class. So, if you want to create multiple instances of the class you cannot create these asstatic
for the purpose you want. Second, declaring a field as public is bad form. You should declare them as private or protected and access them through properties. Read this article if you want to know why you should make the fields private and access them through a public property[^] Finally, you can make the array global by adding it as astatic
field to the class (because you only want one instance of the array made available globally across your application (right?) So your class will look something like this:public class NewMonitorDetails
{
// The static array of all NewMonitorDetails objects to be made available globally
private static ArrayList newMonitorDetailsArray = new ArrayList();// The instance fields (I corrected the names to fit the .NET naming guidelines) private int monNumber; private string edid\_Info; // An empty constructor - not strictly required as the compiler will create // a default empty constructor for you. public NewMonitorDetails() { } // public property backing the private field. public int MonNumber { get { return this.monNumber; } set { this.monNumber = value; } } // public property backing the private field. public string EDID\_Info { get { return this.edid\_Info; } set { this.edid\_Info = value; } } // public property backing the private field. public static ArrayList NewMonitorDetailsArray { get { return NewMonitorDetails.newMonitorDetailsArray; } // No setter required - you can just modify the existing array // rather than create a new one. }
}
Does this help?
Thanks a lot for your reply Colin. Its really helpful as I am new to obj. oriented... Got what you r saying. Now how do I set and retrieve values to this class. What I am doing exactly is I get the Monitor number and EDID info for each active Monitors connected from dll. So I want store this pair of values for each monitors in some global data structure so that I can use it where ever I want in my app... Thanks again.
-
Thanks a lot for your reply Colin. Its really helpful as I am new to obj. oriented... Got what you r saying. Now how do I set and retrieve values to this class. What I am doing exactly is I get the Monitor number and EDID info for each active Monitors connected from dll. So I want store this pair of values for each monitors in some global data structure so that I can use it where ever I want in my app... Thanks again.
Manu_81 wrote:
Now how do I set and retrieve values to this class.
Through the properties. In C# this looks just the same as getting and setting the field (although the compiled MSIL is different, but you don't need to worry about that). For example:
myMonitor.MonNumber = 2;
myMonitor.EDID_Info = "Some Info";ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
-
Thanks a lot for your reply Colin. Its really helpful as I am new to obj. oriented... Got what you r saying. Now how do I set and retrieve values to this class. What I am doing exactly is I get the Monitor number and EDID info for each active Monitors connected from dll. So I want store this pair of values for each monitors in some global data structure so that I can use it where ever I want in my app... Thanks again.
Also, I just double checked the code. The
NewMonitorDetailsArray
property should have been static - so I've corrected that (see updated previous post) You can access theArrayList
of allNewMonitorDetails
by usingNewMonitorDetails.NewMonitorDetailsArray
You cannot set this as it is controlled by the class. But you can perform any methods on the
ArrayList
(likeAdd
) so you can modify the contents of the array. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell