使用静态方法检索Row? [英] Retrieve Row using static method?

查看:63
本文介绍了使用静态方法检索Row?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



我需要一些帮助 - 自从我编写代码以来已经有一段时间了,因此我很快迷失了它。



我有一个问题:



我有一个带有gridView的表单,gridView包含来自的信息一个名为Orders的表。每个记录都是Order类型。



然后我有一个静态int方法,用于返回表中最后一个记录中包含的信息。给定列。



对此方法的调用是使用Type类型的参数和一个字符串 - 从中​​检索信息的字段名称。 />


那我该怎么办?我知道一些方法,但不知道如何做到这一点,不包括改变调用方法(或参数类型)的方式,或将方法从静态转为非静态。

解决方案

没有正确的方法:因为该方法是静态的,它无法访问任何表单实例变量 - 而GridView是一个表单实例对象。因此,除非将GridView或GridViewRow作为参数传递,否则没有好的方法来执行此操作。



有一种方法 - 但它是非常糟糕的做法,容易造成间歇性问题,绝对不会以任何方式,形状或形式推荐



但是,如果对于任何愚蠢的(和它 愚蠢,相信我)你不能将方法改为非静态或改变参数,你可以创建一个静态GridView变量并复制你的GridView在你的程序中的适当点。然后你的静态方法可以检索它,因为它不再是你的表单类的实例成员。



但我宁愿吃自己的耳朵而不是它。

Hey,

I need some assistance - it''s been a while since I''ve written code, and as such I quickly got lost in it.

I have a question:

I have a form with a gridView, the gridView contains information from a table called Orders. Each record is of type Order.

I then have a static int method that is used to return the information contained in the last of the records of the table, inside a given column.

The call to this method is made using parameter of type Type and one a string - the field name from where the information is to be retrieved.

So how should i proceed? I know a bunch of ways, but not any way in which to do this that doesn''t include changing the way the method is called (or parameter types), or turning the method from static to non-static.

解决方案

There is no proper way to do this: because the method is static, it has no access to any of the form instance variables - and the GridView is a form instance object. So unless the GridView or a GridViewRow is passed through as a parameter, there is no "nice" way to do this.

There a way - but it is extremely bad practice, liable to cause you intermittent problems and definitely not recommended in any way, shape or form.

However if for whatever stupid (and it is stupid, trust me) reason you can''t change the method to non-static or alter the parameters, you can create a static GridView variable and copy your GridView into that at a suitable point in your program. Your static method can then retrieve it, because it is no longer an instance member of your form class.

But I would prefer to eat my own ears than do it.


这篇关于使用静态方法检索Row?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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