Solved

Creating a property

Posted on 2002-03-30
20
219 Views
Last Modified: 2010-05-02
I'm usually developing in FoxPro where creating a property is interactive.  I'm creating an ActiveX control in VB and I need to provide a property for the user to set.  This property will be a certain path.  These Property Get & Let confuse me.  Can someone please give me the steps to accomplish this?  
0
Comment
Question by:newvber
  • 10
  • 9
20 Comments
 

Author Comment

by:newvber
ID: 6907533
This is the code I have so far:
private pcPath

Public Property Get cPhoto_Path() As String
    cPhoto_Path = pcPath
End Property

Public Property Let cPhoto_Path(ByVal vNewValue As String)
    pcPath = vNewValue
End Property

I get no errors, but when I enter a value in the property, it does not remain during run-time.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6907569
Hi newvber,

-better make that declare

private pcPath as String

-have tried to run it like ?

msgbox yourcontrol.cPhoto_Path
yourcontrol.cPhoto_Path = "test"
msgbox yourcontrol.cPhoto_Path

:O)Bruintje
0
 
LVL 1

Expert Comment

by:COAtheGREAT
ID: 6907643
Bouth previous answer are OK and you shuld use both. First you use when you nead to know when property is changed and do some actions as response.

In that case you use Property Let if the property is variable or Property Set if the property is object.

If you want property to be read only then you write only Property Get and dont wtite let or set, and if you want property to be read-write then you shuld write get and let or set.

If you dont nead to know when property changes and want property to be read-write then you declare Public variable, and it is seen outside as property.
0
 

Author Comment

by:newvber
ID: 6907705
COAtheGREAT, I tried your suggestion and I'm still having the same problem of setting the property in development and not seeing the value I set during run-time.  I don't know what step I am missing.
0
 
LVL 1

Expert Comment

by:COAtheGREAT
ID: 6907752
I think this is what you need:
Use PropertyBag to save value of the properties betwen design time ond run time

Public Property Let bgColor(vNewValue As OLE_COLOR)

    Text1.BackColor = vNewValue
    PropertyChanged "bgColor"

End Property

Public Property Get bgColor() As OLE_COLOR

    bgColor = Text1.BackColor

End Property

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)

    Text1.BackColor = PropBag.ReadProperty("bgColor")

End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)

    PropBag.WriteProperty "bgColor", Text1.BackColor

End Sub
0
 
LVL 1

Expert Comment

by:COAtheGREAT
ID: 6907790
Try this

private pcPath

Public Property Get cPhoto_Path() As String
   cPhoto_Path = pcPath
End Property

Public Property Let cPhoto_Path(ByVal vNewValue As String)
   pcPath = vNewValue
   PropertyChanged "cPhoto_Path"
End Property

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
   pcPath= PropBag.ReadProperty("cPhoto_Path")
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
   PropBag.WriteProperty "cPhoto_Path", pcPath
End Sub
0
 

Author Comment

by:newvber
ID: 6907791
Is it handled the same way when you're not assigning the property value to an object?  I want to use the value in a method.  Do I need to use an object in order to have the value available to me?
0
 

Author Comment

by:newvber
ID: 6907792
Sorry, COAtheGREAT, I didn't see your second post.  Give me a minute to try it out.
0
 

Author Comment

by:newvber
ID: 6907811
COAtheGreat, I'm working at being a super pain in the *****.  When I'm in another method I want to get the value from that property.  I've tried the following:

x = pcPath
x = cPhoto_path
x = user_control1.cphoto_path
x = oStarImage.cphoto_path   (This is the name of my user control)

Have you ever worked this hard for 50 points before?
0
 
LVL 1

Expert Comment

by:COAtheGREAT
ID: 6907820
if it is method within control then
x = pcPath
if it is from outside then
x = oStarImage.cphoto_path

I dont have much time so be quick, asking is no problem just time :)
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Expert Comment

by:COAtheGREAT
ID: 6907844
does it working ?
0
 

Author Comment

by:newvber
ID: 6907847
The x = pcPath doesn't work.  It's always empty.  
0
 
LVL 1

Expert Comment

by:COAtheGREAT
ID: 6907858
show me all code
0
 
LVL 1

Expert Comment

by:COAtheGREAT
ID: 6907861
show me all code
0
 

Author Comment

by:newvber
ID: 6907869
The save() is where I'm trying to see what the developer assigned to the property at design time.

Option Explicit
Private pcPath
Private plActive

Private Sub Command1_Click()
    Save "star.jpg"
End Sub

Public Sub Save(tcFileName As String)
Dim x As String

x = pcPath

End Sub


Private Sub oStarImage_ReadProperties(PropBag As PropertyBag)
    pcPath = PropBag.ReadProperty("cPhoto_Path")
    plActive = PropBag.ReadProperty("lActive")
End Sub

Private Sub oStarImage_WriteProperties(PropBag As PropertyBag)
    PropBag.WriteProperty "cPhoto_Path", pcPath
    PropBag.WriteProperty "lActive", plActive
End Sub

Public Property Get cPhoto_Path() As String
    cPhoto_Path = pcPath
End Property

Public Property Let cPhoto_Path(ByVal vNewValue As String)
    pcPath = vNewValue
    PropertyChanged "cPhoto_Path"
End Property

Private Property Get lActive() As Variant
    lActive = plActive
End Property

Private Property Let lActive(ByVal vNewValue As Boolean)
    plActive = vNewValue
    PropertyChanged "lActive"
End Property
0
 
LVL 1

Expert Comment

by:COAtheGREAT
ID: 6907875
Private Property Get lActive() As Variant
   lActive = plActive
End Property

Private Property Let lActive(ByVal vNewValue As Boolean)
   plActive = vNewValue
   PropertyChanged "lActive"
End Property

this is big error so control cant be placed on form property cant be variant and bool at the same time. Chose
0
 
LVL 1

Expert Comment

by:COAtheGREAT
ID: 6907881
second error
Private pcPath
it should be
Private pcPath as string
and same for plActive
0
 

Author Comment

by:newvber
ID: 6907883
Sorry about that.  I was in the middle of trying to add a different property when I checked EE.  I remembered you wanted things fast, so I just copied & pasted as soon as I could, before checking the new stuff.
0
 
LVL 1

Accepted Solution

by:
COAtheGREAT earned 50 total points
ID: 6907896
dont be sory it is normal, next it is not

Private Sub oStarImage_ReadProperties(PropBag As PropertyBag)
   pcPath = PropBag.ReadProperty("cPhoto_Path")
   plActive = PropBag.ReadProperty("lActive")
End Sub

Private Sub oStarImage_WriteProperties(PropBag As PropertyBag)
   PropBag.WriteProperty "cPhoto_Path", pcPath
   PropBag.WriteProperty "lActive", plActive
End Sub

it is

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)

   pcPath = PropBag.ReadProperty("cPhoto_Path", "")
   plActive = PropBag.ReadProperty("lActive", False)

End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)

   PropBag.WriteProperty "cPhoto_Path", pcPath, ""
   PropBag.WriteProperty "lActive", plActive, False

End Sub

and now work :)
0
 

Author Comment

by:newvber
ID: 6907910
Beyond Excellent!  Went way beyond call of duty for a meezely 50 points.  If I had more points, I'd certainly give them to COAtheGREAT!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now