Solved

Moving code from a main class to a child class

Posted on 2011-03-23
8
240 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 86

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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