C# - Passing A Control To A Class
Page 1 of 1
TheDiggler




Posts: 564

PostPosted: Sat, 23rd Mar 2013 18:12    Post subject: C# - Passing A Control To A Class
Maybe, what I am trying to do is stupid and just can't be done but I thought I would ask here anyways. I have a form, frmMain and a class, Class1. frmMain has a label, lblTest. What I want to do is pass lblTest to the constructor of Class1. Now, I can certainly pass lblTest to any method in Class1 but not it's constructor without getting:

Error 1 A field initializer cannot reference the non-static field, method, or property

Here is the code to instantiate from frmMain:
Class1 test= new Class1(lblTest );

Here is Class1:
public class Class1
{
private Label lblClassTest ;

public Class1 (Label lblClassTest )
{
// do something...
}
}

Any guidance would be appreciated.
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73250
Location: Ramat HaSharon, Israel 🇮🇱
PostPosted: Sat, 23rd Mar 2013 18:36    Post subject:
I just ran the following code and it compiled correctly:

Code:
namespace WindowsFormsApplication1
{
   public class Class1
   {
      private Label label1;

      public Class1(Label label1)
      {
         this.label1 = label1;
      }
   }

   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();

         Class1 c = new Class1(label1);
      }
   }
}
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73250
Location: Ramat HaSharon, Israel 🇮🇱
PostPosted: Sat, 23rd Mar 2013 18:50    Post subject:
I think your problem is where you initialize Class1. You are using lblTest before it was initialized. Call "new Class1(lblTest );" in your frmMain constructor, not as field initializer.

In other words, instead of:

Code:
namespace WindowsFormsApplication1
{
   public class Class1
   {
      private Label label1;

      public Class1(Label label1)
      {
         this.label1 = label1;
      }
   }

   public partial class Form1 : Form
   {
      public Class1 c = new Class1(label1);

      public Form1()
      {
         InitializeComponent();
      }
   }
}


Do:

Code:
namespace WindowsFormsApplication1
{
   public class Class1
   {
      private Label label1;

      public Class1(Label label1)
      {
         this.label1 = label1;
      }
   }

   public partial class Form1 : Form
   {
      public Class1 c;

      public Form1()
      {
         InitializeComponent();

         c = new Class1(label1);
      }
   }
}
Back to top
TheDiggler




Posts: 564

PostPosted: Sat, 23rd Mar 2013 18:56    Post subject:
That was EXACTLY my problem LeoNatan.

Feel a bit stupid, I was looking at code that was passing controls between Forms but did not catch where they instantiated from.
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73250
Location: Ramat HaSharon, Israel 🇮🇱
PostPosted: Sat, 23rd Mar 2013 19:14    Post subject:
Cheers Smile
Back to top
Areius




Posts: 14858

PostPosted: Sun, 14th Apr 2013 23:41    Post subject: *****
*****


Last edited by Areius on Fri, 19th Sep 2025 16:13; edited 1 time in total
Back to top
TheDiggler




Posts: 564

PostPosted: Mon, 15th Apr 2013 01:56    Post subject:
Areius wrote:
Not sure why you need this approach, but I don't recommend this pattern. If you need to have access to the properties of labels, better use model view controller for example. Might be a bit out of reach here depending on what you actually need.


Your are right, and that is exactly how I planned to approach the project until I read the class (school) specifications which throws out any notion of programming best practices.
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73250
Location: Ramat HaSharon, Israel 🇮🇱
PostPosted: Mon, 15th Apr 2013 09:08    Post subject:
I never liked people that tell me what is a correct "approach" and what isn't, especially on large projects, where crazy approaches are sometimes necessary, even if giong against what people have read in their design pattern 101 books during study.
I realize it's not the case here, but still. Often I see people on StackOverflow go into discussions of why someone "would want to do" something, instead of answering the question at hand. It wouldn't be as bad if they'd offer a different solution, but in the large minority of cases, people just would like to lecture what they believe is right.
Back to top
TheDiggler




Posts: 564

PostPosted: Mon, 15th Apr 2013 15:49    Post subject:
LeoNatan wrote:
I never liked people that tell me what is a correct "approach" and what isn't, especially on large projects, where crazy approaches are sometimes necessary, even if giong against what people have read in their design pattern 101 books during study.
I realize it's not the case here, but still. Often I see people on StackOverflow go into discussions of why someone "would want to do" something, instead of answering the question at hand. It wouldn't be as bad if they'd offer a different solution, but in the large minority of cases, people just would like to lecture what they believe is right.


I know exactly what you mean, StackOverflow is one example. Another, anywhere someone writes about "best practices" and has open comments, it is almost assured there is a ton of arguing. Some people can be very dogmatic about what they feel are "best practices".

Until now, my programming class assignments were very generally specified; "write a program that does A. B. and C.". This particular project was very specific; "I want A. but it needs to be contained in this class which also does B. and must use X., Y. and Z. methods, properties and events". I don't particularly like this approach. The challenge for me is to give them exactly what they want while maintaining some semblance of what I understand to be "best practices".
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Mon, 15th Apr 2013 16:05    Post subject:
Google "spaces or tabs" and prepare for the same nonsense. Apparently I'm a retard, because I use tabs for indentation (with that I mean the tab char, not the key which can be configured to insert x spaces in most editors).
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73250
Location: Ramat HaSharon, Israel 🇮🇱
PostPosted: Mon, 15th Apr 2013 18:50    Post subject:
I hate 4 spaces. I always set Visual Studio and Xcode to keep tabs as tabs. Laughing
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73250
Location: Ramat HaSharon, Israel 🇮🇱
PostPosted: Mon, 15th Apr 2013 18:55    Post subject:
TheDiggler wrote:
I know exactly what you mean, StackOverflow is one example. Another, anywhere someone writes about "best practices" and has open comments, it is almost assured there is a ton of arguing. Some people can be very dogmatic about what they feel are "best practices".

