需要帮助吗?访问另一个类中的变量 [英] need a help ? to access variables in another class

查看:49
本文介绍了需要帮助吗?访问另一个类中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public partial class Login : System.Web.UI.Page
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
   
    }
    public void Button1_Click(object sender, EventArgs e)
    {
 
        String Strt_Address = TextBox1.Text;
        String End_Address = TextBox2.Text;
 
        string geocodeUrl1 = string.Format(@"http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false", Strt_Address);
 
        string geocodeUrl2 = string.Format(@"http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false", End_Address);
 
        
 
        XmlDocument geocodeXmlDoc1 = new XmlDocument();
        geocodeXmlDoc1.Load(geocodeUrl1);
 
        XmlDocument geocodeXmlDoc2 = new XmlDocument();
        geocodeXmlDoc2.Load(geocodeUrl2);
 

        XmlNamespaceManager XmlMngr1 = new XmlNamespaceManager(geocodeXmlDoc1.NameTable);
        XmlMngr1.AddNamespace("geo", @"http://www.w3.org/2003/01/geo/wgs84_pos#");
 
        XmlNamespaceManager XmlMngr2 = new XmlNamespaceManager(geocodeXmlDoc2.NameTable);
        XmlMngr2.AddNamespace("geo", @"http://www.w3.org/2003/01/geo/wgs84_pos#");
 

        String sLong1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr1).InnerText;
        String sLat1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr1).InnerText;
 
        String sLong2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr2).InnerText;
        String sLat2 = geocodeXmlDoc2.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr2).InnerText;
 
        Label1.Text = sLong1 + ", " + sLat1;
        Label2.Text = sLong2 + ", " + sLat2;
 

        Page.ClientScript.RegisterStartupScript(Page.GetType(), "script",
        "calcRoute()", true);
        }
 
      }





所以我想访问这个String sLong1,sLat1,sLong2, sLat2在另一个班级



让我说我有哈弗森班级





so i want to access this String sLong1, sLat1, sLong2, sLat2 in another class

let say i have harversine class

public class harversine {

        // i want to access those varables in here!!!!
           how can i do that
        
     }





从解决方案中添加了OP的评论。



检查我的价值传递方法是否正确







Added the OP's comment from solution.

Check my value passing metod is correct or not


public partial class Login : System.Web.UI.Page
{
    //public String End_Address = string.Empty;
    public static Double elong1, elat1;
 
   public static Double Elong1
    {
        get { return Login.elong1; }
        set { Login.elong1 = value; }
    }
 
       public static Double Elat1
    {
        get { return Login.elat1; }
        set { Login.elat1 = value; }
    }
 
 public void Button1_Click(object sender, EventArgs e)
    {
 
String sLong1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lat", XmlMngr1).InnerText;
        String sLat1 = geocodeXmlDoc1.DocumentElement.SelectSingleNode(@"//geometry/location/lng", XmlMngr1).InnerText;
 

 
elong1 = Convert.ToDouble(sLong1);
elat1 = Convert.ToDouble(sLat1);
 
}





//这是一个示例类



// this is a sample class

public class harversine
    {
 
     String loudlong1 = Convert.ToString(elong1);
     String loudlat1 = Convert.ToString(elat1);
 
       // let say i want pass Fanswer to Textbox in button click event 
     String Fanswer = loudlong1 + ", " + loudlat1;
 
}







这都是样品,我想看看是否这是正确与否在我写一个原始代码之前。



我是一名学生,我也是该领域的新手。所以如果我错了,请帮助我纠正这个问题。




this all are samples and i want to see if this correct or not before i'm writing a original code.

i'm a student and i'm new to the field as well. so someone please kindly help me to correct this if i'm wrong.

推荐答案

首先让varibale全局公开

然后访问该类变量按类对象名称



first make the varibale global and public
and then access that variable by class object name
like
Login l=new Login
string str=l.loudlong1 


这篇关于需要帮助吗?访问另一个类中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