Solved

Moving code from a main class to a child class

Posted on 2011-03-23
8
212 Views
Last Modified: 2012-05-11
Dear experts,

My question seems to be simple but i find it hard to actually explain what i want to do. Everything i know about vb.net is self thought and i feel i skipped the basics.

My current project is becoming unreadable because most of the code is in the main class together with the interface. So i want to move my code to classes.

Let's say i have a main class and a child class.
I moved my subs and functions to the child class and put inherits main at the top, no errors so far.

now i call the functions in the main class like child.function this also works

The problem is now that the code in the child class the controls are not found (text-boxes and such). What i can do now is edit the child class and put main. in front of every textbox and button. This will take alot of time, is there a better way to do this?


0
Comment
Question by:JorieUCL
  • 4
  • 3
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35198229
Can you post an example of your code?
0
 
LVL 1

Author Comment

by:JorieUCL
ID: 35198586
Thx kaufmed for your comment, my question is more general i guess. but i will post a example
I't might clear things up a little

main is the main form

Public Class main

 Private Sub Button5(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        child.saveplanning()
       
    End Sub
end class

Open in new window


   
      Public Class Child

    Inherits main
    Public Sub saveplanning()
     
        If [b]TextBox25.TextLength[/b] < 1 Then
            MsgBox("error")
                     
        End If
end sub
end class

Open in new window


If i change TextBox25.TextLength into main.TextBox25.TextLength it will work i guess, but if i have to edit this everywhere it will take a lot of time.
can this be done faster

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 150 total points
ID: 35199030
How are you making this call:

child.saveplanning()

Open in new window


What I mean is that you don't automatically have access to the methods of child unless you either have a reference to a child object inside of Main, or you have public static methods inside of child.

I get the feeling you are working with the "default" instance of a form that VB provides to you for free. The default instance points to the "main" form of your application--that is whichever is designated to run when the application starts. When you are calling main.TextBox25.TextLength from within the child form, I believe you are accessing the default instance.

What I think you need to understand is scope and where variables are accessible and where they are not. You generally don't (shouldn't) have access to variables declared in one class from within an external class. This is the concept of data hiding which object-oriented programming proselytizes. Variables should be declared Private and have a Public property which exposes access to them.

With inherited variables, you won't get access to variables marked as Private. This is where the Protected keyword comes into play. A Protected variable is one that is not accessible to external classes; however, it is accessible to child classes. This access modifer is what you would typically use when you want to allow a child form to interact with controls defined in a parent form.

For your example, the controls *should* have been declared as Protected by the designer when you dragged them onto the form. I would inspect the .designer.vb file to see if they indeed are protected.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35199051
Looking back, I didn't exactly cover "scope". I focused more on accessibility. I discussed scope in an article I wrote:    http://www.experts-exchange.com/A_4322.html
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 total points
ID: 35199314
kaufmed is addressing the technical side of your issues.

Your stated core problem, though, is:

    "My current project is becoming unreadable because most of the code is in the main class together with the interface."

Did you know that you can create your own Regions that can be collapsed so they don't take up space in your code editor window?
http://msdn.microsoft.com/en-us/library/6xywk05b.aspx
0
 
LVL 1

Author Comment

by:JorieUCL
ID: 35199568
Thank you all for your comments,

Kaufmed: your article is alot to take in at once but i will definitely read it. I never got the idea of private sub, i thought this was used for performance reasons.

I tried searching trough my main.designer.vb but i did not find the protected controls, i did notice

Private Sub InitializeComponent()
...
all my form components
...

Is this the reason why only the mainform may use the components?

Idle_Mind: I didn't know about the regions, i noticed VS sometimes made them but making them myself will make things easier to read indeed.

I'm not sure what's the right way to keep projects organized. Using lot's of classes like in web development or putting everything in the vb file where they belong.

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35200007
>>  your article is alot to take in at once

I forgot to mention it, but the scope information is right at the beginning under the section "Variable Scope". That section is only a few paragraphs. The rest of the article may be of use to you as well.
0
 
LVL 1

Author Closing Comment

by:JorieUCL
ID: 35229784
Thanks kaufmed and idle_mind, i both gave you points for this question. I hope that's ok.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now