Object References

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hoseinbf
    New Member
    • Jun 2013
    • 6

    Object References

    Hello every body
    I have defined a class in VB. Its name is "Gens" with one property "property Fit as double"
    now I have:
    Code:
    dim a,b as new gens
    a.fit=25
    b=a
    a.fit=30
    msgbox (b.fit)
    The problem is that the answer is 30. Even there is no "b=a" after changing "a" but b changes by changing "a" and they are dynamically linked together how I can break this link between a and b?

    THANX A LOT
    Last edited by Frinavale; Jun 12 '13, 06:45 PM.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You need to create a new instance of the class and assign it to one of your varibles. But I don't understand the purpose of this at all. Why link them in the first place.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      If you want to store different values in both variables, you should declare new instances of the Gens class for each variable.

      Like this:
      Code:
      Dim a As New Gens
      Dim b As New Gens
      
      b.fit=25
      a.fit=30
      MessageBox.Show(a.fit)
      MessageBox.Show(b.fit)
      The above code will show 30 in the first message box and 25 in the second message box.


      Now, I'm not sure what you are expecting to happen when you are setting b = a.

      Say you have instantiate a but not b.

      Then you set b = a.

      Like this:
      Code:
      Dim a As New Gens
      Dim b As Gens
      
      a.fit = 30
      b = a
      MessageBox.Show(b.fit)
      Both variables a and b are going to reference the same Object in memory.

      Therefore if you change the property fit in one object, it will also change for another object.

      For example, if you had:
      Code:
      Dim a As New Gens
      Dim b As Gens
      
      a.fit = 30
      b = a
      b.fit = 25
      
      MessageBox.Show(a.fit)
      MessageBox.Show(b.fit)
      You would see two message boxes and they would both be displaying 25 because both variables reference the same Object in memory.

      Now, you will SHOULD get a null reference error if you do this:
      Code:
      Dim a As New Gens
      Dim b As Gens
      
      b.fit = 25
      a.fit = 30
      
      b = a
      MessageBox.Show(b.fit)
      Because b has not been instantiated at the point in the logic where it is being used.

      -Frinny
      Last edited by Frinavale; Jun 12 '13, 06:59 PM.

      Comment

      • hoseinbf
        New Member
        • Jun 2013
        • 6

        #4
        Thank you for your reply
        simply let say I want to find the maximum of a function.
        here is my sample code:
        Code:
        public class gen()
            property len as double
            property area as double
        end class
        
        public sub main()
            dim a,b as gen
            a=new gen
            b=new gen
            b.area=-1
            for i as integer=1 to 10
                a.len=random.nextdouble  ' just a random number
                a.area=a.len * a.len
                if a.area>b.area then
                    b=new gen
                    b=a
                end if
            next
            msgbox ("maximum random Area was=" & b.area)
        end sub
        Okay, this code do not return maximum value of 'area'. each time that I change 'a', other variable also changes. it seems that in this case, a and b are one variable with different names.
        what modifications I should do?
        Thanks a lot
        Last edited by Frinavale; Jun 12 '13, 07:11 PM. Reason: Added code tags. Please post code in code tags in the future.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          I do not think that you understand pointers.

          I know that it is a c++/c# term, but it is a very important concept to comprehend.

          Whenever you declare a variable for a class, that variable Points to a location in memory where the class's data begins (well, kind of but we'll keep it simple).

          If you set ObjectVariableA = ObjectVariableB you are are changing ObjectVariableA 's pointer to point to the memory location that ObjectVariableB is pointing to.

          Pointers do not apply to primitive variable types like Decimal, or String. If you set DecimalA=5 and DecimalB=10 and then you set DecimalA=Decima lB....and then you change one, the other will not change.

          You really should check out this MSDN article about Value Types and Reference Types


          So, the following code will set the variable largestAreaGen to point to the gen Object instance that contains the largest area.

          It will then print the area property of the gen instance that the largestAreaGen variable points to.
          Code:
          Public Sub main()
              Dim allGens As New List(Of Gen)
              Dim fixedRand As New Random(CInt(Date.Now.Ticks And &h0000FFFF))
              For i As Integer=1 to 10
                  Dim g As New Gen
                  g.len = fixedRand.NextDouble()' just a random number
                  g.area = g.len * g.len
                  allGens.Add(g)
              Next
          
              Dim largestAreaGen As New Gen
              largestAreaGen.area = -1
          
              For Each a As Gen In allGens
                  If a.area > largestAreaGen.area Then
                   largestAreaGen = a
                  End If
              Next
              MessageBox.Show("maximum random Area was=" & largestAreaGen.area)
          End Sub
          -Frinny
          Last edited by Frinavale; Jun 12 '13, 07:37 PM.

          Comment

          • hoseinbf
            New Member
            • Jun 2013
            • 6

            #6
            THank you Frinny
            this is good but in my real code, I am looking inside a very large set of data. (for example between millions instead of '10' in the above code) and I really don't need to keep all of them in a list. each time I want to compare new element with the best existing element and if new element is not desirable I will just forget it.

            on the other hand, when I use the same code as yours:
            Code:
            dim b as new list (of gen)
            it returns following error message:
            Type 'list' is not defined.
            (Visual basic 2010 express)

            Thanks a lot

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              This same concept applies to large sets of data.

              In the example I needed a container for my data and it just so happens to be a List that is 10 objects large.

              If your data is not stored in Objects, then you may not even have cause for concern with the pointers that you are describing.

              There are a Lot of ways to find the largest thing in a list.

              You could use the Enumerable.Max method (Linq)

              You could use the List(T).Sort method

              If your data is in a DataTable, you could just do a Select right on the table data to retrieve the largest thing...


              It all depends on what you are comfortable with and how your data is stored.

              -Frinny

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Wait a sec, List is not defined?
                What .NET framework are you targeting?

                Visual Studio 2010 should be targeting the .Net Framework 4.0....

                Lists are in that Framework.

                The List(T) class is part of the System.Collecti ons.Generic namespace.

                Make sure to include it in your project and in your file.

                -Frinny
                Last edited by Frinavale; Jun 12 '13, 08:00 PM.

                Comment

                • hoseinbf
                  New Member
                  • Jun 2013
                  • 6

                  #9
                  Dear Frinny
                  the links was very nice and helpful.
                  than you so much

                  Comment

                  • hoseinbf
                    New Member
                    • Jun 2013
                    • 6

                    #10
                    Thanks again. the problem of 'List' is now solved.

                    Comment

                    Working...