VBAHole22 Posted May 19, 2005 Posted May 19, 2005 I have a web service that returns a class that I build to hold the return values. I have string variables in there like countyname, countyfips, etc. I have get/set properties for these. It's all nice and neat. When the object is returned in the test page it shows all of the return values in nice tidy XML. Here is the problem. I want to add 3 more variables to my class. But they are going to be arrays. I won't know how many values will be in there before hand. How do I write the variables and properties for these arrays of strings? I tried using array lists instead and it sort of worked #region Members private string countyName; private string countyFIPS3; private string countyFIPS5; private ArrayList adjCountyNames = new ArrayList(); private ArrayList adjCountyFIPS3 = new ArrayList(); private ArrayList adjCountyFIPS5 = new ArrayList(); #endregion I was able to instantiate a new object (ci) and loop through and use ci.Add to add values to these arraylists but when I sent the object back I didn't get the results in my xml. Is my issue with how I am returning the arraylist? Should I stick with arraylist and then push it to an array at some point? Confused? I am trying to make things a little more OOP so I can reuse some of this. I can do this no problem old school, but I'm trying to learn this new stylee. Thanks for any suggestions. Quote Wanna-Be C# Superstar
mskeel Posted May 19, 2005 Posted May 19, 2005 I would probably hide the arraylists from the user completey -- make them private data members -- and then add public methods like: public int AddCountryNames(string[] names) //or whatever sort of structure the names come it public int AddCountryFIPS3(string[] names) public int AddCountryFIPS5(string[] names) Inside these methods you will keep full control over what operations can actually happen to the arraylists. That way, if you have a private method for putting the XML together, have the user just give you a string and then you can put the XML together yourself without having to couple your XML creation code to outside classes. Also, this gives you the flexibility to use different types of collections if you want. Maybe you'll want a custom class or maybe you'll want to switch to an array. Hope that helps. Quote
VBAHole22 Posted May 20, 2005 Author Posted May 20, 2005 Thank you for that suggestion. I kind of understand what you are saying. I got this to work but I see where your method could provide more flexibility. The reason I was not getting the result in the web service return object was because I had not exposed public properties for the arraylist of this object. I guess you need to expose them before they get included in the return object. Makes sense. Hurray for VA! Quote Wanna-Be C# Superstar
mskeel Posted May 20, 2005 Posted May 20, 2005 Hurray for VA!HA HA HA!! I am happy you got it figured out. The one major downside to my suggestion is that it takes a lot more work to build in all that abstraction. Since you've got it working and it really doesn't appear that you need the added flexibility, I wouldn't worry about abstracting what you have any further. The abstraction/strong typing debate is one of the major issues with Object Oriented programming. The way it is supposed to be done is great when it is done well, but it is very hard and takes a lot of overhead (more code) to do. Another really funny thing about OOP is that classes and code really aren't as reusable as originally thought. In order to create a class that is general enough to reuse you have to strip most of the specifics that make the class good and useful. Then you still have to write code to inherit the class and reuse the functionality. Very few classes can actually be used right out of the box for exactly what they were inteded to be used for (other than the most basic of data structures, unless you want strong typing). That's another big allure for Model Driven Architecture -- it promotes reuse at the Domain level which is a set of classes. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.