473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Add a Class property dynamically at run-time



is there a way to add a class property an run-time?
If so, how?
any code samples?

*** Sent via Developersdex http://www.developersdex.com ***
Nov 18 '05 #1
11 6873
No, I don't think so. And, if you could, you would be adding code, and again
that is a bad idea.

You certainly can use a collection of some type, and add a to that
collection, and that collection would return a value

dim colTest as new Collection
colTest.Add 1,"one"
colTest.Add 10,"ten"

msgbox coltest("ten")

The above would return 10

And, you can make a collection "public" in a class object. So you could at
runtime certainly add new values to a collection, and retrieve them

clsMyClassObject.MyCollection("apple")
clsMyClassObject.MyCollection("banana")

So, could sort of consider apple, and banana new properties of our class
object, but really, you are just using a collection here....
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com
http://www.members.shaw.ca/AlbertKallal
Nov 18 '05 #2
On Fri, 18 Nov 2005 03:10:12 GMT, Larry <do********@nospamhere.com> wrote:


is there a way to add a class property an run-time?
If so, how?
any code samples?

*** Sent via Developersdex http://www.developersdex.com ***


For all intents and purposes, no you can't - however, for most reasons I can
think of that you'd want to do that, I can think of other coding patterns to
accomplish the same thing. What are you trying to accomplish?

Also, if you really do need to generate code, but the code that does the
generating doesn't have to be in the project that's being generated, that can
certainly be done using automation. I've had reason to do things like that
and have done it successfully.
Nov 18 '05 #3
You may be able to do it but it flies in the face of COM programming.

COM programming is all about interfaces and the contract an interface
creates between the COM server and the COM client. If you can arbitrarily
add methods to a class module then you are changing the contract, that is
unacceptable in COM terms.

Why don't you explain what your problem is rather than suggesting the
solution and asking how to do it.
--
Terry Kreft

"Larry" <do********@nospamhere.com> wrote in message
news:oC***************@news.uswest.net...


is there a way to add a class property an run-time?
If so, how?
any code samples?

*** Sent via Developersdex http://www.developersdex.com ***

Nov 18 '05 #4

so if I want to make a collection of objects this class
Public Class MyBook
public mystring as string
public mynumber as integer

end class

i want to a collection of mybook.

and I want to be able to dynamically add to the collection. I also want
to retrieve from the collection based on "mystring". so how do you do
that?



*** Sent via Developersdex http://www.developersdex.com ***
Nov 18 '05 #5
can you serialize that collection?
if so, is there any sample code?

*** Sent via Developersdex http://www.developersdex.com ***
Nov 18 '05 #6
Larry <do********@nospamhere.com> wrote in
news:FG***************@news.uswest.net:
so if I want to make a collection of objects this class
Public Class MyBook
public mystring as string
public mynumber as integer

end class

i want to a collection of mybook.

and I want to be able to dynamically add to the collection. I
also want to retrieve from the collection based on "mystring". so
how do you do that?


That's not how it's done in Access class modules.

You create a class module, then a public variable of type
collection. That makes it a public member of your class module.

VBA collections have a key and a value. You can't define anything
else. If you need more than two attributes, you need to use
something other than a collection, but for faking the ability to add
properties at runtime, it might suffice (depending on your
properties), since most properties are a name/value pair.

If that's not sufficient, then you have to use an array, which can
*not* be a public member of a class module. But you can wrap it in
public functions (or a public properties) to allow you to manipulate
its contents from outside the class.

Read up on custom collections and class modules and you should be
set.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 18 '05 #7
I think you're looking to create a collection class something like the
following

' *************************************
' Class module called MyBooks
' *************************************
Option Compare Database
Option Explicit

Private m_MyBooks As Collection
' ************************************
'

Private Sub Class_Initialize()
Set m_MyBooks = New Collection
End Sub

Private Sub Class_Terminate()
Do Until m_MyBooks.Count = 0
Call m_MyBooks.Remove(1)
Loop

Set m_MyBooks = Nothing
End Sub

Function Add(Name As String, Number As Integer) As MyBook
Dim mb As MyBook

Set mb = New MyBook
With mb
.mystring = Name
.mynumber = Number
End With

