473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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:
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
9 1222
Rabbit
12,516 Expert Mod 8TB
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
9,735 Expert Mod 8TB
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
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
9,735 Expert Mod 8TB
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
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
9,735 Expert Mod 8TB
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
9,735 Expert Mod 8TB
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
Dear Frinny
the links was very nice and helpful.
than you so much
Jun 12 '13 #9
Thanks again. the problem of 'List' is now solved.
Jun 12 '13 #10

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

Similar topics

1
by: Jerry | last post by:
Hi All, How can I prevent a script from running when a previous instance of the script had already been triggered and the script is running in the background already? So, even when a script is...
2
by: Gizmo | last post by:
hi all i have a bit of a prob with some 2 classes iv created if say i have class A and class B. class A hold an atribute of class B and class be Holds an Atribute of class A. both are...
4
by: pheonix1t | last post by:
hello, I'm trying to create a linked server from an SQL2000 to a Unify ELS (very old, odbc is version 1) database on SCO unix. The odbc driver is old but it works fine when used by applications...
10
by: Thomas Mlynarczyk | last post by:
Hi, In many cases, one needs only a single instance of a class in a script. Since PHP5 offers class variables like MyClass::$myVar and even class constants, it seems to me that an instantiation...
2
by: tuan_vandyk | last post by:
Hi I desperately need help with my project. Theoretically everything should work bu it just isn't. Please email me for a copy of the project's source code. It was made in Turbo C++ 5. Please if...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
3
by: AlFire | last post by:
Hi, Q: from the subject, why objects of old style classes are instances of 'object'? True
4
by: moon24 | last post by:
Hi im working with linked list and i have to implement a function that deletes the duplicates of a number. for example if given 2 7 1 7 12 7 then the result should be 2 7 1 12 here is what I have:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.