Simple scope question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bobneedshelp
    New Member
    • Feb 2007
    • 17

    Simple scope question

    I come from a Java background so this a puzzle to me. I'm wondering why this happens in .net. Why do I have use the class name in order to get the variables value in .net?

    Code:
    Public Class Form1
        Public varPub as String = "PUBLIC"
        Private Sub Main
            varPub = "PUBLIC"
            subClass = New SecondClass()
            subClass.IsIT()
        End Sub
    End Class
    
    Public Class SecondClass
        Inherits Form1
        Public Sub IsIT()
            MessageBox.Show(varPub) 'Shows blank
            MessageBox.Show(Form1.varPub) 'shows value
        End Sub
    End Class
  • mmaslar
    New Member
    • Aug 2008
    • 8

    #2
    In your "SecondClas s" class, varPub is not defined. (In fact, there ought to be a warning to that effect on MessageBox.Show (varPub).

    So, it's necessary to specify the class where the variable is actually defined.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      You went from java to vbnet? I would have thought c# was the natural transition for a java person.

      At any rate that is interesting that a derived class does not get the default value of the base class. Hmm

      I think you doctored your source code(possibly to make it be an "example") when you copied it over, because the C# version of that doesn't even compile.

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Well, part of the problem is the way VB.NET likes to hold people's hands, trying to make it easy for you, but instead making it easy to misunderstand and make mistakes.

        Technically, you shouldn't be able to call Form1.varPub in SecondClass. The only way you should be able to do that is if it were declared as Shared (static). What VB is doing for you is creating an instance of Form1 and getting the public member varPub for you.

        As to the other side of the problem, I'm not too sure. I put together a sample C# version of your program to test your problem, and it works fine for me.
        Code:
        public class Program
        {
            public string varPub = "PUBLIC";
        
            static void Main(string[] args)
            {
                subClass c = new subClass();
                c.IsIT();
                Console.ReadKey();
            }
        }
        
        public class subClass : Program
        {
            public void IsIT()
            {
                Console.WriteLine(varPub);
            }
        }

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Originally posted by mmaslar
          In your "SecondClas s" class, varPub is not defined. (In fact, there ought to be a warning to that effect on MessageBox.Show (varPub).

          So, it's necessary to specify the class where the variable is actually defined.
          That shouldn't be right, because his SecondClass inherits Form1. It should inherit all it's members and methods as well.

          EDIT:
          I know that's not correct, because I just put this test together in VB.NET:
          Code:
          Module Module1
          
              Sub Main()
                  Dim c As New Class2
                  c.IsIT()
                  Console.ReadKey()
              End Sub
          
          End Module
          
          Public Class Class1
              Public varPub As String = "PUBLIC"
          End Class
          
          Public Class Class2
              Inherits Class1
              Public Sub IsIT()
                  Console.WriteLine(varPub)
              End Sub
          End Class
          And it works like expected. I'm not quite sure what's wrong with OP's code. I'd try to imitate it, but it seems like a strange combination of a console app and a windows forms app.

          Can you clarify what you are doing?

          Comment

          • bobneedshelp
            New Member
            • Feb 2007
            • 17

            #6
            Originally posted by insertAlias
            That shouldn't be right, because his SecondClass inherits Form1. It should inherit all it's members and methods as well.

            EDIT:
            I know that's not correct, because I just put this test together in VB.NET:
            Code:
            Module Module1
            
                Sub Main()
                    Dim c As New Class2
                    c.IsIT()
                    Console.ReadKey()
                End Sub
            
            End Module
            
            Public Class Class1
                Public varPub As String = "PUBLIC"
            End Class
            
            Public Class Class2
                Inherits Class1
                Public Sub IsIT()
                    Console.WriteLine(varPub)
                End Sub
            End Class
            And it works like expected. I'm not quite sure what's wrong with OP's code. I'd try to imitate it, but it seems like a strange combination of a console app and a windows forms app.

            Can you clarify what you are doing?
            C# was my choice but the boss said vb. The setup is a generic service form calling a class
            to run applicaitons.

            I have several applications that use the same fields to insert data into a database. I created a separate class to do this. The initial idea was to pass a structure and that
            didn't work at all. It would be easier on the eyes to see the structure passed and then manipulate the data instead of passing ten variables. The function gets called multiple times.

            The next step was to get the variable information from the original class. I thought you could inherit a class and access the variables the same way you could in java. At least that's what my vb book says. I added this to my test application to see how to get the data over. The only way to do it was to add the class name in front of the variable.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              I did the following:
              [code=c#]
              secondclass cd = new secondclass();
              cd.testit();
              [/code]
              [code=c#]
              public class baseclass
              {
              protected string somestr = "INIT VALUE";

              public baseclass()
              {
              somestr = "baseclass constructor";
              }
              }
              public class secondclass : baseclass
              {
              public void testit()
              {
              System.Windows. Forms.MessageBo x.Show(somestr) ;
              }
              }
              [/code]

              And the output was "baseclass constructor", so even in an inherited class, the base class's constructor is called. If that helps at all.
              I had to declare the member variable as "protected" to keep it hidden(not public) but to allow it to be visible to the derrived class.

              Comment

              • bobneedshelp
                New Member
                • Feb 2007
                • 17

                #8
                That works for me in an application but for some reason not in a service. I am using a namespace for the service and those seem to affect how variables can be accessed. The fix was making the variable shared. I'll try to figure out why later but it's working now.

                Thanks for the help.

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Oooo some of those terms public/private/static/protected/internal and etc effect if a class with another namespace (and/or different "assembly") can access the member.
                  I don't know much about using them in "fancy" ways

                  Comment

                  Working...