Should I Use Classes The Ways I Have Been?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

50 Replies - 11407 Views - Last Post: 22 March 2013 - 12:30 PM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Should I Use Classes The Ways I Have Been?

Posted 04 March 2013 - 06:07 PM

So people in here always tell me to use classes and I have been but I have a few questions regarding these classes but this will throughly answer most of my questions.

First if a class instance is continuously being created over and over again does this inhibit performance Vs's say using a static class? For instance I have a class that helps output a string of the current font used and it gets fired every single time someone types a letter in the richtextbox like so

 private void Update_Font_Lbl()
        {
//new instance created
            CGetRTBFontString rtbs = new CGetRTBFontString();
            rtbs.Set_Font_Object = (this.tbctrl_main.SelectedTab.Tag as RichTextBox);
            this.tlstrp_lbl_font.Text = rtbs.Return_Font_String();
        }

  private void rtb_selection_changed(object sender, EventArgs e)
        {
            Update_Font_Lbl();
        }


Does this make any difference?

Second I hope I haven't become too obsessive but I've even been creating classes for simple things like a font dialog like so
 abstract class CFontChangePrompt<T>
    {
        protected abstract Font Get_Set_Font
        {
            set;
        }
        public abstract T Font_Change_Object { get; set; }
        public void Set_Font_To_Object_With_Dialog()
        {
            using (FontDialog fd = new FontDialog())
            {
                if (fd.ShowDialog() == DialogResult.OK)
                {
                    this.Get_Set_Font = fd.Font;
                }
            }
        }
    }

    class CFontChangeRTB : CFontChangePrompt<RichTextBox>
    {
        public override RichTextBox Font_Change_Object
        {
            get;
            set;
        }
        protected override Font Get_Set_Font
        {
            set
            { this.Font_Change_Object.SelectionFont = value; }
        }

    }

    


}



What the above code allows is the changing of a font type for multiple objects types in classes for instance a Richtextbox but you could create another class for changing the font for a menu or a label etc. etc.

Am I being obsessive using classes for even simpler things like this or is this good code?

LAST BUT NEAT LEAST For some things that will be used the same way over and over again in a particular way that can't change I figured static classes were better for example in my creation of a new tab I use

public static class CTabMethods
    {
        private static int tab_num = 2;
        private static int Increment_Tab_Num
        {
            set { tab_num += value; }
        }
        public static TabPage Return_RTB_Tab_Object()
        {
            var tab = Create_Default_Tab_Page();
            var rtb = Create_Default_RTB();
            tab.Controls.Add(rtb);
            tab.Tag = rtb;
            Increment_Tab_Num = 1;
            return tab;
        }
        private static TabPage Create_Default_Tab_Page()
        {
            TabPage t = new TabPage
            {
                BackColor = SystemColors.Control,
                Location = new Point(4, 22),
                Padding = new Padding(3),
                Size = new Size(744, 465),
                Name = "tb" + tab_num.ToString(),
                Text = "tb" + tab_num.ToString(),
                TabIndex = 0
            };
            return t;
        }
        public static void Close_Currently_Selected_Tab_Page(TabControl ctrl)
        {
            if (ctrl.TabCount > 1)
            {
                ctrl.SelectedTab.Dispose();
            }
        }
        private static RichTextBox Create_Default_RTB()
        {
            RichTextBox r = new RichTextBox
            {
                Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right),
                Location = new Point(6, 6),
                Size = new Size(732, 453),
                TabIndex = 2,
                Font = new Font("Microsoft Sans Serif", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))),
                Name = "rtb" + tab_num.ToString()
            };
            return r;
        }

    }
   



Is this a proper time to use static classes?

THANKS IN ADVANCE GUYS

Is This A Good Question/Topic? 0
  • +

Replies To: Should I Use Classes The Ways I Have Been?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: Should I Use Classes The Ways I Have Been?

Posted 04 March 2013 - 06:27 PM

Quote

For instance I have a class that helps output a string of the current font used and it gets fired every single time someone types a letter in the richtextbox like so

You would be better off making a static method in this. That way you don't create an instance of hte class every time you want perform that function.

Quote

Second I hope I haven't become too obsessive

You have. A class for one method is... well... silly.
Take your design queues from the .NET framework. Notice how they group large numbers of things in a namespace or class because they make sense?
  • System.IO.Directory -
  • System.IO.Ports.Serial -
  • System.Windows.Forms -


Last but not least-
I don't think you really grasp the concept of static.
Think of it as one instance that shares everything across multiple uses. If you make a class static you can't make unique individual instances of it.
If you make a static DodgeRam class, then there will only every be one DodgeRam. You can't make a white one, and a blue one and a green one.

The way you are making these static classes to form a specific styled control doesn't make sense in the real world. Why would you want to make numerous RichTextBoxes that all are located at 6,6?