m_MyBooks.Add mb, Name
Set Add = m_MyBooks(Name)
Set mb = Nothing
End Function

Function Count() As Integer
Count = m_MyBooks.Count
End Function

Function Item(Index) As MyBook
Set Item = m_MyBooks(Index)
End Function

Sub Remove(Item)
m_MyBooks.Remove Item
End Sub

' *************************************
' Class module called MyBook
' *************************************
Option Compare Database
Option Explicit

Public mystring As String
Public mynumber As Integer

' *************************************
' Test code in standard module
' *************************************
Function mBooks()
Dim mbs As MyBooks
Dim mb1 As MyBook
Dim mb2 As MyBook
Dim mb3 As MyBook

Set mbs = New MyBooks

Set mb1 = mbs.Add("a", 1)
Set mb2 = mbs.Add("b", 2)
Set mb3 = mbs.Add("c", 3)

Debug.Print "From the Add method"
Debug.Print "--------------------"
Debug.Print mb1.mystring, mb1.mynumber
Debug.Print mb2.mystring, mb2.mynumber
Debug.Print mb3.mystring, mb3.mynumber
Debug.Print

Set mb1 = Nothing
Set mb2 = Nothing
Set mb3 = Nothing

Set mb1 = mbs.Item(1)
Set mb2 = mbs.Item(2)
Set mb3 = mbs.Item(3)

Debug.Print "From the Item Index"
Debug.Print "--------------------"
Debug.Print mb1.mystring, mb1.mynumber
Debug.Print mb2.mystring, mb2.mynumber
Debug.Print mb3.mystring, mb3.mynumber
Debug.Print

Set mb1 = Nothing
Set mb2 = Nothing
Set mb3 = Nothing

Set mb1 = mbs.Item("a")
Set mb2 = mbs.Item("b")
Set mb3 = mbs.Item("c")

Debug.Print "From the Item Key"
Debug.Print "--------------------"
Debug.Print mb1.mystring, mb1.mynumber
Debug.Print mb2.mystring, mb2.mynumber
Debug.Print mb3.mystring, mb3.mynumber

Set mb1 = Nothing
Set mb2 = Nothing
Set mb3 = Nothing

Set mbs = Nothing
End Function


--
Terry Kreft

"Larry" <do********@nospamhere.com> wrote in message
news:FG***************@news.uswest.net...

so if I want to make a collection of objects this class
Public Class MyBook
public mystring as string
public mynumber as integer

end class

i want to a collection of mybook.

and I want to be able to dynamically add to the collection. I also want
to retrieve from the collection based on "mystring". so how do you do
that?



*** Sent via Developersdex http://www.developersdex.com ***

Nov 18 '05 #8
rkc
Terry Kreft wrote:
I think you're looking to create a collection class something like the
following Private Sub Class_Terminate()
Do Until m_MyBooks.Count = 0
Call m_MyBooks.Remove(1)
Loop

Set m_MyBooks = Nothing
End Sub


Is 'unloading' a collection like this something you recommend doing
when the items are objects? I don't think I have ever seen it done before.
Nov 19 '05 #9
Yes, I've experienced applications which wouldn't close because this was
ommitted (not every time of course that would make life too easy <g>).

--
Terry Kreft

"rkc" <rk*@rochester.yabba.dabba.do.rr.bomb> wrote in message
news:y_*****************@twister.nyroc.rr.com...
Terry Kreft wrote:
I think you're looking to create a collection class something like the
following

Private Sub Class_Terminate()
Do Until m_MyBooks.Count = 0
Call m_MyBooks.Remove(1)
Loop

Set m_MyBooks = Nothing
End Sub


Is 'unloading' a collection like this something you recommend doing
when the items are objects? I don't think I have ever seen it done before.

Nov 19 '05 #10
"Larry" <do********@nospamhere.com> wrote in message
news:4W**************@news.uswest.net...
can you serialize that collection?
if so, is there any sample code?


No, you have to roll your own. On the other hand, remember we got a
relational database called JET at our disposal. This kind of reduces the
need for serialization of data that you need to persist.

