Results 1 to 5 of 5

Thread: declaring class array property

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    declaring class array property

    I have an interface which is going to set up the definition of 3 dimensional array which will contain something like :

    Name of variable e.g "txtName", Type of variable e.g "String or Int", Content e.g "C:\\myfile.txt" or "1".

    I need to create a class with the appropriate definition but am a wee bit confused about interfaces.

    Any ideas gurus ?
    Last edited by venerable bede; May 23rd, 2006 at 11:11 AM.

    Parksie

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: declaring class array property

    I strongly suggest that you do not use a 3D array for that. You would be far better off defining a class or structure with three properties corresponding to those values, then give your container class a property that is a collection of those objects. You haven't specified which version you're using (c'mon, after 692 posts you should know better) so I don't know if Generic collections are available to you. Here's an example using an ArrayList but you should use a strongly typed collection, either derived from CollectionBase in .NET 1.x or a Generic.List in .NET 2.0.
    VB Code:
    1. Public Class Class1
    2.  
    3.     Private _variables As ArrayList
    4.  
    5.     Public ReadOnly Property Variables() As ArrayList
    6.         Get
    7.             'Don't create the collection until it is needed.
    8.             If Me._variables Is Nothing Then
    9.                 Me._variables = New ArrayList
    10.             End If
    11.  
    12.             Return Me._variables
    13.         End Get
    14.     End Property
    15.  
    16. End Class
    17.  
    18. Public Class Variable
    19.  
    20.     Private _variableName As String
    21.     Private _variableType As Type
    22.     Private _value As Object
    23.  
    24.     Public ReadOnly Property VariableName() As String
    25.         Get
    26.             Return Me._variableName
    27.         End Get
    28.     End Property
    29.  
    30.     Public ReadOnly Property VariableType() As Type
    31.         Get
    32.             Return Me._variableType
    33.         End Get
    34.     End Property
    35.  
    36.     Public Property Value() As Object
    37.         Get
    38.             Return Me._value
    39.         End Get
    40.         Set(ByVal value As Object)
    41.             Me._value = value
    42.         End Set
    43.     End Property
    44.  
    45.     Public Sub New(ByVal name As String, ByVal type As Type)
    46.         Me.New(name, type, Nothing)
    47.     End Sub
    48.  
    49.     Public Sub New(ByVal name As String, ByVal type As Type, ByVal value As Object)
    50.         Me._variableName = name
    51.         Me._variableType = type
    52.         Me._value = value
    53.     End Sub
    54.  
    55. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: declaring class array property

    Oops. Here's a C# translation:
    Code:
    public class Class1 {
        
        private ArrayList _variables;
        
        public ArrayList Variables {
            get {
                // Don't create the collection until it is needed.
                if ((this._variables == null)) {
                    this._variables = new ArrayList();
                }
                return this._variables;
            }
        }
    }
    public class Variable {
        
        private string _variableName;
        
        private Type _variableType;
        
        private object _value;
        
        public Variable(string name, Type type) : 
                this(name, type, null) {
        }
        
        public Variable(string name, Type type, object value) {
            this._variableName = name;
            this._variableType = type;
            this._value = value;
        }
        
        public string VariableName {
            get {
                return this._variableName;
            }
        }
        
        public Type VariableType {
            get {
                return this._variableType;
            }
        }
        
        public object Value {
            get {
                return this._value;
            }
            set {
                this._value = value;
            }
        }
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: declaring class array property

    Thanks for the detailed reply.

    You're right, I always forget to mention that I am using VS 2005 and 2.0 of the framework :-(.

    The arraylist isn't available so I will have to study up on Generics to get this to work. All new to me. I'm scared

    Last edited by venerable bede; May 24th, 2006 at 03:45 AM.

    Parksie

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: declaring class array property

    Of course the ArrayList is available. It hasn't been removed in .NET 2.0, but you'd be better to use a Generic.List anyway.
    Code:
    public class Class1
    {
        private List<Variable> _variables;
    
        public List<Variable> Variables
        {
            get
            {
                // Don't create the collection until it is needed.
                if ((this._variables == null))
                {
                    this._variables = new List<Variable>();
                }  return this._variables;
            }
        }
    }
    The List will behave pretty much exactly as an ArrayList does except that it will only accept items of type Variable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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