"Building" a variable inside of a loop
"Building" a variable inside of a loop
"Building" a variable inside of a loop
"Building" a variable inside of a loop
"Building" a variable inside of a loop
"Building" a variable inside of a loop "Building" a variable inside of a loop "Building" a variable inside of a loop "Building" a variable inside of a loop "Building" a variable inside of a loop "Building" a variable inside of a loop "Building" a variable inside of a loop "Building" a variable inside of a loop
"Building" a variable inside of a loop "Building" a variable inside of a loop
"Building" a variable inside of a loop
Go Back  Xtreme Visual Basic Talk > > > "Building" a variable inside of a loop


Reply
 
Thread Tools Display Modes
  #1  
Old 02-16-2006, 05:17 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default "Building" a variable inside of a loop


Wow, that's a crummy title.

So here's what I'm trying to do: I have four labels on a form, named from "Player1" to "Player4". I want to read their Text properties into an array of a custom data type so I can pass them back and forth to another form where the user can customize them.

So what I'm TRYING to do is something like:
Code:
For X = 1 To 4 Settings(X - 1).NameVal = Me.("Player" & X).Text Next
...but that, of course, spits back "Identifier expected".

Is there an elegant way to do this? I'd rather do this than write four separate assignment statements.
Reply With Quote
  #2  
Old 02-16-2006, 05:27 PM
mrjeffy321 mrjeffy321 is offline
Ultimate Contributor
 
Join Date: Apr 2003
Location: Texas, USA
Posts: 1,623
Default

To do it the way your trying now, you'll h ave to place the labels into a control array. Right now they are all seperate labels, you cannot just concatenate a string to access the label.

However, since you only have four, you dont really save much by using a loop anyway. Why not just hard code it in,
Code:
Settings(0).NameVal = Player1.Text Settings(1).NameVal = Player2.Text 'and so on
Reply With Quote
  #3  
Old 02-17-2006, 03:48 PM
herilane's Avatar
herilane"Building" a variable inside of a loop herilane is offline
Unashamed geek

Retired Moderator
* Expert *
 
Join Date: Jul 2003
Location: London, England
Posts: 8,988
Default

You can refer to controls using Me.Controls:
Code:
For X = 1 To 4 Settings(X - 1).NameVal = Me.Controls("Player" & X).Text Next
Reply With Quote
  #4  
Old 02-19-2006, 03:38 AM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by herilane
You can refer to controls using Me.Controls:
Code:
For X = 1 To 4 Settings(X - 1).NameVal = Me.Controls("Player" & X).Text Next
Thank you! Exactly what I needed.

I'm still a complete n00b to this, but I'm interested in knowing...is there a way to explain why this works without drowning me in jargon that will careen over my head? Is this what the Controls function (is that what that is? Seems to act like one) is basically for, or is it just a beneficial side effect?
Reply With Quote
  #5  
Old 02-19-2006, 07:02 AM
Iceplug's Avatar
Iceplug"Building" a variable inside of a loop Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

When you create a control, you specify a name for it. (In the editor, the name of the control is simply the variable name.)

In the Controls collection, you can access a control from its .Name. The Controls collection will find the control with the corresponding name and give it to you,

Also, under Option Strict, you will want to call X.ToString() when concatenating strings .
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #6  
Old 02-19-2006, 02:00 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by Iceplug
In the Controls collection, you can access a control from its .Name. The Controls collection will find the control with the corresponding name and give it to you,
So it sounds like I'm using it for exactly the intent with which it was intended. Excellent. Thanks.
Quote:
Also, under Option Strict, you will want to call X.ToString() when concatenating strings .
Instead of assuming that VB will handle the conversion from the Integer to the String? Yeah, that makes sense. I was having some problems before when I was testing this, it was throwing an exception at me, and I thought that might have been why, but then I realized I was putting my test loop in Public Sub New() that I was putting the loop above the InitializeComponent() call, which meant it was trying to find a control that didn't exist yet. Oops.

(Do you still NEED Public Sub New() in VB2005, or can I accomplish the same things with Form1_Load? I'm just using it to set up initializations that I can't do through the form editor, after the "'Add any initialization after the InitializeComponent() call" comment line.)
Reply With Quote
  #7  
Old 02-19-2006, 06:03 PM
Iceplug's Avatar
Iceplug"Building" a variable inside of a loop Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Yes, you need a Sub New in VB 2005. They've (not quite) conveniently hidden it from you in the rest of the form's Partial Class tucked away in the designer, which you have to show all files to find, making constructors less intuitive and... erm, anyway, yes you still need them .

But, as far as putting things in Sub New, you typically do not need to.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #8  
Old 02-19-2006, 07:15 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by Iceplug
But, as far as putting things in Sub New, you typically do not need to.
Okay. Let me throw a problem at you, then:

I have a Settings form, called SettingsDialog, that I call from my main one. I pass this Settings form an array of a custom data type (defined with a Structure statement) that the form uses to populate the controls.

So my Public Sub New for this form starts like this:
Code:
Public Sub New(ByVal Settings() As MainForm.SettingsStructure)
If I try to do this using SettingsDialog_Load, as in:
Code:
Private Sub SettingsDialog_Load(ByVal sender As Object, _ ByVal e As System.EventArgs, _ ByVal Settings() As MainForm.SettingsStructure) Handles Me.Load
...I get this error: "Method 'Private Sub SettingsDialog_Load(sender As Object, e As System.EventArgs, Settings() As Inquizitor2005.MainForm.SettingsStructure)' cannot handle Event 'Public Event Load(sender As Object, e As System.EventArgs)' because they do not have the same signature."

Am I correct in assuming that Public Event Load (whatever that is) doesn't like me adding in that passed variable, and in this case Public Sub New is the way to go?

Last edited by clemon79; 02-19-2006 at 07:28 PM.
Reply With Quote
  #9  
Old 02-20-2006, 07:52 AM
Iceplug's Avatar
Iceplug"Building" a variable inside of a loop Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Oh, if you are trying to pass a parameter into a form, then yes, you should use the Sub New to do so.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #10  
Old 02-20-2006, 01:17 PM
clemon79 clemon79 is offline
Freshman
 
Join Date: Jan 2006
Location: Seattle, WA
Posts: 37
Default

Quote:
Originally Posted by Iceplug
Oh, if you are trying to pass a parameter into a form, then yes, you should use the Sub New to do so.
Excellent. I'm coming from Pascal where I was taught in high school (a VERY long time ago) to pass everything you can, so this is the kind of thing I want to know so I make sure I don't learn bad coding habits early. (I'm sure I'm doing all kinds of other unpleasant things in the name of "well, it works!", but every little bit helps. )

I really appreciate your (and EVERYONE'S!) help!

Last edited by clemon79; 02-20-2006 at 01:25 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
"Building" a variable inside of a loop
"Building" a variable inside of a loop
"Building" a variable inside of a loop "Building" a variable inside of a loop
"Building" a variable inside of a loop
"Building" a variable inside of a loop
"Building" a variable inside of a loop "Building" a variable inside of a loop "Building" a variable inside of a loop "Building" a variable inside of a loop "Building" a variable inside of a loop "Building" a variable inside of a loop "Building" a variable inside of a loop
"Building" a variable inside of a loop
"Building" a variable inside of a loop
 
"Building" a variable inside of a loop
"Building" a variable inside of a loop
 
-->