如何将参数从一个函数传递到另一个函数?这两个方法都属于同一个类...... [英] How to pass parameters from one function to another?both the methods are in the same class...

查看:111
本文介绍了如何将参数从一个函数传递到另一个函数?这两个方法都属于同一个类......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将参数从一个函数传递到另一个函数?两个方法都在同一个类中...我正在使用VS 2010 Ultimate ..代码如下所示...





How to pass parameters from one function to another??both the methods are inside the same class...i'm using VS 2010 Ultimate..the code is as shown below...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
 
namespace WpfApplication7
{
///

/// Interaction logic for Window3.xaml
///

public partial class Window3 : Window
{

public Window3()
{
InitializeComponent();
FillDataGrid();

}
 
private void Window_Loaded(object sender, RoutedEventArgs e)
{
 
}
 
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
 
}
private void comboBox1_Loaded(object sender, RoutedEventArgs e)
{
List data = new List();
data.Add("APPLE");
data.Add("GOOGLE");
data.Add("INFOSYS");
data.Add("IBM");
data.Add("HP");
 
// ... Get the ComboBox reference.
var comboBox1 = sender as ComboBox;
 
// ... Assign the ItemsSource to the List.
comboBox1.ItemsSource = data;
}

public void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var comboBox1 = sender as ComboBox;
string value = comboBox1.SelectedValue as string;

}
 
public void FillDataGrid()
{

SqlConnection con = new SqlConnection("Data Source=ANAIK-PC;Initial Catalog=wpfstocks;Integrated Security=True");
string command = "select * from applel2";
SqlCommand cmd = new SqlCommand(command, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("applel2");
da.Fill(dt);
dataGrid1.ItemsSource = dt.DefaultView;
} 
} 
}





任何建议???



Any suggestions???

推荐答案

一个函数(在你的情况下是一个方法)可以通过调用它将参数传递给另一个函数。

如果这不是你的场景(例如你正在调用)顺序两种方法)然后你可以:

  • 将相同的参数显式传递给两种方法
A function (well, a method in your case) can pass parameters to another function by invoking it.
If this is not your scenario (e.g. you are calling sequentially both methods) then you may:
  • Expicitly pass the same parameters to both methods
  • 将参数包装在一个类中,使第一个方法返回一个引用此类的实例,最后使用这样的引用作为第二个方法参数。
  • 使参数可用作类的成员(两种方法的同一类)
  • ...


你可以简单地调用一个方法(适当的)来自同一类的其他方法的参数。

其他技术可能是使用类级变量,可以在bot中访问h方法。



如果您有更具体的问题,请告诉我您想要从另一个方面打电话的确切方法。
You can simply call a method (with appropriate arguments needed) from other method of same class.
Other technique may be use class level variable which would be accessible in both methods.

If you are having more specific question, please let me know exactly which method you want to call form other one.


这篇关于如何将参数从一个函数传递到另一个函数?这两个方法都属于同一个类......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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