Frankly, the way you are doing this I think you would be better off with WPF where you can define a style for a control that includes the color, size, border curve radius and so on. That way you can make a BlueButton style and GlassButton style etc.
Was This Post Helpful? 2
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Should I Use Classes The Ways I Have Been?

Posted 05 March 2013 - 03:32 AM

View PosttlhIn`toq, on 04 March 2013 - 06:27 PM, said:

Quote

For instance I have a class that helps output a string of the current font used and it gets fired every single time someone types a letter in the richtextbox like so

You would be better off making a static method in this. That way you don't create an instance of hte class every time you want perform that function.

Quote

Second I hope I haven't become too obsessive

You have. A class for one method is... well... silly.
Take your design queues from the .NET framework. Notice how they group large numbers of things in a namespace or class because they make sense?
  • System.IO.Directory -
  • System.IO.Ports.Serial -
  • System.Windows.Forms -


Last but not least-
I don't think you really grasp the concept of static.
Think of it as one instance that shares everything across multiple uses. If you make a class static you can't make unique individual instances of it.
If you make a static DodgeRam class, then there will only every be one DodgeRam. You can't make a white one, and a blue one and a green one.

The way you are making these static classes to form a specific styled control doesn't make sense in the real world. Why would you want to make numerous RichTextBoxes that all are located at 6,6?

Frankly, the way you are doing this I think you would be better off with WPF where you can define a style for a control that includes the color, size, border curve radius and so on. That way you can make a BlueButton style and GlassButton style etc.


Thanks for your help friend; that makes sense. I use classes for more elaborate things too but I can see where you are coming from I'm overdoing it for just one method there...I'm curious what you mean about the RTB being at 6,6? I'm doing this because the user is creating another tab in my program and it opens up another tab in the tab control identical to the preceding one. Why is that bad?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: Should I Use Classes The Ways I Have Been?

Posted 05 March 2013 - 08:32 AM

Picture this...
  • 3 months from now you've made significant upgrades to this app. The tabs now resize and scale when the window resizes. 6,6 is no longer the position of the control on the first tab.
  • What if you upgrade this so the user can reposition all the controls on tab one?


If you want the new control to be the same size and location as its counterpart on the first tab, then get the properties right off the one on the first tab.

Also, hand coding for every control is a maintenance nightmare. If you update tab one's features then you have to recode this part too.
Assuming the new tab is a complete copy of the first tab, then do that. If you make a proper .Clone() method that loops through the .Controls of the first tab and duplicate them on the second tab, using their .Type to dynamically make a new control. HEck... double check... maybe a tabpage already has a .Clone() method...I forget. If it does you can use it. If it doesn't, write one.
Was This Post Helpful? 1
  • +
  • -

#5 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Should I Use Classes The Ways I Have Been?

Posted 05 March 2013 - 12:52 PM

I second tlhIn`toq's sentiments. You are getting carried away, and I think you are missing the point.

The thing you have to realize about classes is that they are a tool for managing complexity, and making our lives easier as developers. Your CFontChangePrompt<T> class hierarchy is completely opposed to this idea. It makes an incredibly simple bit of code, extremely complicated - (see Accidental Complexity). Worse than that actually, you've made it completely confusing.

That code that brings up the font dialog is perfectly okay in the UI class, since the UI class's job is to handle the UI :)

Quote

Is this a proper time to use static classes?


As has been mentioned, there is no need for you to use static where you have, and hiding all that object creation logic behind a wall of static methods is very inflexible, and unnecessary. In this case, as you are creating UI controls, keep that code in the UI class where it belongs.

My general opinion/advice on static methods (and static classes) is that they do still have a place, but you should think hard before finally opting for a static method or class. Sometimes it is the right solution, but many more times, it isn't.

Put simply, static members aren't OO. If you designing an application using the OO paradigm, and you want it to be flexible and testable, your use of static members should be limited.

This post has been edited by CodingSup3rnatur@l-360: 05 March 2013 - 01:07 PM

Was This Post Helpful? 1
  • +
  • -

#6 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Should I Use Classes The Ways I Have Been?

Posted 06 March 2013 - 12:58 PM

