Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Moving code from a main class to a child class

Posted on 2011-03-23
8
234 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show/Hide ASP.NET controls with JQuery best practices 3 86
Store results in vb.net 3 24
VB.Net Tranfer data between to VB.Net apps 8 38
vb.net class 3 21
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 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