Results 1 to 17 of 17

Thread: Class Question

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    Class Question

    If I have a class called MyClass structured as follows:

    VB Code:
    1. Public Class MyClass
    2.    Public Sub Property1 As String
    3.    Public Sub Property2 As String
    4.    Public Sub Property3 As String
    5. End Class

    and i have 2 ways to fill those properties...

    Way 1
    Property1 = "Jim"
    Property2 = "Jimmy"
    Property3 = "Jimmers"

    Way2
    Property1 = "Tim"
    Property2 = "Timmy"
    Property3 = "Timothy"

    How can I take an instance of my class (let's call it MyBigClass)... and instantiate it so that it's properties fall in line with either way 1 or way 2??? So that they are "automatically" filled???

    Thanks,

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    "and i have 2 ways to fill those properties...

    Way 1
    Property1 = "Jim"
    Property2 = "Jimmy"
    Property3 = "Jimmers"

    Way2
    Property1 = "Tim"
    Property2 = "Timmy"
    Property3 = "Timothy" "

    I know it's nearly midnight here, but I can't see the difference
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Sorry, I'll try to be more clear...

    What I mean is...

    I want the class to be able to set the properties for the MyBigClass object...

    The values themselves....

    I don't know if this is close to what I would need to do, but...

    If I were to say something like...

    Dim MyBigClass as New MyClass.Jim

    It would set the Properties to match Way 1

    Understand now?

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    You've convinced me I'm going to bed now and see what responses you've had by the morning

    Oh. Just one thing. Looking at your interests do you consider there is a difference between "God" and "Jesus" or "God" and "The Holy Ghost"?

    Goodnight!
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    LOL...

    Ok... I hope you get some good rest...

    And no, there is no difference... unless you count the order in which they came to be known to us

    The idea is not to change the property type or anything, but the data in the properties to begin with...

    "Tim" and "Jim" are very different... (well, in the first character anywayz)

    I hope you understand now...

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  6. #6
    Lively Member
    Join Date
    Feb 2003
    Posts
    117
    Use a constructor
    VB Code:
    1. Class MyClass
    2.       private aName1 as String
    3.       private aName2 as String
    4.       private aName3 as String
    5.       Public Sub New(byVal name1 as String, byVal name2 as String, byVal name3 as String)
    6.     aName1 = name1
    7.     aName2 = name2
    8.     aName3 = name3
    9. End Sub
    10.  
    11. End Class
    12.  
    13. dim someClass as MyClass
    14. someClass = new MyClass("John", "Tim", "suxor")

    Obviously if you didn't want to pass the variables to the constructor you'd just assign them as follows...
    VB Code:
    1. Class MyClass
    2.       private aName1 as String
    3.       private aName2 as String
    4.       private aName3 as String
    5.       Public Sub New()
    6.     aName1 = "John"
    7.     aName2 = "Tim"
    8.     aName3 = "Hacker"
    9. End Sub
    10.  
    11. End Class

    Hope this helps.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Yeah, that helps some...

    I guess I could use some string constants (LoadJim, LoadTim for expample)

    and then do something like a select statement to set the values accordingly...

    is there a better way?

    It could be that my LoadJim, LoadTim and so forth options will be generated from the items in a collection... any ideas to utilize this better?

    Thanks,

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    And just a tidbit more info...

    The items in the collection will be of the same type of the item that I am wanting to initialize... or the properties will be very closely related...

    squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  9. #9
    Lively Member
    Join Date
    Feb 2003
    Posts
    117
    VB Code:
    1. Class MyClass
    2.       private aName1 as String
    3.       private aName2 as String
    4.       private aName3 as String
    5.       Public Sub New(byVal name1 as String, byVal name2 as String, byVal name3 as String)
    6.     aName1 = name1
    7.     aName2 = name2
    8.     aName3 = name3
    9. End Sub
    10.  
    11. End Class
    12.  
    13. dim someClass as MyClass
    14. select case way
    15.       case 1
    16.             someClass = new MyClass("John", "Tim", "suxor")
    17.       case 2
    18.             someClass = new MyClass("a", "b", "c"
    19. end select

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Is there NO other way to do this???

    There are like 50 different properties... and i won't be able to hard code them in like you are describing...

    I guess I will have to use a variable for each of the properties and .... shoot...

    What's the use?

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  11. #11
    Lively Member
    Join Date
    Feb 2003
    Posts
    117
    Well, how about posting what you do actually have to do? Maybe you aren't structuring your data in appropriate manner for the task? Maybe we could come up with another solution?

  12. #12
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    If your values are coming from a collection, declare an array of objects of MYCLASS and run a loop through the collection, populating the properties accordingly for each element of your MYCLASS array.

    Tell me if I'm not understanding something here.

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi squirrelly1,


    "And no, there is no difference... unless you count the order in which they came to be known to us "

    Amen.

    With regard to your problem, it looks like some of us are not clear on what you are trying to do. It is obviously not just trying to fill properties with values. If you are trying to create properties with different names in different instances of the class, could you not set up the properties as a collection and loop through them using a reference? (This is a bit beyond me really - just a thought)
    Last edited by taxes; May 28th, 2004 at 07:12 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  14. #14
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Why not use set & get property procedures? If you had a set procedure in your class called MyName, you could then use the dot notation to set the value. This wouldn't work for the constructor unless you passed the values in, but if you had an instance of your class named clsTryThis, you could do clsTryThis.Somename = "Jim". If you also had a get procedure, you could do strUsername = clsTryThis.SomeName. Plus, you'd have intellisense.

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Ok. These classes I'm using are huge so I will try to reconstruct what I want to do on a smaller scale...

    Let's say I have a class as follows:

    VB Code:
    1. Public Class ModelObject
    2.    Public ModelName As String
    3.    Public String1 As String
    4.    Public String2 As String
    5. End Class

    And I have a collection of these ModelObject classes

    VB Code:
    1. Public Class ModelObjectCol
    2.    'Collection stuff (add, remove, item, search, so forth)
    3. End Class

    And I have another collection of Production classes...

    What I would like to be able to do is to create a Production Object from the data held in a specific model object (found in the model object collection)

    Am I going about this the wrong way? Is there a better way to structure this?

    Thanks,

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    It's almost like an inventory/production system, but not quite...

    squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  17. #17
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    I'm not at work where VB is, and am on my 2nd glass of wine.
    But here's a conceptual stab...

    Dim myProd as new Production
    myProd.Something = myModelCol.Model("name_or_index").ModelName
    myProdCol.Add(myProd)

    Look at the dataset for examples. A dataset is a collection of tables, which in turn have properties and objects (some of which are also collections), etc.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width