/ Forside / Teknologi / Udvikling / VB/Basic / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
VB/Basic
#NavnPoint
berpox 2425
pete 1435
CADmageren 1251
gibson 1230
Phylock 887
gandalf 836
AntonV 790
strarup 750
Benjamin... 700
10  tom.kise 610
Brug af variabler mellem forms
Fra : K.L.


Dato : 22-11-00 10:23

Hej

Er det muligt af bruge en variabel mellem to forms. Man kan jo ikke
lave en variabel public eller kan man ? Den følgende linie vil give en
fejl:

Public Dim count as Integer

MVH Kim

 
 
N/A (22-11-2000)
Kommentar
Fra : N/A


Dato : 22-11-00 12:41



Helge Bjørkhaug (22-11-2000)
Kommentar
Fra : Helge Bjørkhaug


Dato : 22-11-00 12:41

"J@N" <Nowhere@InSpace.Com> skrev i melding news:8vg3t6$6ph$1@news.inet.tele.dk...
> Lav dig et modul og definer variablen som følger:
>
> Global Count as integer
>
> Så kan den ses overalt.
>
> Men du kunne jo også fra Form1 sætte værdien direkte ind i det berørte felt
> på Form2. Dvs før du åbner den kan du: "Form2.txtFelt.Text=27" og derefter
> åbne Form2. På den måde slipper du for globale variable, som er af det onde
> (taler af egen erfaring)
>

Ett alternativ er å bruke OO.
Er er ett eksempel fra Element K;

********** THE TIP **********

An OO way to pass Visual Basic variable values to a form

Object oriented programming guidelines stipulate that objects --
including forms -- should maintain their own internal values. With
this in mind, you'll need a way to pass values to loaded forms without
using universally global variables. Fortunately, Visual Basic provides
just such a way with its property Let() and Get() procedures.

For example, suppose you have a button Click() event on Form1 that
loads Form2. Form2 displays a value collected in Form1. In the past,
you may have created a public variable with an application scope, and
then read this variable in Form2 to obtain the proper value. Using
this technique, however, soon results in hard-to-manage, spaghetti code.

As a more elegant alternative, you can add a property to Form2 that
Form1 can then set.

For instance, in Form2 you would add the following property procedures
along with the variable declaration:

Private myVar as Variant

Public Property Get PassVar() As Variant
PassVar = myVar
End Property

Public Property Let PassVar(ByVal vNewValue As Variant)
If IsNumeric(vNewValue) Then myVar = vNewValue
End Property

For these procedures, you can change the property type, but it must
match in both the Get and Let procedures. Also notice that we included
some validation code to ensure that the passed value is a number.

Now, to read or assign a value to this property, use the following
code on Form1

Private Sub Command1_Click()
With Form2
.PassVar = 25
.Show
Debug.Print .PassVar
End With
End Sub


Mvh,

Helge



Niels Krogh (22-11-2000)
Kommentar
Fra : Niels Krogh


Dato : 22-11-00 14:00

Du kan også lave en Property på din variabel :

på en Form2 har du :

Private nCount as Integer

Public Property Get MyCount() as Integer
MyCount = nCount
End Property

Du kan så fra Form1 kalde Debug.Print Form2.MyCount

Vil du ændre på variablen tilføjer du på Form2

Public Property Let MyCount(byval nData as Integer)
nCount = nData
End Property

....og fra fra Form1 skrive Form2.MyCount = 2

mvh
Niels krogh

"K.L." <vespertilio@hotmail.com> skrev i en meddelelse
news:ok3n1t85ov6nr3rvudjfe943tqbi92r73d@4ax.com...
> Hej
>
> Er det muligt af bruge en variabel mellem to forms. Man kan jo ikke
> lave en variabel public eller kan man ? Den følgende linie vil give en
> fejl:
>
> Public Dim count as Integer
>
> MVH Kim





Søg
Reklame
Statistik
Spørgsmål : 177513
Tips : 31968
Nyheder : 719565
Indlæg : 6408602
Brugere : 218887

Månedens bedste
Årets bedste
Sidste års bedste