Problem with Objects

Hi object-orientated gurus

I can't get my head around a simple OO problem, and no wonder, because I'm moving from Access to VB.Net. So excuse my lack of understanding in that area...

I am trying to develop a VB.net app which shall make calls to a web service from Bloomberg to retrieve data about securities. The service requires 3 different objects for the call:
- a header object, which is a simple one with a very small number of attributes
- a Fields list object, which is also simply a list of strings (the name of the fields for which data will be retrieved from the web service)
- an "Instruments" object, which is a "bundle" of 1-n single "Instrument" objects (securities, actually, shares and bonds), each single "Instrument" object having just 3 properties (an ID, a type and an optional market sector)

All these objects are defined through the web service definitions. I seem to have succeeded in defining the first 2 objects, but I stumble badly on the 3rd one, although I have an example of the whole definition, but it's in C#, and there are obviously sufficient differences for me between the 2 languages to fail in setting up the third object.
My main stumbling block is that I have a couple of thousand of "Instrument" objects (securities) to go through and to "add" to the "Instruments" object. I fail already at the first "add".

Here is the C# code:
            //Setting Instrument information
            Instrument bbUniqueId1 = new Instrument();
            bbUniqueId1.id = "EQ0086119600001000";
            bbUniqueId1.type = InstrumentType.BB_UNIQUE;
            bbUniqueId1.typeSpecified = true;

            Instrument ticker = new Instrument();
            ticker.id = "IBM";
            ticker.yellowkey = MarketSector.Equity;
            ticker.yellowkeySpecified = true;
            ticker.type = InstrumentType.TICKER;
            ticker.typeSpecified = true;

            Instrument bbUniqueId2 = new Instrument();
            bbUniqueId2.id = "GB0003252318";
            bbUniqueId2.type = InstrumentType.ISIN;
            bbUniqueId2.typeSpecified = true;

            // group all instrument into a single instance of type Instruments.
            Instruments instrs = new Instruments();
            instrs.instrument = new Instrument[] { bbUniqueId1, ticker, bbUniqueId2 };

Open in new window


My case is different insofar as I am going through a DB table, selecting one security after the other and setting up the "Instrument" object (called "OneSec" in my code) for each of them. Here is my VB.Net code:
                 For i = 0 To dsCT.Tables("SMD").Rows.Count - 1

                   ... here some code to ignore or not securities, which data is in the SMD data table of the dsCT dataset ...

                   ' Add selected security to the instrument list
                    ' Set Instrument attributes
                    Dim OneSec As New BBgDLWebService.Instrument
                    OneSec.id = dsCT.Tables("SMD").Rows(i).Item("BBgGlobalID")
                    OneSec.type = BBgDLWebService.InstrumentType.BB_GLOBAL
                    OneSec.typeSpecified = True
                    OneSec.yellowkeySpecified = False
                    ' "Add" single instrument to the "AllSecs" "Instruments" object
                    AllSecs.instrument = ????
                 Next

Open in new window


The last line should be the somewhat equivalent of the last line in C#, except that I'm thinking of "adding" each single "Instrument" within the loop to the "Instruments" object, whereas the C# example dodges that issue by adding 3 separately defined "Instrument" objects in one go in the "Instruments" object. But I just can't find a valid syntax for that last line. And I also don't know if I should "put" my Instrument objects in the Instruments object only at the very end, or for each index of the For loop.

Can you tell me how to "add" my  "OneSec" objects to the "AllSecs" object ?

Thanks for your help.
Bernard
LVL 1
bthouinIT Analyst and developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
You last line should do something like:
AllSecs.instrument(i) = OneSec

Open in new window


-saige-
bthouinIT Analyst and developerAuthor Commented:
OK, let'see if that code runs now...

Nope. Error message:
"Error: Object reference not set to an instance of an object."
CodeCruiserCommented:
Try following


                 Dim InstrumentsList As New List(Of BBgDLWebService.Instrument)
                 For i = 0 To dsCT.Tables("SMD").Rows.Count - 1

                   ... here some code to ignore or not securities, which data is in the SMD data table of the dsCT dataset ...

                   ' Add selected security to the instrument list
                    ' Set Instrument attributes
                    Dim OneSec As New BBgDLWebService.Instrument
                    OneSec.id = dsCT.Tables("SMD").Rows(i).Item("BBgGlobalID")
                    OneSec.type = BBgDLWebService.InstrumentType.BB_GLOBAL
                    OneSec.typeSpecified = True
                    OneSec.yellowkeySpecified = False
                    ' "Add" single instrument to the "AllSecs" "Instruments" object
                    InstrumentsList.Add(OneSec)
                 Next
                 AllSecs.instrument = New BBgDLWebService.Instrument() { InstrumentsList.ToArray() }

Open in new window

it_saigeDeveloperCommented:
The reason for the object reference error is because "AllSecs.instrument" is not initialized.  What CodeCruiser proposes will work.  You could also initialize the array:
For i = 0 To dsCT.Tables("SMD").Rows.Count - 1
	... here some code to ignore or not securities, which data is in the SMD data table of the dsCT dataset ...

	' Add selected security to the instrument list
	' Set Instrument attributes
	Dim OneSec As New BBgDLWebService.Instrument
	OneSec.id = dsCT.Tables("SMD").Rows(i).Item("BBgGlobalID")
	OneSec.type = BBgDLWebService.InstrumentType.BB_GLOBAL
	OneSec.typeSpecified = True
	OneSec.yellowkeySpecified = False
	' "Add" single instrument to the "AllSecs" "Instruments" object
	If AllSecs.instrument Is Nothing Then
		AllSecs.instrument = New BBgDLWebService.Instrument(dsCT.Tables("SMD").Rows.Count - 1) {}
	End If
	AllSecs.instrument(i) = OneSec
Next

Open in new window


-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bthouinIT Analyst and developerAuthor Commented:
@it_saige: Brilliant ! It works like a breeze !

I learnt 2 things: 1) initialize your objects; 2) syntax of the initialization with the curly brackets at the end

Thanks a lot. You get the points.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.