• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

share same functionality

Ok I have a web app that hase about 10 pages and I couple of the pages share some similiar 'functions' which are calls to a web method. What is the best and recommended way to avoid duplication of the same code in multiple code behind files . Which is a call to a web service.
Please provide clear details.

Thanks.

0
chicabow
Asked:
chicabow
  • 7
  • 6
1 Solution
 
dstanley9Commented:
One way is to add a class to the project that contains these functions.  To use them, simply create an instance of the class and call the functions on it.  

Ex:

public class Util
{
  public int FunctionOne(string paramOne, int paramTwo)
  {
     // do something
   
    // return  an int
  }
}


In your pages:

Util myUtil = new Util();
string p1 = "dafdsa";
int p2 = 45;
int result = myUtil.FunctionOne(p1, p2);
0
 
chicabowAuthor Commented:
Thanks for the response...this is an example of one that I want to put in the Utitlies(building of a drop down) but if I put this in the utilites I get error making reference to the ddlcommunity(name of drop down) not existing, so I assume I have to pass this when I make the call ? Does your example above apply?

private void PopulateCommunities()
{
      localhost.wsservice DService = new localhost.Dservice();
      DataSet ds = DService.GetCommunitiesList();
      ddlcommunity.Items.Add("");
      ddlcommunity.Items.Add("");
      foreach (DataRow dr in ds.Tables[0].Rows)
      {
            ListItem item = new ListItem(dr["communityname"].ToString(),dr["communityid"].ToString());            
            ddlcommunity.Items.Add(item);                              
      }                  
      ds.Dispose();
}
0
 
dstanley9Commented:
Well, you _could_ pass in the ListBox, but a better way may be to bind to the DataTable:

// In web form
DataSet ds = DService.GetCommunitiesList();
ddlcommunity.Datasource = ds.Tables[0];
ddlcommunity.DataTextField = "communityname";
ddlcommunity.DataValueField = "communityid";
ddlcommunity.DataBind();



0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
chicabowAuthor Commented:
just curious, how would you pass in the listbox and take that approach.

I did try the method you displayed there and that works as well, do you feel that is better to do that the 'utitlies' idea? The one thing I did notice is using this way....the default value is not blank as my original code had that but with this, there is no blank value on load. Your thoughts?

ddlcommunity.Items.Add("");


Just trying to understand the better method of doing things.

Thanks.
0
 
dstanley9Commented:
I say it's "better" because it isolates your data from the actual control.  You can get a list of communities without having to worry about an actual control.  

To pass in the DropDownList:

private void PopulateCommunities(DropDownList ddlcommunity)
{
     localhost.wsservice DService = new localhost.Dservice();
     DataSet ds = DService.GetCommunitiesList();
     ddlcommunity.Items.Add("");
     ddlcommunity.Items.Add("");
     foreach (DataRow dr in ds.Tables[0].Rows)
     {
          ListItem item = new ListItem(dr["communityname"].ToString(),dr["communityid"].ToString());          
          ddlcommunity.Items.Add(item);                        
     }              
     ds.Dispose();
}
0
 
dstanley9Commented:
To add a blank item at the front:

ddlcommunity.Items.Insert(0,"");
0
 
chicabowAuthor Commented:
So your saying that this way listed below is better?
   If so how does the blank item get inserted?
   If not, please explain so more.

DataSet ds = DService.GetCommunitiesList();
ddlcommunity.Datasource = ds.Tables[0];
ddlcommunity.DataTextField = "communityname";
ddlcommunity.DataValueField = "communityid";
ddlcommunity.DataBind();


Thanks.
0
 
dstanley9Commented:
That's the most common way to do databinding.  Is it better?  Well, that's relative.  Some people might rather have fine-grained control of actually adding items (like you did in your example), but as you can see, that ties your UI and data layers together.  

Also, with .NET 2.0, there are methods of "declarative" data binding, where you don't have to write ANY code.  You just select a data source, set some binding parameters (e.g. DataTextField and DataValueField) and the data binding code is all done by the fremework.

Here's how to add the blank item:

DataSet ds = DService.GetCommunitiesList();
ddlcommunity.Datasource = ds.Tables[0];
ddlcommunity.DataTextField = "communityname";
ddlcommunity.DataValueField = "communityid";
ddlcommunity.DataBind();
ddlcommunity.Items.Insert(0,"");
0
 
chicabowAuthor Commented:
As for the .NET 2.0, thanks for that FYI, looking forward to when we finally migrate over.

OK, thanks for your input, so the adding of the blank item worked as desired. So I understand all that we did here, my original question was about sharing the same functionality (there are a few spots where I need to populate this drop down), and from what I understand what we did was 'recode' the population of the drop down (still calling the WS) but we still have the same 'amount' of code - meaning this code exists in all the pages where the drop down needs to be populated, or am I incorrect in this thinking?



0
 
dstanley9Commented:
If you look at it purely in terms of lines of code, yes, but in terms of separation of responsibility and pure oo-style programming, it's a cleaner solution.  

But, if you're just wanting to minimize repetitive code, you _can_ use the PopulateCommunities method  from my 5/19 post, although you're still going to repeat a few lines of code each time:

  Utilites u = new Utilities();  
  u.PopulateCommunities(this.ddlcommunity);




0
 
chicabowAuthor Commented:
Thanks for your input and comments, I guess I am recalling some information that was 'brainwashed' into me about 'repeating code' that you should be able to 'modularize' all your code (which is what I thought OO-style programming was). Isnt that what the whole point of using an 'utlities' type idea?

Again, I appreciate your feedback and your thoughts as I value all experts opinions.

Thanks.
0
 
dstanley9Commented:
Yes, you should modularize your code, but I also believe in seaparation of responsibilities.  There's always a balance of mimizing code and maximizing flexibility.  Just throwing a reference of your drop-down list to a "Utility" program to save 5 or 6 lines of code does not seem like a good balance.  IMHO, I do not consider repeating 5 or 6 lines of "framework" code (i.e. not business-logic code) a problem.

If you want a "purer" OO-style solution, you could create a Web User Control or Server Control to handle the drop-down list.  Here's the MSDN docs for that:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwlkWalkthroughCreatingWebUserControls.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconintroductiontocustomwebcontrols.asp
0
 
chicabowAuthor Commented:
Thanks for all your input. :)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now