|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
accessing non-static members from static method..NET 2.0 I'm rewriting a method to become a static method, because FindCommentById is called from a static method. Which gives me several compile errors private static Comment FindCommentById(Guid id) { if (Comment.Id == id) <<<---- see implmentation of Id below this method return this; <<<--- "this" is not valid in static property else { Comment parent = null; foreach (Comment comment in Replies) <<<---- Replies is a collection of Comments associated with a comment, not sure to write that static { parent = Comment.FindCommentById(id); } return parent; } } ***** private Guid _id; public Guid Id { get { return _id; } set { _id = value; } } I know that I cannot call Id from FindCommentById because Id is not static. I tryed to make a static method as well (public static Guid Id), but that didn't work out as well.... any suggestions? On Sat, 06 Dec 2008 22:49:10 -0800, Jeff
<it_consulta***@hotmail.com.nospam> wrote: > I'm rewriting a method to become a static method, because Why are you making the method static, if it has code in it that is > FindCommentById is > called from a static method. Which gives me several compile errors intrinsically dependent on an instance? You can pass an instance into the method, to address the parts that require an instance. But it begs the question, why make the thing static? What's the actual goal here? From just the code you've posted, the only answer that appears appropriate is to suggest that you not make the method static. Assuming you actually do have a good reason to make the method static, it's not apparent from the code you posted, and so there's not really any way to provide any better advice. Pete the reason I started to convert that method into a static is because it is
being accessed by another static method In my code I have this method: public static List<Comment> GetComments(Guid id) { } which return a collection of comments, this method is used as SelectMethod in a GridView (asp.net 2.0). This method again makes a call to FindCommentById, So I thought I should convert FindCommentById into a static method... On the other hand maybe I should convert GetComments into a non-static method and then don't use it as SelectMethod, but instead just call the method programmatically in for example Page_Load event. On Sun, 07 Dec 2008 00:39:30 -0800, Jeff
<it_consulta***@hotmail.com.nospam> wrote: > the reason I started to convert that method into a static is because it Sorry, but that's not a good reason to convert an instance method to a > is > being accessed by another static method static method. It's impossible to say, without having a concise-but-complete code sample that correctly demonstrates the problem. In this case, that would require enough code to see the relationships between the static method(s), instance method(s), and the underlying data being accessed. However, as a general rule, you can't just go converting from static to instance or vice a versa just based on what will compile and what won't. Those characteristics (static and instance) are inherent in the design of the class and its members, and you cannot make a change without changing the design too. Furthermore, any change really needs to be done from the design first, so that you correctly understand the implications of the change, and how best to make them. In your case, it sounds like there's a possibility that the instance method might be more appropriate as a static method. But I can't say that for sure, because you haven't posted enough code to demonstrate the relationship of your data structures to each other. What I can say for sure is that when you find yourself in a static method needing to call an instance method, the very first solution you should explore is where to get an appropriate instance with which to call that instance method. That may or may not work, depending on whether the instance method was ever correctly an instance method in the first place. But it's where you need to start. Pete
Getting IP address & Physical Address from code in c#.
week comparisons help please Are people abusing "var"? Insert string into TextBox Re: Are people abusing "var"? not all code paths return a value program sort order with linq MSXML COM Object GridView Paging/Sorting without using the ObjectDataSource Why is Datagridview cell always rounding to int |
|||||||||||||||||||||||