View PosttlhIn`toq, on 05 March 2013 - 08:32 AM, said:

Picture this...
  • 3 months from now you've made significant upgrades to this app. The tabs now resize and scale when the window resizes. 6,6 is no longer the position of the control on the first tab.
  • What if you upgrade this so the user can reposition all the controls on tab one?


If you want the new control to be the same size and location as its counterpart on the first tab, then get the properties right off the one on the first tab.

Also, hand coding for every control is a maintenance nightmare. If you update tab one's features then you have to recode this part too.
Assuming the new tab is a complete copy of the first tab, then do that. If you make a proper .Clone() method that loops through the .Controls of the first tab and duplicate them on the second tab, using their .Type to dynamically make a new control. HEck... double check... maybe a tabpage already has a .Clone() method...I forget. If it does you can use it. If it doesn't, write one.


I have no idea why I didn't think of this? I do apologize for my ignorance but you are totally right about that and you are totally right to just use the first tabs properties...I just don't understand why I didn't think of that ughs. Good Suggestions!!

ALSO there isn't a clone method but I can write my own. Is it possible to extend the methods in something like a tabpage using that class so you could just type Tabpage.clone(). IF so how would you go about doing that? Otherwise I will just create a normal class

This post has been edited by adn258: 06 March 2013 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: Should I Use Classes The Ways I Have Been?

Posted 06 March 2013 - 01:49 PM

Just a good example of why we say no amount of reading can equal building, building and more building. There's no educational experience the compares to coding yourself into a corner and having to find a solution... or dropping 100 hours on a brute force method only to have some smart arse say "what about these 10 lines as a more elegant way?" Both are lessons that sink in and aren't soon forgotten.
Was This Post Helpful? 3
  • +
  • -

#8 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Should I Use Classes The Ways I Have Been?

Posted 06 March 2013 - 06:47 PM

View PosttlhIn`toq, on 06 March 2013 - 01:49 PM, said:

Just a good example of why we say no amount of reading can equal building, building and more building. There's no educational experience the compares to coding yourself into a corner and having to find a solution... or dropping 100 hours on a brute force method only to have some smart arse say "what about these 10 lines as a more elegant way?" Both are lessons that sink in and aren't soon forgotten.



I created this and notice I'm adding an event to the richtextbox upon creation and I want it to change the toolstrip label to the current font in the selection of the new textbox in the tab. I could have duplicated the properties using getproperties but this actually seems to amount to more lines of code than what I have here. I'm more worried about that event but one event per new tab with a RichTextBox but I'm assuming that wouldn't be a problem since I have a dipose tab.
 class CDuplicateTab
    {
        private RichTextBox Original_RTB;
        private TabPage Original_Tabpage;
        private  int tab_num = 2;
        private string tabStr;
        private string rtbStr;
        private ToolStripLabel ftn_lbl;

        public CDuplicateTab(TabPage Tabpage_To_Clone, ToolStripLabel Font_Label)
        {
            this.Original_Tabpage = Tabpage_To_Clone;
            this.Original_RTB = (Tabpage_To_Clone.Tag as RichTextBox);
            this.ftn_lbl = Font_Label;
        }

        private int Set_Tab_Rtb_Str_From_Tab_Number_And_Increment
        {
            set
            {
                this.tabStr = "tp" + this.tab_num.ToString();
                this.rtbStr = "rtb" + this.tab_num.ToString();
                this.tab_num += value;
            }
        }

        private RichTextBox Duplicate_RTB()
        {
            RichTextBox r = new RichTextBox();
            r.Font = this.Original_RTB.Font;
            r.Size = this.Original_RTB.Size;
            r.Location = this.Original_RTB.Location;
            r.TabIndex = 0;
            r.Name = rtbStr;
            return r;
        }

        private TabPage Create_Duplicate_Tabpage_Add_RTB()
        {
            var rtb = this.Duplicate_RTB();
            rtb.Selectionchanged += (o, e) =>
                {
                    this.ftn_lbl.Text = rtb.SelectionFont.ToString();
                };
            TabPage t = new TabPage();
            t.Size = this.Original_Tabpage.Size;
            t.BackColor = this.Original_Tabpage.BackColor;
            t.Padding = this.Original_Tabpage.Padding;
            t.Name = this.tabStr;
            t.Text = this.tabStr;
            t.Controls.Add(rtb);
            t.Tag = rtb;
            return t;
        }

        public TabPage Create_Tabpage()
        {
            this.Set_Tab_Rtb_Str_From_Tab_Number_And_Increment = 1;
            return this.Create_Duplicate_Tabpage_Add_RTB();
        }
        
    }



Then I just create and object instance during the main form like so