I mean, if you got a bunch of "default" values for a form (say, default
city, default country, default area code etc), then one would want to build
something that lets us set, and more important "store" these default values
for our application. Further, we want to be able to add "new" default over
time, and not break old code. Right off the bat, a developer will often
think the solution here is to serialize these values. Serialization makes
more sense when you do NOT have such a nice database engine at your
disposal. So, in place of serializing those default values to some string
structure that gets saved, why not just create a table of default values?
And, you can use a number, or string as the "item" Token, and then define
fields for values.

I suppose if you don't need to save this serialized data, and just plan to
pass it around to routines, then I guess using the database engine for this
would not make sense.

It would not take long to build yourself a routine that stores a token +
"list of values" as a delimited string into a table....

If you don't need to save the data, then perhaps using a custom collection
would be a solution, and you really thus don't serialize the data when you
do this (there is no need to "deflate" the data...since you are NOT passing
this data to other processes, or other applications). Since we are staying
within ms-access, then you can just as easily pass the collection object
around in place of a serialized (deflated) string.
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com
http://www.members.shaw.ca/AlbertKallal

Nov 21 '05 #11
"Larry" <do********@nospamhere.com> wrote in message
news:FG***************@news.uswest.net...

so if I want to make a collection of objects this class
Public Class MyBook
public mystring as string
public mynumber as integer

end class

i want to a collection of mybook.

and I want to be able to dynamically add to the collection. I also want
to retrieve from the collection based on "mystring". so how do you do
that?


You can read my other post, but the answer is you *can* build a custom
collection, and put data into this memory object (and, you can retrieve by a
token key). This is what we all a "collection" in vb, and it does support
retrieval of values by a key. So, this means with a bit of code you can do
as you ask.

However, I would ONLY use the collection object for non table data. It seems
in your above example, "mybook" would be a book, and that belongs in the
database (hey, we got a full relational database here, and that is ZILLION
times better then a serialized collection object).

A collection object works like:

Dim colBooks As New Collection
Dim Book As New clsBook
Dim strWhatKey As String

Book.m_Author = "Tom Clancy"
Book.m_Title = "Hunt for Red October"
Book.m_Genere = "Adventure"

colBooks.Add Book, "RedOctober"

strWhatKey = "RedOctober"

Debug.Print colBooks(strWhatKey).m_Title
Our class object book would be defined by in the code window going
insert->class module, and then

Option Compare Database
Option Explicit

Public m_Author As String
Public m_Title As String
Public m_Genere As String

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com
http://www.members.shaw.ca/AlbertKallal
Nov 21 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jaakko Holster | last post by:
Lets run following code: ------- snip ------ class Perm { static $read = bindec('001'); static $write = bindec('010'); static $delete = bindec('100'); }
0
by: jerseycat10 | last post by:
Greetings. We are in the midst of updating our automated test suite. One capability we would like to add is to have n test methods per test class. We faintly remembered there being some kind...
5
by: Laszlo Zsolt Nagy | last post by:
Hughes, Chad O wrote: > Is there any way to create a class method? I can create a class > variable like this: > Hmm, seeing this post, I have decided to implement a 'classproperty'...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
2
by: Steve | last post by:
I'm in a weird situation where I'm using ComboBox's in a DataGrid. When the ComboBox selection changes, I'm currently storing the SelectedValue object into the DataSource of the DataGrid cell. ...
3
by: rambalep | last post by:
I need to get a class property value, where property name is contained in a variable. example: class test dim p1 as string dim p2 as string end class .... sub x
0
by: anileshlakhtakia | last post by:
Hi I have a class let say , Class A { public IList animal { get{}set{} } } Class B:A
2
by: Brad Pears | last post by:
I have a class in which I use a "Get" method to fill a dataset. I want this dataset to be a part of the class as I have other methods I want to act on that dataset - once the class has been...
4
by: Etienne-Louis Nicolet | last post by:
I am working on an error handler class. Within a property set method of a given class property I'd like to pass the PropertyInfo to a function. Imagine something like: Public Class MyClass .......
1
by: spyderfusion02 | last post by:
Hi there, I am trying to get the class names dynamically for the script below. I am using the Pagination plugin. I have different class names that are created through PHP so need to get them using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.