For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic Syntax > November 2005 > Referencing an object from a string within an array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Referencing an object from a string within an array
SrMousse

2005-11-25, 7:58 am

I would like to pre-apologize for the length of this post. I have tried to be
as clear and concise as possible. Here is a quick breakdown of what I am
looking to do… the rest is more of an explanation to be sure I am headed in
the right direction.

I need to know how to reference and manipulate an object (specifically a
string) by referring to it with a string.

More specifically, I would like to create an array by which I can access a
“reference” of strings.


I am currently working on a system to solve metrics style problems. In
short, I am using a “algorithm” to discover the values of items within a grid
based on the values of items surrounding them. I have devised a method to
accomplish this goal, however it is based on manual string manipulation and I
am estimating that it will take over 11,000 lines of code to complete the
project based on my current strategy. Here is a rundown of what I am
currently doing.

I have a 9x9 grid that I have created strings to represent the data. Each
string is named accordingly sA1, sA2, sA3, sA4, sA5… sB1, sB2, sB3… sI6, sI7,
sI8, sI9… the capital letter representing the row and the number representing
the column.

I then assign each string a value with the possible data (i.e. sA1 =
“123456789”)

Finally I check the appropriate surrounding items within the matrix to
eliminate characters within the string to determine the appropriate value
that sA1 will result in.

I use a series of loops along with If…Then statements to “parse” each string
looking for the correct criteria to determine the value of each item within
the grid.

Here is a snippet of the code I am referring to:

‘Check if sA1 has already been determined by the value of the string
‘having been eliminated down to a single character
‘If the value has already been determined, skip to the next object in the grid
If (sA1.Lenght <> 1) then
‘If sA2 is known then continue
If (sA2.Lenght = 1) then
‘If the character that sA2 represents exists within sA1 then continue
If (sA1.IndexOf(sA2.Chrs(0)) <> -1) Then
‘Remove the character contained within sA2 from sA1
sA1 = sA1.Remove(sA1.IndexOf(sA2.Chrs(0)), 1)
End If
End If
If (sA3.Lenght = 1) then
If (sA1.IndexOf(sA3.Chrs(0)) <> -1) Then
sA1 = sA1.Remove(sA1.IndexOf(sA3.Chrs(0)), 1)
End If
End If
If (sA4.Lenght = 1) then
If (sA1.IndexOf(sA4.Chrs(0)) <> -1) Then
sA1 = sA1.Remove(sA1.IndexOf(sA4.Chrs(0)), 1)
End If
End If
‘This process continues by checking itself against 19 unique items within
the grid
End If

‘Then we move onto the next item within the grid, A2… and the process
continues through all 81 items within the grid.

This method IS working great, however as you can see, it is very code
intensive… There MUST be a better way.

My problem is two fold; first, each of the 81 objects within the grid checks
itself against 19 UNIQUE objects within that grid. So I have to somehow
define which 19 objects each will check itself against. Second, in order to
loop through the objects I will somehow need to be able to address each item
within the grid dynamically… is that possible, and how is it done?

Here is my though on the strategy.
I create an array for each object within the grid. Within that array I
could reference the 19 objects that it needs to reference itself against.
Then I can then create a loop to parse each of the arrays and run the
appropriate commands. Here is the sample code of how I would think it could
work:

Dim aA1() as String = (“sA2”, “sA3”, “sA4”, “sA5”, “sB1”, “sB2”, “sC1”, etc)
Dim I as Integer = 0

Do Until (I = aA1.GetLength(0) – 1)
If (aA1(i).Lenght = 1) then
If (sA1.IndexOf(aA1(I).Chrs(0)) <> -1) Then
sA1 = sA1.Remove(sA1.IndexOf(aA1(I).Chrs(0)), 1)
End If
End If

I = I + 1
Loop

This would effective reduce 100 lines of code down to 8, which would also
increase accuracy greatly.

Thanks for all of your help! I look forward to hearing your great suggestions.

Rick Rothstein [MVP - Visual Basic]

2005-11-25, 7:02 pm

Below is my standard response to people using VB.NET, or its newer
variants, who post questions on this newsgroup. HOWEVER, let me first
suggest that you consider abandoning your attempt to address each cell
in your grid of data by a unique name. Instead, I would suggest you
use a two-dimensional array to address the individual cells. That way,
you could use the first element to reference the columns and the
second element to reference the rows (or vice versa, whichever way if
more convenient for you to think of it in). Then you can use nested
For-Next loops to address every cell in the grid of data. I think, if
I understood your request correctly, that would reduce the amount of
code you would eventually need. Anyway, it is just a suggestion. Now,
for my standard response....

Almost everybody in this newsgroup is using VB6 or lower. While you
may get a stray answer to VB.NET (including VB2003 and VB2005 which
has dropped .NET from its name) questions here, you should ask them in
newsgroups devoted exclusively to .NET programming (the languages are
different enough to warrant separate newsgroup support). Look for
newsgroups with either the word "dotnet" or "vsnet" in their name.

