Solved

get some variable from one code to put into next code c#

Posted on 2011-02-13
4
230 Views
Last Modified: 2012-05-11
the  code Snippet ID=7955118 how can i use the  result to put into the next code Snippet ID=7955118
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://www.presscouncil.or.th/th2/index.php?option=com_content&view=article&id=230&Itemid=100084";
            string result = null;

            try
            {
                WebClient client = new WebClient();
                result = client.DownloadString(url);
            
               
            }
            catch (Exception ex)
            {
                // handle error
                MessageBox.Show(ex.Message);
            }

        }
    }
}

Open in new window

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Coderbuddy
{
public class ExtractEmails
{
private string s;
public ExtractEmails(string Text2Scrape)
{
this.s = Text2Scrape;
}
public string[] Extract_Emails()
{
string[] Email_List = new string[0];
Regex r = new Regex(@"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}", RegexOptions.IgnoreCase);
Match m;
//Searching for the text that matches the above regular expression(which only matches email addresses)
for (m = r.Match(s); m.Success; m = m.NextMatch())
{
//This section here demonstartes Dynamic arrays
if (m.Value.Length > 0)
{
//Resize the array Email_List by incrementing it by 1, to save the next result
Array.Resize(ref Email_List, Email_List.Length + 1);
Email_List[Email_List.Length - 1] = m.Value;
}
}
return Email_List;
}
}
}

Open in new window

0
Comment
Question by:teera
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
SAMIR BHOGAYTA earned 250 total points
ID: 34886160
Hi, You have to use the constructor for getting the variable to another page or declare it as a public variable and use this variable with the creating Object of that page. or use this link

http://www.c-sharpcorner.com/UploadFile/neerajsaluja/ConstructorsInCSharp11152005233222PM/ConstructorsInCSharp.aspx
0
 
LVL 11

Assisted Solution

by:azarc3
azarc3 earned 250 total points
ID: 34887720
If I correctly understand what you're trying to do, you're trying to get the value of the result variable (text that is pulled when clicking the button on the Windows Form) into the  ExtractEmails class, correct?

Then what you need to do is simple...

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://www.presscouncil.or.th/th2/index.php?option=com_content&view=article&id=230&Itemid=100084";
            string result = null;

            string[] EmailsList = new string[0];

            try
            {
                WebClient client = new WebClient();
                result = client.DownloadString(url);

                if(!string.IsNullOrEmpty(result))
                {
                    CoderBuddy.ExtractEmails helper = new CoderBuddy.ExtractEmails(result);
                    EmailsList = helper.Extract_Emails;
                }
               
            }
            catch (Exception ex)
            {
                // handle error
                MessageBox.Show(ex.Message);
            }

        }
    }
}

Open in new window

0
 

Author Comment

by:teera
ID: 34893953
Hi experts
it mean that I should keep one namesapace in one file and another name space in one file correct or not

0
 
LVL 11

Expert Comment

by:azarc3
ID: 34896171
Operationally it doesn't matter.

As long as the other namespace is physically available (it typically won't compile into a different dll unless you put it in a different project altogether) and the class(es) you want to get at has a public accessor it can be in a different namespace. Also, if the class file physically exists in the WinForms project but is under a different namespace the class(es) in it only need an internal accessor.

Having said that, the best practice is to have your namespaces mirror the path to the class from the root of the project. So if your class is buried three folder levels deep then the namespace should be...

namespace FolderName1.FolderName2.FolderName3
{
    public NameofClass
    {
        ...
    }
}

Have fun!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question