Until now, my programming class assignments were very generally specified; "write a program that does A. B. and C.". This particular project was very specific; "I want A. but it needs to be contained in this class which also does B. and must use X., Y. and Z. methods, properties and events". I don't particularly like this approach. The challenge for me is to give them exactly what they want while maintaining some semblance of what I understand to be "best practices".

To me, design patterns have always been examples of boilerplate code that many people have used to solve similar problems. But I do remember teachers and lecturers who taught them as if they ought to be known by heart and be used whenever possible. I also have coworkers that whenever presenting a design, go "and we use this patter here and that pattern there and we join this and that pattern" and I want to bang my head to the wall, because these patterns are fucking guidelines, but these people insist on using them because they read about them in books and feel are necessary. Likewise, I would like to express my distaste for "design pattern questions" in work interviews. What does it matter if I know how this patter or that is called, if I can implement a close variation of it by myself where needed? So what if I don't remember that they are called "strategy" and "visitor"? Rolling Eyes
Since when did programming become a "learn by heart" as oppose to "do it the best way for the problem"?
Back to top
TheDiggler




Posts: 564

PostPosted: Mon, 15th Apr 2013 19:05    Post subject:
LeoNatan wrote:
TheDiggler wrote:
I know exactly what you mean, StackOverflow is one example. Another, anywhere someone writes about "best practices" and has open comments, it is almost assured there is a ton of arguing. Some people can be very dogmatic about what they feel are "best practices".

Until now, my programming class assignments were very generally specified; "write a program that does A. B. and C.". This particular project was very specific; "I want A. but it needs to be contained in this class which also does B. and must use X., Y. and Z. methods, properties and events". I don't particularly like this approach. The challenge for me is to give them exactly what they want while maintaining some semblance of what I understand to be "best practices".

To me, design patterns have always been examples of boilerplate code that many people have used to solve similar problems. But I do remember teachers and lecturers who taught them as if they ought to be known by heart and be used whenever possible. I also have coworkers that whenever presenting a design, go "and we use this patter here and that pattern there and we join this and that pattern" and I want to bang my head to the wall, because these patterns are fucking guidelines, but these people insist on using them because they read about them in books and feel are necessary. Likewise, I would like to express my distaste for "design pattern questions" in work interviews. What does it matter if I know how this patter or that is called, if I can implement a close variation of it by myself where needed? So what if I don't remember that they are called "strategy" and "visitor"? Rolling Eyes
Since when did programming become a "learn by heart" as oppose to "do it the best way for the problem"?


I often wonder how much time some teams spend working out the complexity of how every little thing should fit in a nice and neat little pattern or arguing back-and-forth about "best practices", forgetting there is a client that is waiting for a program to resolve a problem.
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Mon, 15th Apr 2013 19:06    Post subject:
LeoNatan wrote:
I hate 4 spaces. I always set Visual Studio and Xcode to keep tabs as tabs. Laughing

Yep Razz

To each their own, I just don't see the benefit of spaces. Because among the "space users" there's also always "LOL USE 2" -> "NO 4 LOLZ" -> "WTF, 8 MEN". And here I am with my tabs, not giving a single fuck, because I can just change how many "spaces" one tab char expands to Laughing
It's what the damn key was meant for on typewriters
Back to top
LeoNatan
☢ NFOHump Despot ☢



Posts: 73250
Location: Ramat HaSharon, Israel 🇮🇱
PostPosted: Mon, 15th Apr 2013 19:24    Post subject:
TheDiggler wrote:
LeoNatan wrote:
TheDiggler wrote:
I know exactly what you mean, StackOverflow is one example. Another, anywhere someone writes about "best practices" and has open comments, it is almost assured there is a ton of arguing. Some people can be very dogmatic about what they feel are "best practices".

Until now, my programming class assignments were very generally specified; "write a program that does A. B. and C.". This particular project was very specific; "I want A. but it needs to be contained in this class which also does B. and must use X., Y. and Z. methods, properties and events". I don't particularly like this approach. The challenge for me is to give them exactly what they want while maintaining some semblance of what I understand to be "best practices".

To me, design patterns have always been examples of boilerplate code that many people have used to solve similar problems. But I do remember teachers and lecturers who taught them as if they ought to be known by heart and be used whenever possible. I also have coworkers that whenever presenting a design, go "and we use this patter here and that pattern there and we join this and that pattern" and I want to bang my head to the wall, because these patterns are fucking guidelines, but these people insist on using them because they read about them in books and feel are necessary. Likewise, I would like to express my distaste for "design pattern questions" in work interviews. What does it matter if I know how this patter or that is called, if I can implement a close variation of it by myself where needed? So what if I don't remember that they are called "strategy" and "visitor"? Rolling Eyes
Since when did programming become a "learn by heart" as oppose to "do it the best way for the problem"?


I often wonder how much time some teams spend working out the complexity of how every little thing should fit in a nice and neat little pattern or arguing back-and-forth about "best practices", forgetting there is a client that is waiting for a program to resolve a problem.

Well, I can attest from personal experience, that one of these so called "designs" was prepared by a coworker and it was discussed and discussed and discussed and presented to higher ups, and eventually me and another coworker rewrote a completely different design which was actually possible and coherent, and we implemented it by ourselves.
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group