T
thedillinger
Guest
How to declare a Class that contains other nested Classes and allow use of all variables, subs, etc within everything.
(Mods, I apologise if this has been posted in the wrong forum, I read the announcement and wondered if it should go in .NET Framework Class Libraries, I am too new to programming to know.)
Ok I am stuck, my problem is as described by the title, looking online I have not found an answer.
Here is some of my VB.NET 2012 code to help describe what i am trying to do...
'In Form1.vb (Default Form Code, form has a button control on it) this code works fine with my class...
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim MyNew As New Outer1.Inner2
MyNew.Inner2Sub1()
MyNew.Inner2Sub2()
End Sub
End Class
'In Form1.vb (Default Form Code, form has a button control on it) this code does NOT work...
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim MyNew As New Outer1
MyNew.Inner1.Inner1Sub1() 'gives errors, Error1 = Warning 1 Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated. Error2 = Error 2 Reference to a non-shared member requires an object reference.
MyNew.Inner1Sub1() 'gives error, Error1 = Error 1 'Inner1Sub1' is not a member of projectfilename.Outer1
End Sub
End Class
'In Outer1.vb (An added Class file) ...
Public Class Outer1
'Imagine this Outer1 class had variables and subs too
Public Class Inner1
Public Shared Inner1Var1 As Int32
Public Shared Inner1Var2 As String
Public Sub Inner1Sub1
'Code inserted here
End Sub
End Class
Public Class Inner2
Public Inner2Var1 As Int32
Public Inner2Var2 As String
Public Sub Inner2Sub1
Dim Inner2Sub1Var1 As Int32
'Code inserted here
End Sub
Public Sub Inner2Sub2
Dim Inner2Sub2Var1 As Int32
'Code inserted here
Outer1.Inner1.Inner1Var1 = 10 'note this works and i can successfully set Inner1Var1 value, i can also access the value of Inner1Var1 from here (this point in my code) with another line of code see below
Inner2Sub2Var1 = Outer1.Inner1.Inner1Var1 'this also works
End Sub
End Class
End Class
I am stuck on declaring an instance of my custom made Class in its entirety.
I need to be able to declare my custom made Class in a way to be able to use all variables, subs, etc within the Outer class as well as being able to use use all variables, subs, etc within the Inner nested classes.
If I declare the Outer class I cannot access the Inner classes variables, subs, code.
If I declare the Inner class I cannot access the Outer class or other different nested Inner classes within the Outer class variables, subs, code.
I do not want to declare each class seperately for every single class (all Outer and Inner) as they willl not be able to interact or see each other which is what i want the entire collection of classes to be able to do.
I have heard of Container classes but i do not understand what they are or if they are relevant to my problem.
Continue reading...
(Mods, I apologise if this has been posted in the wrong forum, I read the announcement and wondered if it should go in .NET Framework Class Libraries, I am too new to programming to know.)
Ok I am stuck, my problem is as described by the title, looking online I have not found an answer.
Here is some of my VB.NET 2012 code to help describe what i am trying to do...
'In Form1.vb (Default Form Code, form has a button control on it) this code works fine with my class...
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim MyNew As New Outer1.Inner2
MyNew.Inner2Sub1()
MyNew.Inner2Sub2()
End Sub
End Class
'In Form1.vb (Default Form Code, form has a button control on it) this code does NOT work...
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim MyNew As New Outer1
MyNew.Inner1.Inner1Sub1() 'gives errors, Error1 = Warning 1 Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated. Error2 = Error 2 Reference to a non-shared member requires an object reference.
MyNew.Inner1Sub1() 'gives error, Error1 = Error 1 'Inner1Sub1' is not a member of projectfilename.Outer1
End Sub
End Class
'In Outer1.vb (An added Class file) ...
Public Class Outer1
'Imagine this Outer1 class had variables and subs too
Public Class Inner1
Public Shared Inner1Var1 As Int32
Public Shared Inner1Var2 As String
Public Sub Inner1Sub1
'Code inserted here
End Sub
End Class
Public Class Inner2
Public Inner2Var1 As Int32
Public Inner2Var2 As String
Public Sub Inner2Sub1
Dim Inner2Sub1Var1 As Int32
'Code inserted here
End Sub
Public Sub Inner2Sub2
Dim Inner2Sub2Var1 As Int32
'Code inserted here
Outer1.Inner1.Inner1Var1 = 10 'note this works and i can successfully set Inner1Var1 value, i can also access the value of Inner1Var1 from here (this point in my code) with another line of code see below
Inner2Sub2Var1 = Outer1.Inner1.Inner1Var1 'this also works
End Sub
End Class
End Class
I am stuck on declaring an instance of my custom made Class in its entirety.
I need to be able to declare my custom made Class in a way to be able to use all variables, subs, etc within the Outer class as well as being able to use use all variables, subs, etc within the Inner nested classes.
If I declare the Outer class I cannot access the Inner classes variables, subs, code.
If I declare the Inner class I cannot access the Outer class or other different nested Inner classes within the Outer class variables, subs, code.
I do not want to declare each class seperately for every single class (all Outer and Inner) as they willl not be able to interact or see each other which is what i want the entire collection of classes to be able to do.
I have heard of Container classes but i do not understand what they are or if they are relevant to my problem.
Continue reading...