Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.
Posted
Comments
Kishor Deshpande 24-Jan-13 10:01am    
Can you show me your method?
galextudor 24-Jan-13 16:07pm    
I can show you how the call to it looks like

private void getEntry(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
{
myRec rec = (myRec)gridView1.GetRow(e.RowHandle);
rec.no = LastEntry(typeof(myRec), "currentNo") + 1;
}
Sergey Alexandrovich Kryukov 24-Jan-13 14:36pm    
Why, why static method? And do you know what is a static method in fact?
—SA
galextudor 24-Jan-13 16:01pm    
:)) Yes, I know what a static method is..but it's something like this: I can't change it - it's stupid, dumb and so on, but I can't change it..
Sergey Alexandrovich Kryukov 24-Jan-13 16:10pm    
OK, but anyway you should provide more information; I cannot see a problem right now.

The real problem is not that the method is formally static, as you can always pass additional parameter to it to make it behave exactly like and instance one. From your words, I guess you cannot even change its signature, and then it's a real problem. If parameter information is not enough, you might need to add exchange through a shared static field/property, which would resolve the parameter passing problem but add even more stupidity and dumbness to resulting code...

—SA

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900