Question scope of variables causing me difficulties (unnecessarily)

Dave Kimble

Member
Joined
Nov 20, 2012
Messages
8
Programming Experience
10+
Due to the constraints forced upon me by the structure of OOP programs
and the constraints of VB.NET's scoping of variables,
problems with variables' scope are getting in the way of me doing any work.

I can appreciate that if you have many people writing different parts of the same program,
then scoping the variable might avoid name clashes, but is there any way of switching scoping off when there is only one person writing the whole thing ?

Alternatively, is there a syntax that makes all variables accessible for everywhere?

Specifically, this doesn't work - the socket opened has port=0 :

VB.NET:
Public Class insserver
    Public Shared port As Integer
    Public Shared server As New System.Net.Sockets.TcpListener(System.Net.IPAddress.Parse("127.0.0.1"), port)
    
    Private Sub doConfig()
        port = 'something
    End Sub 
    
    Private Sub openSocket()
        server.Start()
    End Sub
End Class

and this doesn't even compile :

VB.NET:
Public Class insserver
    Public Shared port As Integer
        
    Private Sub doConfig()
        port = 'something
    End Sub 
    
    Private Sub openSocket()
        Public Shared server As New  System.Net.Sockets.TcpListener(System.Net.IPAddress.Parse("127.0.0.1"), port)
        server.Start()
    End Sub
End Class

and this doesn't allow other subs to access server :

VB.NET:
Public Class insserver
    Public Shared port As Integer
        
    Private Sub doConfig()
        port = 'something
    End Sub 
    
    Private Sub openSocket()
        Dim server As New  System.Net.Sockets.TcpListener(System.Net.IPAddress.Parse("127.0.0.1"), port)
        server.Start()
    End Sub
End Class
 
Yes, that's more or less what I'm doing. Isn't that allowed ? :wink:
You most certainly can mix and match code from different sources like this, the catch is that you need to know exactly what every part of all of the code does for you to be able to get it to work right here, which I believe is your biggest problem here, you've gotten code that declares and instantiates a variable outside of the openSocket() and then you got code inside the openSocket() that declares and instantiates a variable with the same name and of course that's simply not going to work (not even in php).
I have the programs (server and client) already written in PHP, so I just have to learn the new rules.
It's actually a much larger paradigm shift than just learning a couple of new rules ;)
For one, .Net is a compiled language environment and php is pure script, therefor you shouldn't assume there's any kind of similarity between the two and I think you have a lot of assumptions about .Net compared to php.
Did you know you don't have to use classes, methods, properties etc. just to write a GUI app ? In fact it makes it MUCH harder and less readable and more error-prone. Enforced variable typing makes it harder again. On the other hand VB.NET proves you don't need $ and ; littered everywhere to write a language that a compiler can understand.
Did you know that just by creating a new vb.net project you're using at least 1 class, so on a technical level you cannot say "You don't have to use classes" because your project will be, by default, using a class and there's nothing you can do about it ;)
I've actually been having a few of the reverse problems in my php university class, I've got a variable outside of a function that I want to have a local copy of inside of the function so I do just that assuming that the inside function's variable is of different scope and is a different variable altogether (if it would have me declare the variable inside of the function it could easily tell me that it's going to overwrite the outside of the functions variable but since scope is something php doesn't have, it allows my bad-for-php coding to run and not give any warnings or errors like I'd like it to) it indeed runs but the results are completely different and it took me a while to figure out that php doesn't have scope, so I renamed the function's variable giving it a "1" at the end of it's name so that it was, indeed, a different variable.
Another thing I've learned about it is that there's no variable types, like at all, so a variable with the name $YourName could be a string at first, but then later be assigned an integer and then later could be assigned a database row array when it seems that when $YourName exists it should always be a string, it should force you to cast non-string values to a string before allowing you to assign a new value, but that's a major paradigm difference between compiled and scripted languages (I have the issue with Javascript and vbscript too). I have been making a point though to include a variable type in the variables name like $YourNamString as a self documenting way of stating $YourName should always only ever be a string.

Also some of the huge benefits of using classes include:
Code organization
Inheritance
Polymorphism
Variable scope
My original problem was that if I declared "server" in openSocket(), I couldn't use it in monitorSocket() - a scoping problem. Any fool can see that the "server" in monitorSocket() is the same thing as the "server" in openSocket(), but the VB.NET compiler doesn't see it that way. It's a hindrance rather than a help.
It's only a hindrance if you don't understand the language/environment you're coding in, when you declare a variable inside a sub/function as soon as the code exits that sub/function it no longer exists, it has fallen out of scope, so therefor no other subs/functions can see/use that variable. What you can & should do is declare the variable outside of the subs/functions & in one sub/function you can instantiate (create the variable aka set it to a value) and in another function use it. Just be sure to call the sub that instantiates it before you call the sub that tries to use it. Creating a constructor can be a good way to ensure the variables are instantiated to some sort of value, creating properties is a great way to allow those values to be changed & you can do validation on the new values in those properties to ensure the values meet the criteria and if not you can reject the new value.
Some tutorial said "Public shared" had the widest scope.
I would be curious to know where you heard that shared has anything to do with variable scope because it doesn't. Straight from Microsoft's documentation:
MSDN said:
Sharing does not alter the access level of a member. For example, a class member can be shared and private (accessible only from within the class), or nonshared and public. For more information, see Access Levels in Visual Basic.
But for your reference here's what it means to use the shared keyword:
MSDN said:
Sharing a member of a class or structure makes it available to every instance, rather than nonshared, where each instance keeps its own copy.
Basically that means if you have a shared variable in one of your classes (doesn't matter what the variable scope is) if you have multiple instances of that class then all of those instances have the same value for that variable & if you change that value in one of the instances it's changed in all of them because that variable is shared among all of them. Scope of course is the level of access outside of that class, ie if the variable is declared as private then it can only be accessed inside that class (if the variable is declared inside a sub/function, it can only be accessed inside that sub/function) whereas if the variable is declared as public then it can be accessed inside and outside the class. There are a couple of other levels, but I think you get the gist of it here.

Full article here: Shared (Visual Basic) - MSDN
 
Did you know that just by creating a new vb.net project you're using at least 1 class, so on a technical level you cannot say "You don't have to use classes" because your project will be, by default, using a class and there's nothing you can do about it

True for .NET languages, but I only said "to write a GUI app". PHP-WinBinder produces an executable file with calls to a pre-compiled php.dll library and the Windows API, and there is not a class, method or property in it. FreeBASIC_GUI seems to do the same sort of thing. Both suffer from not having been given that Microsoft "polish" in terms of installing the compiler and dependencies in the right hierarchies automatically, documentation and so on, and are effectively dead which is a pity.
 
Back
Top