Solved

Moving code from a main class to a child class

Posted on 2011-03-23
8
199 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 74

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 74

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 74

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 74

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

15 Experts available now in Live!

Get 1:1 Help Now