Solved

Creating a property

Posted on 2002-03-30
20
222 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA error replacing data 6 40
MS Acccess - Update Field in Table when report is printed 6 46
How to debug this code 7 64
Recommendation vb6 to vb.net or others 14 174
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

840 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