473,387 Members | 1,611 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,387 software developers and data experts.

Shared function - strange behaviour

I have extracted the code fragment below. The variables do not appear to be
set, which is not what I expected. I can see it must be a problem with a
Shared method and an instance object, but I cann't understand exactly what
is happening - any advice?

Thanks

Robin Hay

Module Module1

Public Class SomeClass

Inherits CollectionBase

Public Structure Thing

Private mFirstThing As String

Private mSecondThing As String

Public Property FirstThing() As String

Get

Return mFirstThing

End Get

Set(ByVal Value As String)

mFirstThing = Value

End Set

End Property

Public Property SecondThing() As String

Get

Return mSecondThing

End Get

Set(ByVal Value As String)

mSecondThing = Value

End Set

End Property

End Structure

Default Public ReadOnly Property Item(ByVal Index As Integer) As Thing

Get

Return CType(list.Item(Index), Thing)

End Get

End Property

Public Sub Add(ByVal theThing As Thing)

List.Add(theThing)

End Sub

Public Shared Function AddThings() As SomeClass

Dim theSomeClass As New SomeClass

Dim theThing As New Thing

theSomeClass.Add(theThing)

For Each theThing In theSomeClass

theThing.FirstThing = "One"

theThing.SecondThing = "Two"

Next

Return theSomeClass

End Function

End Class

Sub Main()

Dim theClass As SomeClass = SomeClass.AddThings

Console.WriteLine("First Thing: {0} Second Thing: {1}",
theClass(0).FirstThing, theClass(0).SecondThing)

Console.ReadLine()

End Sub

End Module
Nov 20 '05 #1
2 1010
Got it! The Structure is a Value type! Silly me.

Hayrob

"Hayrob" <ha****@btinternet.com> wrote in message
news:eB**************@TK2MSFTNGP11.phx.gbl...
I have extracted the code fragment below. The variables do not appear to be set, which is not what I expected. I can see it must be a problem with a
Shared method and an instance object, but I cann't understand exactly what
is happening - any advice?

Thanks

Robin Hay

Module Module1

Public Class SomeClass

Inherits CollectionBase

Public Structure Thing

Private mFirstThing As String

Private mSecondThing As String

Public Property FirstThing() As String

Get

Return mFirstThing

End Get

Set(ByVal Value As String)

mFirstThing = Value

End Set

End Property

Public Property SecondThing() As String

Get

Return mSecondThing

End Get

Set(ByVal Value As String)

mSecondThing = Value

End Set

End Property

End Structure

Default Public ReadOnly Property Item(ByVal Index As Integer) As Thing

Get

Return CType(list.Item(Index), Thing)

End Get

End Property

Public Sub Add(ByVal theThing As Thing)

List.Add(theThing)

End Sub

Public Shared Function AddThings() As SomeClass

Dim theSomeClass As New SomeClass

Dim theThing As New Thing

theSomeClass.Add(theThing)

For Each theThing In theSomeClass

theThing.FirstThing = "One"

theThing.SecondThing = "Two"

Next

Return theSomeClass

End Function

End Class

Sub Main()

Dim theClass As SomeClass = SomeClass.AddThings

Console.WriteLine("First Thing: {0} Second Thing: {1}",
theClass(0).FirstThing, theClass(0).SecondThing)

Console.ReadLine()

End Sub

End Module

Nov 20 '05 #2
"Hayrob" <ha****@btinternet.com> schrieb
Public Shared Function AddThings() As SomeClass
Dim theSomeClass As New SomeClass
Dim theThing As New Thing
theSomeClass.Add(theThing)
For Each theThing In theSomeClass
theThing.FirstThing = "One"
theThing.SecondThing = "Two"
Next
Return theSomeClass
End Function

"Thing" is a structure. A structure is a value type. Whenever you execute

For Each theThing In theSomeClass

the items in theSomeClass are copied to the variable theThing. As it is a
*copy* of the item in the list, changing the properties of the copy doesn't
change the properties of the item in the list.

The only solution is to make a class instead of a strucutre. A class is a
reference type. Consequently, "For Each" gets copies of the *references* of
the items in the list.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Hughes | last post by:
I shared a directory, dietplan6, which is actually several levels down in C:\Program files\ on a Windows 2000 machine, called 'Nom'. When I tried to check its existence from another Win2k machine,...
5
by: amit kumar | last post by:
I am calling a function which returns pointer to a map. The declaration of the map is map<int,vectxyz*>. vectxyz is a vector containing pointer to a class xyz. For map<int,vectxyz*>* p1 In the...
10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
2
by: tshad | last post by:
I am trying to set up some shared functions in my Global.asax file and can't seem to get it to work. In my Global.asax: Public Class myUtils inherits System.Web.HttpApplication public...
14
by: Joe Fallon | last post by:
I am trying to build a Data Access Layer for a SQL Server back end. My class has a number of methods in it. The constructor takes a connection string. I currently have to instantiate an object...
4
by: Chris | last post by:
Hello, I'm just getting started with VB and am new to the group, so please excuse what may seem to be a rudimentary question. I've been writing basic programs and have noticed that the...
0
by: Piotr Kosinski | last post by:
Hi, I noticed strange behaviour in Excel. I wrote small shared add in for Excel. This util create button in standard bar. Button click provides to open a form which has to import data from sql...
47
by: pkirk25 | last post by:
I've made a small program to demonstrate one problem I'm having fixing strings in C. I need to be able to remove HTML mark-ups from text lines. I create my variable, pass it to my function,...
23
by: g.ankush1 | last post by:
#include <stdio.h> /* 1st example int a() { return 1; }
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...

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.