# vb 2005 help please...



## Akumos (Jun 14, 2009)

Hi

I want to use a long peice of text and include the person name in it. I have the name stored as a string called 'name'.

Is it possible to use a label to say something like...

"Hello 'name'"?

If not, what could I use to achieve this?

Thanks


----------



## Kreij (Jun 14, 2009)

I have not used VB in awhile, but in C# you do it like so ...
(This assumes you have a label called myLabel)

myLabel.Text = "Hello " + name;


----------



## Akumos (Jun 14, 2009)

I was thinking

myLabel.Text = "Hello" strName

So same but without the +, I'll try it, ta


----------



## Kreij (Jun 14, 2009)

The "+" concatonates the strings together.
You could also do something like ...

longString = "Hello " + name;
myLabel.Text = longString;

Some languages use the "&" for string conactonation.


----------



## Akumos (Jun 14, 2009)

Worked! Thanks for your help.

Now, how do I use the name variable in a different form? Can I store the name globally?


----------



## Kreij (Jun 14, 2009)

Global variables are kind of a bad practice.
What I would do is modify the form to accept the string.

Form myForm = new Form(string name)

or in the form ...

string _name;

public string Name { set { _name = value; } }

Again, this is C# syntax. Sorry, like I said, haven;t used VB in quite some time.


----------



## Akumos (Jun 14, 2009)

It still helps.

Can't seem to get it working though. As I create the 2nd form object should it let me pass the variable?

Dim Form2 As New Form2(strName)

I get 'Name 'strName' not decalred' But it def is.


----------



## Kreij (Jun 14, 2009)

In the code for the form you will have to declare a local variable

string _name;

Then in the constructor of the form you will have to pass the varialbe in.

Form myForm = new Form(string name)
{
_name = name;
}

You will have to call the form using Form myForm = new Form(name); from the 1st form.


----------



## Kreij (Jun 14, 2009)

Ahhh wait, that was a crappy example ... Give me a second. LOL

Create two forms, Form1 and Form2
In the Form2 give it a local variable and a constructor argument ...

```
string _name;

public Form2 (string name)
{
    _name = name;
}
```

To call the form from within Form1 ...

Form2 myForm2 = new Form2(name);

You can then in Form2 do label.Text = _name;


----------



## Akumos (Jun 14, 2009)

lol ok thanks


----------



## Deleted member 3 (Jun 14, 2009)

Form2.myLabel.Text

I hate working with multiple forms though, I prefer using tabs. Though this obviously doesn't always work.


----------



## Akumos (Jun 14, 2009)

Thanks for the input Dan but I dont totally understand...

the variable is stored as a string called strName in Form1

I want to display is in a lable in Form2...

Can you code it so it fits this example please. Sorry to be a pain!!


----------



## Deleted member 3 (Jun 14, 2009)

Akumos said:


> Thanks for the input Dan but I dont totally understand...
> 
> the variable is stored as a string called strName in Form1
> 
> ...



Ah, you want to read the variable. Just declare it on top of form 1, after the imports.


----------



## Akumos (Jun 14, 2009)

Thanks Kreij

Not too sure where to put this bit in VB

public Form2 (string name)

I have:

Public Class Form2 'Throws an error if I put it here

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

               lblOne.Text = "Hello " + strName 'From Form 1

    End Sub
End Class


----------



## Akumos (Jun 14, 2009)

DanTheBanjoman said:


> Ah, you want to read the variable. Just declare it on top of form 1, after the imports.



How lol?


----------



## Kreij (Jun 14, 2009)

Okay, I am not familiar with VB syntax, but in C# you can declare properties in any class.
When you instantiate the class, you can set the property before displaying the class (in this case a form).

Look up properties in VB.

Dan is more familiar than I am with VB. Maybe he can shed some light on this.


----------



## Kreij (Jun 14, 2009)

Maybe consider learning C#? It's much nicer. 
I could be biased as I never liked any form of Basic. LOL


----------



## Akumos (Jun 14, 2009)

I'm only learning VB because I have a core module at university in it next year. 

