Solved

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

Posted on 2011-02-13
4
241 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:Kelvin McDaniel
Kelvin McDaniel 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:Kelvin McDaniel
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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