|
-
May 23rd, 2006, 05:45 AM
#1
Thread Starter
Fanatic Member
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.
-
May 23rd, 2006, 06:00 PM
#2
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:
Public Class Class1
Private _variables As ArrayList
Public ReadOnly Property Variables() As ArrayList
Get
'Don't create the collection until it is needed.
If Me._variables Is Nothing Then
Me._variables = New ArrayList
End If
Return Me._variables
End Get
End Property
End Class
Public Class Variable
Private _variableName As String
Private _variableType As Type
Private _value As Object
Public ReadOnly Property VariableName() As String
Get
Return Me._variableName
End Get
End Property
Public ReadOnly Property VariableType() As Type
Get
Return Me._variableType
End Get
End Property
Public Property Value() As Object
Get
Return Me._value
End Get
Set(ByVal value As Object)
Me._value = value
End Set
End Property
Public Sub New(ByVal name As String, ByVal type As Type)
Me.New(name, type, Nothing)
End Sub
Public Sub New(ByVal name As String, ByVal type As Type, ByVal value As Object)
Me._variableName = name
Me._variableType = type
Me._value = value
End Sub
End Class
-
May 23rd, 2006, 06:02 PM
#3
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;
}
}
}
-
May 24th, 2006, 03:42 AM
#4
Thread Starter
Fanatic Member
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.
-
May 24th, 2006, 04:48 AM
#5
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|