Solved

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

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

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 94
SqlDependency to get update from sql to my c# app 2 66
Sort GridView by ID Descending 1 17
Where is this file? 3 26
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…

828 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