对象在当前上下文中不存在. -使用属性时 [英] Object doesn't exist in the current context. - When using a property

查看:109
本文介绍了对象在当前上下文中不存在. -使用属性时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想使用属性时遇到问题.

在一种形式中,我有一个变量和一个属性来返回此变量值:

Hi all,

I am having a problem when I want to use a property.

In one form I have a variable and a property to return this variable value:

public string getInvoiceNumber
{
get { return textBox.Text;}
}



此表单调用另一个表单,并且此新表单需要访问其父表单的该值.

例如



This form calls another form, and, this new form needs to access this value of its parent form.

e.g.

Principal principal = new Principal();

MySqlCommand mscInvoiceDetails = new MySqlCommand("SELECT invoice_code, dt_invoice, total_value, observation FROM tb_invoices WHERE invoice_code = \''" + principal.getInvoiceNumber + "\'';", bdConn);



当我在运行时调试该部分时,它显示了principal.getInvoiceNumber = null.

当我在运行时检查父窗体中的此变量(使用快速监视")时,它显示了对象在当前上下文中不存在. .

我该如何解决这个问题?


提前致谢! :)



when I debug this part on running time, it shows me that principal.getInvoiceNumber = null.

When I check on running time this variable in the parent form (using Quick Watch), it shows me Object doesn''t exist in the current context. .

How can I solve this problem?


Thanks in advance! :)

推荐答案

确定.如果没有图片,这将不太容易解释,但是...

在您的代码段中,创建Principles类的实例:
OK. This is not going to be too easy to explain without pictures, but...

In your code snippet, you create an instance of the Principle class:
Principal principal = new Principal();

然后您立即使用该实例:

You then use that instance immediately:

MySqlCommand mscInvoiceDetails = new MySqlCommand("SELECT invoice_code, dt_invoice, total_value, observation FROM tb_invoices WHERE invoice_code = \''" + principal.getInvoiceNumber + "\'';", bdConn);

这种方法有几处错误:

1)永远不要那样访问数据库!阅读有关SQL Injection攻击的信息,然后查找MySqlCommand.Parameters.AddWithValue,然后在将来使用它.否则,任何用户都可以(意外或故意)删除您所有的数据库表...

2)当您创建Principle的实例时,我假设它是从Form派生的类?是否打算让用户输入发票编号?那么,为什么要创建它的new实例,并期望它具有信息?您需要使用Show()或(更可能是)ShowDialog()来允许用户输入一些数据,然后才能将其读出!
您得到的错误-对象在当前上下文中不存在-是因为您在上面创建的Principle的实例仅存在到当前块的末尾-for循环(如果条件或方法).对于整个班级来说,这不是全球性的.

这有意义吗?

There are a couple of things wrong with this approach:

1) Don''t ever access a database that way! Read up on SQL Injection attacks and then look up MySqlCommand.Parameters.AddWithValue then use that in future. Otherwise, any user can (accidentally or deliberately) delete all you database tables...

2) When you create an instance of Principle, I assume that it is a class derived from Form? And that it is intended to have the user enter a invoice number? So why are you creating a new instance of it, and expecting it to have information? You need to use Show() or (more likely) ShowDialog() to allow the user to enter some data before you can read it out!
The error you are getting - object does not exist in the current context - is because the instance of Principle you create above exists only until the end of the current block - a for loop, if condition, or method. It is not global to the whole class.

Does that make any sense?

"I got it. Makes sense. In fact I am using showDialog() in the form where the user enter the invoice number to open the new form (the one I want to access the property in the parent form). But how can I access this variable (in the parent form)? - lucasgrohl 1 min ago"



因此,如果您使用的是ShowDialog,则代码应如下所示:



So, if you are using ShowDialog, your code should look like this:

Principal principal = new Principal();
if (principle.ShowDialog() == DialogResult.OK)
   {
   string myInvoiceNumber = principle.getInvoiceNumber;
   ...
   }

还是我正在阅读您正在尝试实现错误方法的方法?

Or am I reading what you are trying to achieve the wrong way round?

"I asking you because what happens is, I have a form where an user enter all the details of an invoice (client, payment type, etc), and then the system generates automatically the invoice number. After that the user clicks on the button "Create invoice" and the system registers the invoice on the database and asks if the user wants to print it. If the user clicks yes, the system opens a new form containing a reportviewer and user the invoice number (generated in the parent form), to colect the rest of the informations on the database.



知道了!
因此,原则是主要表单,并且您想在单击按钮时访问从原则上的实例打开的表单上的发票编号.

好的.最好的方法是不要尝试从子级访问父级表单-如果这样做,则它将子级固定为父级的设计.这意味着您不能在不考虑对孩子的影响的情况下自由地改变父母,也不能在其他地方重复使用孩子.相反,(因为您始终需要发票号才能打印出来),我将默认构造函数更改为始终要求发票号:



Got you!
So Principle is the main form, and you want to access the invoice number on a form opened from the instance on Principle when you click the button.

OK. The best way is not to try to access the parent form from the child - if you do then it fixes the child to the design of the parent. That means you can''t freely change the parent without considering the effects on the child, and you can''t re-use the child elsewhere. Instead, (since you always need the invoice number in order to print it) I would change the default constructor to always require the invoice number:

private string invoiceNumber;
Public MyPrintInvoiceForm (string invoiceNumberFromParent)
   {
   invoiceNumber = invoiceNumberFromParent;
   }

您将随后:

MyPrintInvoiceForm mpif = new MyPrintInvoiceForm(textbox.Text);
if (mpif.ShowDialog() == DialogResult.OK)
   {
   ...
   }


(这些名称仅是提供线索,它们是从何而来的,而不是对现实世界的建议!)
或在使用ShowDialog方法之前在父级设置的子级中提供一个get/set属性.


(The names are just to give a clue where things are coming from, not suggestions for the real world!)
Or provide a get/set property in the child which the parent sets before using the ShowDialog method.


这篇关于对象在当前上下文中不存在. -使用属性时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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