Variable scope within classes

tomhosking

Well-known member
Joined
Aug 4, 2005
Messages
49
Programming Experience
1-3
I have a form and a class module, with basic controls for a log text box. When Button1 is clicked, the text in txtIn should be added to txtLogBox. The class module contains an overall class, and a
errorhandler class. I want the errorhandler class to be a subset of the overall class, and also to be able to "see" the form like the overall class can. I tried causing the sub class to inherit the overall one, but presumably that just sends it into an endless loop,
since each one will then create the other.

Effectively, I want to create objects within a class that can use variables from the parent, such as a form pointer.

Any help on this matter would be much appreciated!
 
I'm afraid that your post doesn't make much sense. Your terminology is not standard so it is difficult to determine exactly what the relationships are. Can you explain again more clearly?
 
Sorry about that, Im still very new to VB, having come from languages like PHP/Perl.


My form code looks like this:
VB.NET:
[/size]
[size=1]Public [color=#0000ff]Class[/color] Form1[/size]

[size=1][color=#0000ff]Inherits[/color] System.Windows.Forms.Form[/size]

[size=1][color=#0000ff]Public[/color] thisSession [color=#0000ff]As[/color] [color=#0000ff]New[/color] clsSession[/size]

[size=1][color=#0000ff]Public[/color] temForm1 [color=#0000ff]As[/color] Form1[/size]

[size=1][color=#0000ff]Public[/color] [color=#0000ff]Sub[/color] Form1_Load([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] [color=#0000ff]MyBase[/color].Load[/size]

[size=1]thisSession.showform([color=#0000ff]Me[/color])[/size]

[size=1][color=#0000ff]End[/color] [color=#0000ff]Sub

[/color][/size][size=1][color=#0000ff]Private[/color] [color=#0000ff]Sub[/color] Button1_Click([color=#0000ff]ByVal[/color] sender [color=#0000ff]As[/color] System.Object, [color=#0000ff]ByVal[/color] e [color=#0000ff]As[/color] System.EventArgs) [color=#0000ff]Handles[/color] Button1.Click[/size]

[size=1]thisSession.changetext(txtIn.Text)[/size]

[size=1][color=#0000ff]End[/color] [/size][color=#0000ff][size=1]Sub[/size]

[size=1]End[/size][/color][size=1] [color=#0000ff]Class

[/color][/size][size=1]


and my class file like this:

VB.NET:
 Public Class clsSession [/size]
[size=1]Public Shared temForm1 As Form1[/size]

[size=1]Public objError As New clsErrorHandler[/size]

[size=1]Public Function showform(ByVal frm As Form1)[/size]

[size=1]temForm1 = frm[/size]

[size=1]End Function[/size]

[size=1]Public Sub changetext(ByVal value As String)[/size]

[size=1]temForm1.txtLogBox.Text = temForm1.txtLogBox.Text & value[/size]

[size=1]End Sub[/size]

[size=1]End Class[/size]

[size=1]Public Class clsErrorHandler : Inherits clsSession[/size]

[size=1]Public Sub AddDebug(ByVal logtext As String)[/size]

[size=1]thisSession.temForm1.txtDebugBox.Text = temForm1.txtDebugBox.Text & TimeOfDay() & " : " & logtext & Chr(13) & Chr(10)[/size]

[size=1]temForm1.txtDebugBox.SelectionStart = Len(temForm1.txtDebugBox.Text)[/size]

[size=1]temForm1.txtDebugBox.ScrollToCaret()[/size]

[size=1]End Sub[/size]

[size=1]End Class[/size]

[size=1][size=1]


So I create a session object onLoad, which contains a number of useful functions eg for debugging, as well as many other classes that do the main work (not included yet).

I want the errorhandler class to be able to use temform1, but telling clsErrorHandler to inherit clsSession causes an infinite loop (each one now creates an instance of the other).

I have been told by someone that this is not possible in a language like VB, and am about to restart the project, much of which I have written, in C#, which I am told is a truer OOPL. However, if possible I would like to use the code I have in VB... [/size]
 
Whoever told you that about VB.NET is a bigot who is judging VB.NET by the standards of VB6. VB.NET is a fully OO language and pretty much anything that can be done in C# can be done in VB.NET.

I've got to say, I still don't fully understand what you're trying to achieve, but it seems rather odd that the ErrorHandler class is inheriting from a base class that has a member variable that is an ErrorHandler. It's like looking into a mirror with another mirror behind you and seeing reflections of yourself into infinity. I'd say that the reason you have run into this issue is that your design is flawed. Sorry, but that's how it seems to me. You'd have the same issue with C#.
 
What I want is to be able to call handleError.AddDebug("Errormessage") from anywhere in the program, so that I can use it as a debugging tool. However, limitng myself to one such method would be a bit poor, and placing them all in the main form isnt making use of the OOPL. Therefore, I create a base object, from which other objects are created, eg a debugging class, a "measurements" class, a class with various useful string methods etc. To do this, the subclasses need to be able to use the form's properties/methods. It would be possible to define a proxy variable to the form each time I need it, but that's decidedly inefficient and likely to cause problems.

My eventual aim is to set up a system where you could call

sources(4).targets(9).move()

and the controlled item (in my case a theodolite) would move from source 4 to target 9. That seems to me a better way than doing move(4,9,loads of other parameters) where it would be easy to get confused. As I said, I am still a beginner to OOP, maybe Im still thinking in terms of procedural languages...
 
tomhosking said:
maybe Im still thinking in terms of procedural languages...
I think you might be. It sounds like what you want is for everything to be able to see everything, which is one of the things that VB6 was, and is, criticised for. If you want properties or methods to be visible project-wide, you can put them in a module. A module is a VB-specific construct, but it is equivalent to a class where all members are Shared (or static in C#), and is, in fact, implemented as such when compiled. You shouldn't just be putting everything into a module though because you will be breaking the rules of OOP.

As for debugging, you already have classes included in the .NET Framework that are designed to aid you there, like Debug and Trace. VB.NET also supports conditional compilation, like C-based languages.
 
I dont necessarily want everything visible everywhere, just the things that I choose, such as the Form object, or the error handler.

The errors that I will be dealing with come from an external device, so I need to set up ways of dealing with them. Putting the code in every place that might conceivably need it is hugely inefficient and liable to get omitted accidentally.
 
Declare the class where? If I declare it in the form, its only visible there, if I declare it elsewhere then its only visible there.

if I create thisSession in the form, then try to use thisSession.changetext() within a class it cant.
 
Back
Top