• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Moving code from a main class to a child class

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
JorieUCL
Asked:
JorieUCL
  • 4
  • 3
2 Solutions
 
käµfm³d 👽Commented:
Can you post an example of your code?
0
 
JorieUCLAuthor Commented:
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
 
käµfm³d 👽Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
käµfm³d 👽Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
JorieUCLAuthor Commented:
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
 
käµfm³d 👽Commented:
>>  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
 
JorieUCLAuthor Commented:
Thanks kaufmed and idle_mind, i both gave you points for this question. I hope that's ok.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now