Solved

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

Posted on 2011-02-13
4
225 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now