Thanks for all your time and help Kreij, much appreciated!


----------



## Deleted member 3 (Jun 14, 2009)

Dim bla as string


----------



## Akumos (Jun 14, 2009)

that just decalres a new variable, I want to use the same on as in form1


----------



## Deleted member 3 (Jun 14, 2009)

Like I said, declare it on top of the form, after the imports.


----------



## Akumos (Jun 14, 2009)

OK, thanks for both your help guys!!


----------



## FordGT90Concept (Jun 15, 2009)

Concatenation in VB is done with ampersand...


```
myLabel.Text = "Hello " & name
```





DanTheBanjoman said:


> Form2.myLabel.Text
> 
> I hate working with multiple forms though, I prefer using tabs. Though this obviously doesn't always work.


"Form2" would have to be have the Shared keyword (making it static) and so would "myLabel."

The proper way to do this is to make a property.

```
Public Property NameField() As String
  Get
    Return myLabel.Text
  End Get
  Set(ByVal value As String)
    myLabel.Text = value
  End Set
End Property
```

You use that code inside of your "Form2."  When you call Form2, it would look something like this:

```
Private Sub CallForm2()

 ' Create an instance of Form2
  Dim form As New Form2()

  ' Well set the text here just as an example...
  form.NameField = "foobar"

  ' Show the form and wait until the form is closed to proceed
  form.ShowDialog()

  ' The name that has been read back...
  MsgBox("Hello " & form.NameField)

End Sub
```

What the label is called internally now has no bearing on your main form that calls it.


----------



## Akumos (Jun 15, 2009)

Thnks, that helps alot.


----------



## Deleted member 3 (Jun 15, 2009)

FordGT90Concept said:


> "Form2" would have to be have the Shared keyword (making it static) and so would "myLabel."



Que? You can just access controls on another form that way. No additional code needed. Form2 would of course be the name for the form. And obviously myLabel would be the name of a label on Form2.


----------



## FordGT90Concept (Jun 15, 2009)

You can't directly access the definition of a class unless it has an instance or is static. If you are accessing Form2 via "Form2," Form2 would have to have the Shared keyword which means the compiler initializes at runtime.  If you don't have the Shared keyword, Form2 would be inaccessible outside of the constructors and properties inherited from the type Object.


So...

You'd make "myLabel" shared so you can access it via "Form2.myLabel" but because myLabel requires an instance of "Form" to exist, you'll have to also share Form2 to make certain the underlying Form is instantiated.


In any case, the property is the proper way to go (create an instance then address it, only reveal the stuff you want to modify).


----------



## Deleted member 3 (Jun 15, 2009)

FordGT90Concept said:


> You can't directly access the definition of a class unless it has an instance or is static. If you are accessing Form2 via "Form2," Form2 would have to have the Shared keyword which means the compiler initializes at runtime.  If you don't have the Shared keyword, Form2 would be inaccessible outside of the constructors and properties inherited from the type Object.
> 
> 
> So...
> ...



http://dan.h0sted.org/Doopdeedoo.rar

Run it, see it work. Look at the two lines of code I wrote.


----------



## FordGT90Concept (Jun 15, 2009)

Form1.Designer.vb

```
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
```

Note the "Friend."  VB makes all form components "Friend" by default which makes it accessible through the instance in the same project.


As for Form1, that must mean that the class Form1 is implicitly static (I didn't know that).  In C#, Form1 is a type.  In VB, Form1 is both a type and an instance.

When you do Form1.TextBox1.Text, Form1 is pointing to an instance of Form1 that carries the same name as the form.  Using the "Friend" keyword on TextBox1, you could gain access to its Text property.


----------



## Deleted member 3 (Jun 15, 2009)

VB is made for morons like me. Everything just works magically like that. It's not even case sensitive. And it can do basically anything the other languages can, that's why I use it


----------



## FordGT90Concept (Jun 15, 2009)

I like case sensitive.  I always capitalize classes and lowercase instances.  For example:


```
Car car = new Car();
```

Not that it makes a huge difference...it just makes  code easier to follow.


----------

