Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays? Any suggestions?

Status
Not open for further replies.

RandomEvil

Programmer
Dec 11, 2004
4
US
I would like to construct an Array that will cut something like this code down. I had tried but wasn't able to get it to work so used IF THEN.


If Option1(O) = True Then
txtStartpnt.Text = 100
End If
If Option1(1) = True Then
txtStartpnt.Text = 123
End If
If Option1(2) = True Then
txtStartpnt.Text = 135
End If
If Option1(3) = True Then
txtStartpnt.Text = 150
End If

Does anyone have a good formula to programming ARRAYS?

Thanks for any help

R E
 
You could try a Select structure:

Code:
Select Case Index
  Case 1
  txtStartpnt.Text = 100
  Case 2
  txtStartpnt.Text = 135
' and so on ....
' ....
  Case Else
End Select

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
There are 2 ways...
Method 1 : IIf(test, true, false)
Code:
txtStartpnt.Text = iif(Option1(0), 100, _
                   iif(Option1(1), 123, _
                   iif(Option1(2), 135, _
                   iif(Option1(3), 150, 0))))

Method 2 : Tag Property
Set the Values of the opitions in the Tag properties in either the Designer or like this in Form_Load
Code:
  Option1(0).Tag = 100
  Option1(1).Tag = 123
  Option1(2).Tag = 135
  Option1(3).Tag = 150

Then in the Option1_Click event place this code:
Code:
  txtStartpnt.Text = Option1(Index).Tag

Short Enough?

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Sorry but I don't get it.
What does your problem have to do with programming ARRAYs?
So far I only see an IF/SELECT problem here.

WOuld you like to improve your "Option1" structure?
->If so: To what purpose? What would you like to achieve?

Cheers,
Andy


[blue]The last voice we will hear before the world explodes will be that of an expert saying:
"This is technically impossible!" - Sir Peter Ustinov[/blue]
 
you could just do

If Option1(0) = True txtStartpnt.Text = 100
If Option1(1) = True txtStartpnt.Text = 135
...
If Option1(N) = True txtStartpnt.Text = 58000

Or Set an array with the values

and do a sub

Private Sub SetText(index)
txtStartpnt.Text = MyArray(index)
end sub
 
PS

To call the sub do somthing like this

Dim i as int

For i = 0 to Option1.count-1
if Option1(i).value = true then SetText(i)
next i
 
I'll take a guess:
Code:
Dim vals, i as Integer
vals=Array("100","123","135","150") 'extend to whatever is needed.
For i = 0 to 3
  if option1(i) Then
    txtStartpnt.Text=vals(i)
    exit For
  End If
Next i

Like this?

[blue]The last voice we will hear before the world explodes will be that of an expert saying:
"This is technically impossible!" - Sir Peter Ustinov[/blue]
 
If your data (100, 123, 135, 150) followed some pattern (eg. 100, 110, 120, 130) then an array solution could be appropriate, but it may still be easier to use one of the solutions already provided or VB's Choose function as follows;

Dim Choice as integer
Dim MyChoice as Integer

'set MyChoice to 1, 2, 3 or 4

Choice = Choose(MyChoice, 100, 123, 135, 150)

regards Hugh
 
yep that is true for a small number of values but if he has 100 Etc then the above is alot better and easier to debug
 
Like I said way up there ^

Why would you want to go through the mess of using If Then or Select Case or even IIf() when you can just use the Tag Property... thats what it is there for...

When you click on a checkbox in a control array, it sends the Index to the Click event, and thats all you need...

Code:
Private Sub Option1_Click(Index As Integer)
  txtStartpnt = Option1(Index).Tag
End Sub

the only place if needs be used is here...
IF you click the option, the code executes ;-)

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
True but he wanted an example of arrays("well thats what i thought anyways")
 
Ok. I don't think that RandomEvil is talking about an option button, which has a tag property. I think he's talking about an array called Option1, which doesn't have one. I'd use 2 arrays, like this:

Dim Option1(100) as Whatever
Dim Values1(100) as Whatever

Values1(0) = 148
Values1(1) = 269
...
for i = 0 to 99
if Option1(i) then
txtStartPnt.Text = Values1(i)
end if
next

If you have a large number of values, this is easier to debug, since you don't have to wander through multiple if blocks. This also assumes that the values themselves have no pattern to them; if they did, you could populate the array with some kind of loop.

Bob
 
BobRodes said:
Ok. I don't think that RandomEvil is talking about an option button, which has a tag property.
Are you serious?

Take a Good Look At the Code...
Code:
If [b]Option1(O) = True[/b] Then
  txtStartpnt.Text = 100
End If
If Option1(1) = True Then
  txtStartpnt.Text = 123
End If
If Option1(2) = True Then
  txtStartpnt.Text = 135
End If
If Option1(3) = True Then
  txtStartpnt.Text = 150
End If

It has to be A Control Array of OptionButtons..

A) The naming Option1(x) is just a little TOO IRONIC

B) Why would you be testing for True?

C) If it WAS a regular array, you could just set the values to the array itself... Because you would have some kind of index calling it anyway, otherwise the array would be a waste of memory...
All you would have to do is this to simplify it...
Code:
Option1(O) = 100
Option1(1) = 123
Option1(2) = 135
Option1(3) = 150
...
txtStartpnt.Text = Option1(x)

So, YES it is an OptionButton and YES it does have a tag...

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Well, maybe I'm serious, and maybe seriously befuddled. Only randomevil knows for sure....of course you make perfect sense, but that can be a liability in the Microsoft world. See, randomevil could have gone out and made an array called option1, and set it to point to a bunch of option buttons not in a control array, all without telling us. As an instructor, that's the sort of world I live in, I'm afraid. So, perhaps I'm a victim of my own enculturation. Didn't mean to insult you, though, or randomevil for that matter.

:)

Bob
 
excuse me, i also have almost the same problem out here, but the diff. is not option button but textbox..i dont know whats wrong with this.. the code here is:

dim i as byte
for i = text1.lbound to text1.ubound
if text1(i) = "" then
msgbox "blah..blah..blah.."
next i
end sub
my problem is, if i have 20 textbox, i just would want to make my code short instaed of telling
if text1(0).text = "" or if text1(1).text = ""..and so on till Text1(20)
any help will be appreciated..thanks in advance
 
Dim i As Byte
For i = Text1.lbound To Text1.ubound
If Text1(i) = "" Then MsgBox "blah..blah..blah.."
Next i


your problem is you have no end if


This is fine in C but not vb

if text1(i) = "" then
msgbox "blah..blah..blah.."


For vb you need to do this

If Text1(i) = "" Then MsgBox "blah..blah..blah.."

or
If Text1(i) = "" Then
MsgBox "blah..blah..blah.."
end if
 
dim i as byte
for i = text1.lbound to text1.ubound
if text1(i) = "" then
msgbox "blah..blah..blah.."
next i
end sub

Unless you are using VB.Net, you do not have LBound and UBound properties for a TextBox... (and even then, it would be Text1.Text.UBound, etc...)
You do have Count, but that can still get messy...
This is my favorite method: (using For Each Next)

Code:
Dim T As TextBox
For Each T In Text1
  If T = "" Then
    msgbox "blah..blah..blah.."
  End If
Next

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top