有人检查这个并告诉我在课堂上获取价值的方法吗? [英] someone check this and tell me way to access value in class?

查看:81
本文介绍了有人检查这个并告诉我在课堂上获取价值的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我将按钮点击事件的价值传递给上课







< pre lang =cs> public partial class 登录:System.Web.UI.Page
{


protected void Page_Load( object sender,EventArgs e)
{
}
public void Button1_Click( object sender,EventArgs e)
{
String End_Address = TextBox2.Text;



//我将文本框值传递给字符串...在c#





所以我想访问类文件中的字符串....



  public   class  harversine {
登录实例= 登录();
字符串 valueEnd_Address = instance.End_Address;







这是否正确?

解决方案

你可以这样做:



  public   partial   class 登录:System.Web.UI.Page 
{
public String End_Address = string .Empty;

受保护 void Page_Load(对象发​​件人,EventArgs e)
{
}
public void Button1_Click( object sender,EventArgs e)
{
End_Address = TextBox2.Text;





然后访问它完全使用你所写的内容。


逻辑上它正确但你会不获取信息,因为实例将是一个不同的对象,而不是与客户端呈现的相同。相反,您可以尝试以下选项之一。



1.将end_address作为参数传递给构造函数以使用harversine类。

ex:在

后面的登录代码中,harversine inst = new harversine(TextBox2.Text); 



2.在harversine类中声明属性EndAddress并分配vlaue点击事件背后的代码

ex:

 harversine inst = new harversine(); inst.EndAddress = TextBox2.Text; 


您可以使用 C#中的属性 [ ^ ]。

harversine 类中声明属性。

  private   string  _End_Address; 
public string End_Address
{
get { return _End_Address; }
set {_End_Address = value ; }
}



从登录页面设置。请参阅:

  public   void  Button1_Click ( object  sender,EventArgs e)
{
harversine obj = new harversine( );
obj.End_Address = TextBox2.Text;
}



您可以在 harversine 类中访问它:

  public   void  SomeFunction()
{
if (End_Address!= null
string EndAdd = End_Address;
}







有关详细信息,请参阅以下链接:

C#Property [ ^ ]





- Amit


someone tell me way to pass the value of button click event to class



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 End_Address = TextBox2.Text; 


// i'm passing textbox value into string... in c#


so i want to access the string in class file....

public class harversine {
        Login instance = new Login();
        String valueEnd_Address = instance.End_Address;




is this correct or not?

解决方案

You could do this:

public partial class Login : System.Web.UI.Page
{
    public String End_Address = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public void Button1_Click(object sender, EventArgs e)
    {
        End_Address = TextBox2.Text; 



Then to access it use exactly what you wrote.


Logically its correct but you will not get info as instance will be a differnt object and not the same what is rendered to the client. Instead you can try one of the following options.

1. Pass end_address as a paramater to the constructor to harversine class.
ex: In login code behind

harversine inst = new harversine(TextBox2.Text);


2. Declare a property EndAddress in harversine class and assign the vlaue in code behind click event
ex:

harversine inst = new harversine(); inst.EndAddress =TextBox2.Text;  


You can easily do it using Properties in C#[^].
Declare a Property in harversine class.

private string _End_Address;
public string End_Address
{
    get { return _End_Address; }
    set { _End_Address = value; }
}


Set it from login page. See this:

public void Button1_Click(object sender, EventArgs e)
{
    harversine obj = new harversine();
    obj.End_Address = TextBox2.Text;
}


You can access it in your harversine class:

public void SomeFunction()
{
    if(End_Address != null)
        string EndAdd = End_Address;
}




Refer the below link for more details:
C# Property[^]


--Amit


这篇关于有人检查这个并告诉我在课堂上获取价值的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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