public partial class main_frm : Form
    {
        CDuplicateTab cdt;
        public main_frm()
        {
            InitializeComponent();
            this.tb1.Tag = this.rtb1;
            cdt = new CDuplicateTab(this.tb1,this.tlstrp_lbl_currentfnt);
           
        }

        private void tlstrp_btn_new_tab_Click(object sender, EventArgs e)
        {
            this.Create_Tab();
        }

        private void Create_Tab()
        {
            this.tbctrl_main.Controls.Add(cdt.Create_Tabpage());
        }




I would definitely assume this is a great time to use a class like I did with essential encapsulation of private methods and one public method exposed for example. That said that event if someone would explain why or why that isn't safe that would help a lot.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: Should I Use Classes The Ways I Have Been?

Posted 06 March 2013 - 07:02 PM

If all these tabs are just copies of a set format, then why not make a UserControl? You can design the look, fonts, placement, custom events... everything then just make new instances of the UserControl.

The tab represents an object of some type, right? So one object means one control to represent it.
Was This Post Helpful? 1
  • +
  • -

#10 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Should I Use Classes The Ways I Have Been?

Posted 07 March 2013 - 02:13 AM

View PosttlhIn`toq, on 06 March 2013 - 07:02 PM, said:

If all these tabs are just copies of a set format, then why not make a UserControl? You can design the look, fonts, placement, custom events... everything then just make new instances of the UserControl.

The tab represents an object of some type, right? So one object means one control to represent it.


That wouldn't work I don't think because a tabpage can't be added to a usercontrol without a tabcontrol or whatever correct? The object is a tabpage with a richtextbox but you can't make a user control out of that unless I'm missing something? Otherwise my above event should be safe and all.

This post has been edited by adn258: 07 March 2013 - 02:14 AM

Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: Should I Use Classes The Ways I Have Been?

Posted 07 March 2013 - 07:30 AM

No. Make a UserControl with the things you need. Then put the UserControl on the new tabpage.

The objct is a tabpage with a RTB? I doubt it. The object is what the user should be dealing with. It might be a document. It might be an invoice. it might be a movie review.

What is the PURPOSE of the tabpage? What does it display or manage?
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Should I Use Classes The Ways I Have Been?

Posted 07 March 2013 - 08:03 AM

tlhIn`toq gives good advice, but I can sympathize with the dilemma that adn258 is going through since I've had to deal with similar UI. The issue is that he is trying to do something like:
Form
+-RichTextToolBar
+-TabControl
  +-TabPage
  | +-RichTextBox
  +-TabPage
  | +-RichTextBox
  :


When the focus is on any given RichTextBox within a TabPage, the RichTextToolBar two levels up needs to reflect the current styles, and also be able to apply new styles. When the focus switches to a different tab, the previous tab's styles should not be affected.

I tried to convince the UI designer that this is easier to implement. But he (correctly) pointed out that it was ugly:
Form
+-TabControl
  +-TabPage
  | + UserControl
  | | +-RichTextToolBar
  | | +-RichTextBox
  +-TabPage
  | + UserControl
  | | +-RichTextToolBar
  | | +-RichTextBox
  :


Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: Should I Use Classes The Ways I Have Been?

Posted 07 March 2013 - 08:19 AM

What's ugly about that? Its clean design.

Again, if all that care and concern is being spent on the theme and style of the GUI then move the project to WPF where you can define a style. You can even design 5 styles and let the user decide what they like: BlueGlass, RedBrick, PolishedMetal, AlienGreen, SystemDefault

All this work on doing this in WinForms is like spending 5,000 hours to stylize a 1980 Ford Fiesta.


Was This Post Helpful? 1
  • +
  • -

#14 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Should I Use Classes The Ways I Have Been?

Posted 07 March 2013 - 11:03 PM

View PostSkydiver, on 07 March 2013 - 08:03 AM, said:

tlhIn`toq gives good advice, but I can sympathize with the dilemma that adn258 is going through since I've had to deal with similar UI. The issue is that he is trying to do something like:
Form
+-RichTextToolBar
+-TabControl
  +-TabPage
  | +-RichTextBox
  +-TabPage
  | +-RichTextBox
  :


When the focus is on any given RichTextBox within a TabPage, the RichTextToolBar two levels up needs to reflect the current styles, and also be able to apply new styles. When the focus switches to a different tab, the previous tab's styles should not be affected.

I tried to convince the UI designer that this is easier to implement. But he (correctly) pointed out that it was ugly:
Form
+-TabControl
  +-TabPage
  | + UserControl
  | | +-RichTextToolBar
  | | +-RichTextBox
  +-TabPage
  | + UserControl
  | | +-RichTextToolBar
  | | +-RichTextBox
  :



That's correct and yes the object is heavily based on a tabpage since I want the user to be able to open up NEW TABS and the way to reference the font etc. of the RTB is by adding a tag to the new tabpage of the RTB.

This can't be done in a user control but perhaps this guy is right. Maybe windows forms is just getting dated and I should look in WPF
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Should I Use Classes The Ways I Have Been?

Posted 07 March 2013 - 11:14 PM

Yes, abandon WinForms and go WPF.

WPF subtly pushes you towards better code designs. If you find yourself writing code that looks like WinForms code in WPF, there are two possibilities: you are doing things wrong, or you are overriding the default behavior of a control. More likely than not, it's the former. In my experience, the need the override the default behavior of a control only occurs when you are trying to implement a Silverlight, Windows 7, or Web 2.0-like effect in WPF because the control doesn't exist (yet). (For example, implementing your own autocomplete in earlier versions of WPF.)
Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »