By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

Object References

P: 6
Hello every body
I have defined a class in VB. Its name is "Gens" with one property "property Fit as double"
now I have:
Expand|Select|Wrap|Line Numbers
  1. dim a,b as new gens
  2. a.fit=25
  3. b=a
  4. a.fit=30
  5. 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
Jun 12 '13 #1
Share this Question
Share on Google+
9 Replies


Rabbit
Expert Mod 10K+
P: 12,382
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.
Jun 12 '13 #2

Frinavale
Expert Mod 5K+
P: 9,731
If you want to store different values in both variables, you should declare new instances of the Gens class for each variable.

Like this:
Expand|Select|Wrap|Line Numbers
  1. Dim a As New Gens
  2. Dim b As New Gens
  3.  
  4. b.fit=25
  5. a.fit=30
  6. MessageBox.Show(a.fit)
  7. 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:
Expand|Select|Wrap|Line Numbers
  1. Dim a As New Gens
  2. Dim b As Gens
  3.  
  4. a.fit = 30
  5. b = a
  6. 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:
Expand|Select|Wrap|Line Numbers
  1. Dim a As New Gens
  2. Dim b As Gens
  3.  
  4. a.fit = 30
  5. b = a
  6. b.fit = 25
  7.  
  8. MessageBox.Show(a.fit)
  9. 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:
Expand|Select|Wrap|Line Numbers
  1. Dim a As New Gens
  2. Dim b As Gens
  3.  
  4. b.fit = 25
  5. a.fit = 30
  6.  
  7. b = a
  8. MessageBox.Show(b.fit)
Because b has not been instantiated at the point in the logic where it is being used.

-Frinny
Jun 12 '13 #3

P: 6
Thank you for your reply
simply let say I want to find the maximum of a function.
here is my sample code:
Expand|Select|Wrap|Line Numbers
  1. public class gen()
  2.     property len as double
  3.     property area as double
  4. end class
  5.  
  6. public sub main()
  7.     dim a,b as gen
  8.     a=new gen
  9.     b=new gen
  10.     b.area=-1
  11.     for i as integer=1 to 10
  12.         a.len=random.nextdouble  ' just a random number
  13.         a.area=a.len * a.len
  14.         if a.area>b.area then
  15.             b=new gen
  16.             b=a
  17.         end if
  18.     next
  19.     msgbox ("maximum random Area was=" & b.area)
  20. 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
Jun 12 '13 #4

Frinavale
Expert Mod 5K+
P: 9,731
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=DecimalB....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.
Expand|Select|Wrap|Line Numbers
  1. Public Sub main()
  2.     Dim allGens As New List(Of Gen)
  3.     Dim fixedRand As New Random(CInt(Date.Now.Ticks And &h0000FFFF))
  4.     For i As Integer=1 to 10
  5.         Dim g As New Gen
  6.         g.len = fixedRand.NextDouble()' just a random number
  7.         g.area = g.len * g.len
  8.         allGens.Add(g)
  9.     Next
  10.  
  11.     Dim largestAreaGen As New Gen
  12.     largestAreaGen.area = -1
  13.  
  14.     For Each a As Gen In allGens
  15.         If a.area > largestAreaGen.area Then
  16.          largestAreaGen = a
  17.         End If
  18.     Next
  19.     MessageBox.Show("maximum random Area was=" & largestAreaGen.area)
  20. End Sub
-Frinny
Jun 12 '13 #5

P: 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:
Expand|Select|Wrap|Line Numbers
  1. dim b as new list (of gen)
  2.  
it returns following error message:
Type 'list' is not defined.
(Visual basic 2010 express)

Thanks a lot
Jun 12 '13 #6

Frinavale
Expert Mod 5K+
P: 9,731
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
Jun 12 '13 #7

Frinavale
Expert Mod 5K+
P: 9,731
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.Collections.Generic namespace.

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

-Frinny
Jun 12 '13 #8

P: 6
Dear Frinny
the links was very nice and helpful.
than you so much
Jun 12 '13 #9

P: 6
Thanks again. the problem of 'List' is now solved.
Jun 12 '13 #10

Post your reply

Sign in to post your reply or Sign up for a free account.