Click here to Skip to main content
14,982,843 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Hi, so I have the following code:

C#
public webSite4(double bookPrice, string titleBook, string getUrl) //bookamillion
        {
            // used on each read operation
            byte[] buf = new byte[8192];

            // prepare the web page we will be asking for
            string urlResult = string.Empty;

            if (string.IsNullOrEmpty(isbnEntry.Text))
            {
                bookEntry.Text.Replace(" ", "+");
                string urlTitle =
                    String.Format(
                    "http://www.booksamillion.com/search?id=5910205702379&query={0}&where=book_title&search.x=24&search.y=9&search=Search&affiliate=&sort=price_ascending",
                    bookEntry.Text);
                urlResult = urlTitle;

            }
            else if (string.IsNullOrEmpty(bookEntry.Text))
            {
                string urlIsbn =
                    String.Format(
                    "http://www.booksamillion.com/search?id=5910205702379&query={0}&where=isbn&search.x=16&search.y=8&search=Search&affiliate=&sort=price_ascending",
                isbnEntry.Text);
                urlResult = urlIsbn;
            }
            else
            {
                string urlBoth =
                    String.Format("http://www.booksamillion.com/search?id=5910205702379&query={0}&where=isbn&search.x=16&search.y=8&search=Search&affiliate=&sort=price_ascending",
                    isbnEntry.Text);
                urlResult = urlBoth;
            }

            // prepare request
            CookieContainer cookies = new CookieContainer();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlResult);
            HttpWebResponse response = null;

            // set header values
            request.Method = "GET";
            request.CookieContainer = cookies;

            // get response data
            response = (HttpWebResponse)request.GetResponse();


            string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
            response.Close();

            string getPrice = string.Empty;
  
            HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
            htmlDoc.OptionFixNestedTags = true;
            htmlDoc.LoadHtml(responseData); // load html            
            HtmlAgilityPack.HtmlNode rootNode = htmlDoc.DocumentNode;

            titleBook = rootNode.SelectSingleNode("/html/body/div[4]/div/div/div[2]/ol/li/div/span/a/img").Attributes["alt"].Value;

            getUrl = rootNode.SelectSingleNode(@"/html/body/div[4]/div/div/div[2]/ol/li/div/span/a").InnerHtml.ToString();

    
            getPrice = getPrice.Substring(15);
                    

            bookPrice = System.Convert.ToDouble(getPrice);

            double priceConvert = bookPrice * 0.73; //converts USdollars to euro

            string webSource = "BooksAMillion";
        }
The method basically gets data off a website (the book title, price and url).

The Idea is that I have 3 more methods with the same variables and need to compare all bookPrice results. This is done by having a return type to all of the needed methods and be able to get the bookPrice variable in one place and compare them.

Any help is much appreciated :)

Thanks!
Posted
Updated 12-Feb-14 1:42am
v2
Comments
Richard C Bishop 11-Feb-14 15:53pm
   
So where are you stuck? Any errors?
slayasty 11-Feb-14 15:59pm
   
Well, the thing is im not exactly sure how it is suppose to be done. Google only helped me a little but I dont know how I can get 3 return variables that are different type off 1 method
Richard C Bishop 11-Feb-14 16:07pm
   
See my solution below for a brief example of something you could do.

I would recommend having that method return an object[]. You can handle each item returned from the method call as needed.

The method signature would look something like this:
protected object[] ReturnMultipleTypes(object sender, EventArgs e)
        {
            object[] d = new object()[3];
            return d;
        }

Keep in mind the access modifier needs to be appropriate for your situation and the size of your array will be dependent on the number of items you need to return.
   
v2
Comments
slayasty 11-Feb-14 16:32pm
   
may I ask why it is treated as object? I am using web forms and not web control, does that make a difference?
Richard C Bishop 11-Feb-14 16:37pm
   
Everything inherits from the "object" class, so using object is the only way to handle multiple data types in one instance. Does that make sense or answer your question?
slayasty 11-Feb-14 16:38pm
   
oh yes, makes sense. I will try the proposed solution and keep you posted! :)
Richard C Bishop 11-Feb-14 16:39pm
   
Sounds good. Good luck!

Come back if you need anything else on this.
If you want to return multiple values of different Types from a method, but still want to use strong-typing, then using a Tuple is one way to do that. Example:
C#
private void button1_Click(object sender, EventArgs e)
{
    var tt1 = method1("one", "two", 100.32D);

    string title = tt1.Item1;
    string bookURL = tt1.Item2;
    double price = tt1.Item3;
}

private Tuple<string, string, double> method1(string bookTitle, string bookURL, double bookPrice)
{
    // code to calculate/determine Book Title, URL, and Price, here ...

    return new Tuple<string, string, double>(bookTitle, bookURL, bookPrice);
}
Of course, you could use a Class, or, a Struct; if you want to examine why you might use a Tuple, see Eric Lippert's comments here: [^].

Disclaimer: I am not familiar with WebForms, and its limitations (if any).
   
v2
Comments
Richard C Bishop 12-Feb-14 9:58am
   
That is interesting, I have never even heard of "Tuple".
BillWoodruff 12-Feb-14 12:06pm
   
They are an interesting addition to C#, beginning with FrameWork 4.0.

One value of Tuples is that they give you a way to have an "immutable" collection, so, are thread-safe.

Some people really "hate" the Tuple syntax:

http://blog.gfader.com/2010/09/don-be-too-lazy-and-avoid-type.html

There's a chapter from Petricek and Skeet's book, "Functional Programming, on Tuples that compares their use in C# and F# here:

http://msdn.microsoft.com/en-us/library/vstudio/hh297117(v=vs.100).aspx
slayasty 12-Feb-14 14:04pm
   
MM, I will try that and see what happens! :)

Edit: Well I just noticed that in your code in the button method the variables are already known, but that is not the case in my program as each method will generate their own Url and price, so I need to make the return type varible before doing anything right?
BillWoodruff 12-Feb-14 23:11pm
   
If I understand your goals here, then I think the return Type can stay the same, but you can remove the parameter list to the 'method1 method. So, you'd do what you needed to do in the method to fetch/calculate your values, then "package" them in the Tuple, and return the Tuple.

A Tuple is a convenient way to "package" a collection of different Types. What we used to do with the old 'ArrayList is better done with a Tuple, now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900