Solved

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

Posted on 2011-02-13
4
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

734 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