For the microsoft news server, try these newsgroups for Visual Basic
..NET related questions...

microsoft.public.dotnet.languages.vb
microsoft.public.dotnet.languages.vb.upgrade
microsoft.public.dotnet.languages.vb.controls
microsoft.public.dotnet.languages.vb.data

And these for more general .NET questions

microsoft.public.dotnet.general
microsoft.public.vsnet.general

Note: There are many other .NET newgroups (use the first three
"fields" from the last two as templates when searching for them), but
the above ones should get you started.

Rick




"SrMousse" <SrMousse@discussions.microsoft.com> wrote in message
news:3A7C5CE1-08A0-490F-A3CF-EFF08B2F501D@microsoft.com...
> I would like to pre-apologize for the length of this post. I have

tried to be
> as clear and concise as possible. Here is a quick breakdown of what

I am
> looking to do. the rest is more of an explanation to be sure I am

headed in
> the right direction.
>
> I need to know how to reference and manipulate an object

(specifically a
> string) by referring to it with a string.
>
> More specifically, I would like to create an array by which I can

access a
> "reference" of strings.
>
>
> I am currently working on a system to solve metrics style problems.

In
> short, I am using a "algorithm" to discover the values of items

within a grid
> based on the values of items surrounding them. I have devised a

method to
> accomplish this goal, however it is based on manual string

manipulation and I
> am estimating that it will take over 11,000 lines of code to

complete the
> project based on my current strategy. Here is a rundown of what I

am
> currently doing.
>
> I have a 9x9 grid that I have created strings to represent the data.

Each
> string is named accordingly sA1, sA2, sA3, sA4, sA5. sB1, sB2, sB3.

sI6, sI7,
> sI8, sI9. the capital letter representing the row and the number

representing
> the column.
>
> I then assign each string a value with the possible data (i.e. sA1 =
> "123456789")
>
> Finally I check the appropriate surrounding items within the matrix

to
> eliminate characters within the string to determine the appropriate

value
> that sA1 will result in.
>
> I use a series of loops along with If.Then statements to "parse"

each string
> looking for the correct criteria to determine the value of each item

within
> the grid.
>
> Here is a snippet of the code I am referring to:
>
> 'Check if sA1 has already been determined by the value of the string
> 'having been eliminated down to a single character
> 'If the value has already been determined, skip to the next object

in the grid
> If (sA1.Lenght <> 1) then
> 'If sA2 is known then continue
> If (sA2.Lenght = 1) then
> 'If the character that sA2 represents exists within sA1 then

continue
> If (sA1.IndexOf(sA2.Chrs(0)) <> -1) Then
> 'Remove the character contained within sA2 from sA1
> sA1 = sA1.Remove(sA1.IndexOf(sA2.Chrs(0)), 1)
> End If
> End If
> If (sA3.Lenght = 1) then
> If (sA1.IndexOf(sA3.Chrs(0)) <> -1) Then
> sA1 = sA1.Remove(sA1.IndexOf(sA3.Chrs(0)), 1)
> End If
> End If
> If (sA4.Lenght = 1) then
> If (sA1.IndexOf(sA4.Chrs(0)) <> -1) Then
> sA1 = sA1.Remove(sA1.IndexOf(sA4.Chrs(0)), 1)
> End If
> End If
> 'This process continues by checking itself against 19 unique items

within
> the grid
> End If
>
> 'Then we move onto the next item within the grid, A2. and the

process
> continues through all 81 items within the grid.
>
> This method IS working great, however as you can see, it is very

code
> intensive. There MUST be a better way.
>
> My problem is two fold; first, each of the 81 objects within the

grid checks
> itself against 19 UNIQUE objects within that grid. So I have to

somehow
> define which 19 objects each will check itself against. Second, in

order to
> loop through the objects I will somehow need to be able to address

each item
> within the grid dynamically. is that possible, and how is it done?
>
> Here is my though on the strategy.
> I create an array for each object within the grid. Within that

array I
> could reference the 19 objects that it needs to reference itself

against.
> Then I can then create a loop to parse each of the arrays and run

the
> appropriate commands. Here is the sample code of how I would think

it could
> work:
>
> Dim aA1() as String = ("sA2", "sA3", "sA4", "sA5", "sB1", "sB2",

"sC1", etc)
> Dim I as Integer = 0
>
> Do Until (I = aA1.GetLength(0) - 1)
> If (aA1(i).Lenght = 1) then
> If (sA1.IndexOf(aA1(I).Chrs(0)) <> -1) Then
> sA1 = sA1.Remove(sA1.IndexOf(aA1(I).Chrs(0)), 1)
> End If
> End If
>
> I = I + 1
> Loop
>
> This would effective reduce 100 lines of code down to 8, which would

also
> increase accuracy greatly.
>
> Thanks for all of your help! I look forward to hearing your great

suggestions.
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com