PDA

View Full Version : Visual Basic Question


NYFAN
01-29-2007, 02:46 PM
Ok, I feel rather stupid asking this question, but I just got a copy of Visual Basic from my IT department because I need to write a really basic program, and I thought this was easier for what I'm going to do than C++ because I'm not as familiar with C++...

Anyway, all I'm trying to do is declare a global variable, and it's giving me all sorts of issues. I'm using the basic syntax of:

Global X as string

and it gives me all sorts of errors (one saying that because it is a namespace I can't use it as an expression) and then if I put it in the general declarations area it says wrong syntax.

I am able to declare a local variable easily, but this is proving to be quite challenging - and I don't know why...

I'm using Microsoft Visual Studio 2005.

Any advice is appreciated.

PackerFanatic
01-29-2007, 02:57 PM
I believe if you simply change the word "global" to "public", your problem will be solved :)

Fidatelo
01-29-2007, 03:03 PM
I don't think you can create Global variables in .Net. Even if you can, I'm sleptical as to why you would want to. If you want to create a module/member level variable (either in an old-school code module or inside of a class) you can use the syntax:

Private _x As String

If you need to access this variable from outside it's scope (which is to say, from outside the module/class), you can either change "Private" to "Public", or preferably write a Property accessor like this:

Public Propery x() As String
Get
Return _x
End Get
Set(ByVal value As String)
_x = value
End Set
End Property

NYFAN
01-29-2007, 03:05 PM
you are my hero. i hate when things like that change and nobody tells me!

NYFAN
01-29-2007, 03:06 PM
PackerFanatic got it for me, I was using the wrong lingo - last time I used VB was oh, 8 years ago haha....

PackerFanatic
01-29-2007, 03:08 PM
Yeah, VB has changed a bit since then :-D

Fidatelo
01-29-2007, 03:10 PM
I think I may have gone a little more in-depth than neccessary in my reponse. I would like to resubmit my answer:

"What PackerFanatic said"

:)

Mr. Wednesday
01-29-2007, 03:14 PM
Yeah, VB has changed a bit since then :-D
To the extent that some diehards (like myself) prefer to refer to the .NET version as Visual Fred — any similarity to VB is coincidental. :) (I exaggerate, somewhat.)

NYFAN
01-30-2007, 10:56 AM
I got such a great response yesterday I'm coming back :) They have apparently also changed how to create an array of textboxes. I realize you can do this through code now, but I don't know how to link it up to the actual textboxes on the form, without having to do each one separately (is there another way?). Thanks in advance!

Fidatelo
01-30-2007, 11:23 AM
NOTE: I'm writing this directly into the browser so the syntax may be slightly inaccurate or the spelling wrong, but it should be close:

Dim oTextBox As TextBox
Dim oTextBoxes As System.Collections.Generic.List(Of TextBox)

For Each i As Integer = 1 To iWhateverYouAreCountingGoesHere

oTextBox = New TextBox()
oTextBox.Name = "txtGenericTextBox" + i.ToString()
oTextBoxes.Add(oTextBox)

Next


... Later in code, when you want to display them ...

Dim x As Integer = 10 ' Or whatever starting x coordinate you want
Dim y as Integer = 10 ' Or whatever starting y coordinate you want
dim spacer as Integer = 10 ' Or however much room you want between controls

For Each oTextBox As TextBox In oTextBoxes

oTextBox.Location = New System.Drawing.Point(x, y)
myForm.Controls.Add(oTextBox)

y += oTextBox.Height + spacer

Next

NYFAN
01-30-2007, 01:17 PM
Thank you!