Author Topic: Structure properties  (Read 3915 times)

0 Members and 1 Guest are viewing this topic.

Ricky_76

  • Guest
Structure properties
« on: January 18, 2008, 04:46:17 AM »
Hi,

I have a simple structure:

public structure contact
    dim Name as string
    dim Prop1 as string
    dim Prop2 as string
    dim Prop3 as string
end structure

1) I want to know if I can enumerate the structure properties
    ex:
    Dim c as new contact
    c.getprop()

2) I need to change programmatically the properties "Prop1", "Prop2", "Prop3"
    can I use a routine like this:

    Dim c as new contact
    For i = 1 To 3
        MsgBox(c.Prop & i)
    Next



tnx a lot!
/r

SomeCallMeDave

  • Guest
Re: Structure properties
« Reply #1 on: January 18, 2008, 07:51:03 AM »
This probably isn't the best way, but it does seem to work.

I used a class that provided a method using IEnumerable.

Code: [Select]

namespace ConsoleApplication1
{
    class Program
    {
        public class Contact
        {
            private string mName;
            private string mProp1;
            private string mProp2;
            private string mProp3;
            private string mProp4;

            //constructor for Testing only
            public Contact()
            {
                mName = "Ricky";
                mProp1 = "Prop1";
                mProp2 = "Prop2";
                mProp3 = "Prop3";
                mProp4 = "Prop4";

            }
            public string Name
            {
                get { return mName; }
                set { mName = value; }
            }

            public string Prop1
            {
                get { return mProp1; }
                set { mProp1 = value; }
            }

            public string Prop2
            {
                get { return mProp2; }
                set { mProp2 = value; }
            }

            public string Prop3
            {
                get { return mProp3; }
                set { mProp3 = value; }
            }

            public string Prop4
            {
                get { return mProp4; }
                set { mProp4 = value; }
            }
           
            // The enumerable method
            public IEnumerable<string> Props()
            {
                yield return mProp1;
                yield return mProp2;
                yield return mProp3;
                yield return mProp4;

            }
        }// class Contact

        static void Main(string[] args)
        {
            Contact myContact = new Contact();
            foreach (string prop in myContact.Props())
            {
                Console.WriteLine(prop);
            }
            Console.ReadLine();
        }
    }
}





EDIT:  I just noticed the code you provided is VB.  I don't know if VB has this capability.  Sorry
« Last Edit: January 18, 2008, 07:54:54 AM by SomeCallMeDave »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8787
  • AKA Daniel
Re: Structure properties
« Reply #2 on: January 18, 2008, 08:28:51 AM »
Very nice SCMD!

SomeCallMeDave

  • Guest
Re: Structure properties
« Reply #3 on: January 18, 2008, 08:34:10 AM »
Thanks Daniel.

Is that the way you would handle it?

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8787
  • AKA Daniel
Re: Structure properties
« Reply #4 on: January 18, 2008, 09:20:40 AM »
Thanks Daniel.

Is that the way you would handle it?

I was thinking using a method like ToArray() but your solution is more elegant  :kewl:

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8787
  • AKA Daniel
Re: Structure properties
« Reply #5 on: January 18, 2008, 01:44:22 PM »
Just not as pretty as C# but here is my take
Also SMCD,  IMO you were correct in using a Class instead of a Struct (its too big to be a struct)

Code: [Select]
Public Class Contacts
    Inherits List(Of Contact)
    ' Methods
    Public Sub New()
    End Sub

    Public Sub New(ByVal contacts As IEnumerable(Of Contact))
        Dim con As Contact
        For Each con In contacts
            MyBase.Add(con)
        Next
    End Sub

End Class


Public Class Contact
    Implements IComparable
    ' Methods
    Public Sub New()
        Me.m_Name = String.Empty
        Me.m_Prop1 = String.Empty
        Me.m_Prop2 = String.Empty
        Me.m_Prop3 = String.Empty
        Me.m_Prop4 = String.Empty
    End Sub

    Public Sub New(ByVal stringArray As String())
        Me.FromArray(stringArray)
    End Sub

    Public Sub New(ByVal name As String, ByVal prop1 As String, ByVal prop2 As String, ByVal prop3 As String, ByVal prop4 As String)
        Me.m_Name = name
        Me.m_Prop1 = prop1
        Me.m_Prop2 = prop2
        Me.m_Prop3 = prop3
        Me.m_Prop4 = prop4
    End Sub

    Public Function CompareTo(ByVal obj As Object) As Integer
        If Not TypeOf obj Is Contact Then
            Throw New ArgumentException("Object is not a Contact.")
        End If
        Dim c2 As Contact = DirectCast(obj, Contact)
        Return Me.m_Name.CompareTo(c2.m_Name)
    End Function

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        If (obj Is Nothing) Then
            Return False
        End If
        If Object.ReferenceEquals(Me, obj) Then
            Return True
        End If
        If (Not MyBase.GetType Is obj.GetType) Then
            Return False
        End If
        Dim objContact As Contact = DirectCast(obj, Contact)
        Return Me.m_Name.ToUpper.Equals(objContact.m_Name)
    End Function

    Public Sub FromArray(ByVal StringArray As String())
        Dim i As Integer
        For i = 0 To 5 - 1
            If (StringArray(i) Is Nothing) Then
                StringArray(i) = String.Empty
            End If
        Next i
        Me.m_Name = StringArray(0)
        Me.m_Prop1 = StringArray(1)
        Me.m_Prop2 = StringArray(2)
        Me.m_Prop3 = StringArray(3)
        Me.m_Prop4 = StringArray(4)
    End Sub

    Public Overrides Function GetHashCode() As Integer
        Return Me.m_Name.GetHashCode
    End Function

    Public Function ToArray() As String()
        Return New String() { Me.m_Name, Me.m_Prop1, Me.m_Prop2, Me.m_Prop3, Me.m_Prop4 }
    End Function

    Public Overrides Function ToString() As String
        Dim sb As New StringBuilder
        sb.Append(Me.m_Name)
        sb.Append(",")
        sb.Append(Me.m_Prop1)
        sb.Append(",")
        sb.Append(Me.m_Prop2)
        sb.Append(",")
        sb.Append(Me.m_Prop3)
        sb.Append(",")
        sb.Append(Me.m_Prop4)
        Return sb.ToString
    End Function


    ' Properties
    Public Property Name As String
        Get
            Return Me.m_Name
        End Get
        Set(ByVal value As String)
            Me.m_Name = value
        End Set
    End Property

    Public Property Prop1 As String
        Get
            Return Me.m_Prop1
        End Get
        Set(ByVal value As String)
            Me.m_Prop1 = value
        End Set
    End Property

    Public Property Prop2 As String
        Get
            Return Me.m_Prop2
        End Get
        Set(ByVal value As String)
            Me.m_Prop2 = value
        End Set
    End Property

    Public Property Prop3 As String
        Get
            Return Me.m_Prop3
        End Get
        Set(ByVal value As String)
            Me.m_Prop3 = value
        End Set
    End Property

    Public Property Prop4 As String
        Get
            Return Me.m_Prop4
        End Get
        Set(ByVal value As String)
            Me.m_Prop4 = value
        End Set
    End Property


    ' Fields
    Protected m_Name As String
    Protected m_Prop1 As String
    Protected m_Prop2 As String
    Protected m_Prop3 As String
    Protected m_Prop4 As String
End Class



SomeCallMeDave

  • Guest
Re: Structure properties
« Reply #6 on: January 18, 2008, 01:51:01 PM »
Ouch.  :)

I'm glad I decided to try to learn C#.net 'stead of VB.net.

Fatty

  • Guest
Re: Structure properties
« Reply #7 on: January 18, 2008, 01:54:02 PM »
Easier yet, I think:

Code: [Select]
        Dim c As New contact
        For Each obj As [Object] In c.GetType.GetFields
            MsgBox(obj.name)
        Next
The same with values

~'J'~

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8787
  • AKA Daniel
Re: Structure properties
« Reply #8 on: January 18, 2008, 01:56:41 PM »
Wow! Does that work if they are private, or protected?  :-o

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8787
  • AKA Daniel
Re: Structure properties
« Reply #9 on: January 18, 2008, 02:01:47 PM »
Hmm this is also worth noting

Quote
The GetFields method does not return fields in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which fields are returned, because that order varies.

Fatty

  • Guest
Re: Structure properties
« Reply #10 on: January 18, 2008, 02:11:44 PM »
Strange, it had returned all the fields in particular order
as I declared before in my own experience
Okay, forget about :)

Regards,

~'J'~

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8787
  • AKA Daniel
Re: Structure properties
« Reply #11 on: January 18, 2008, 02:19:15 PM »
I’ll have to do a few tests myself ( you can’t believe everything MS says )  ;-)
Very creative thinking!!! 8-)

Ricky_76

  • Guest
Re: Structure properties
« Reply #12 on: January 21, 2008, 08:43:22 AM »
Easier yet, I think:

Code: [Select]
        Dim c As New contact
        For Each obj As [Object] In c.GetType.GetFields
            MsgBox(obj.name)
        Next
The same with values

~'J'~

NICE, tnx to all  :roll:
now I can retrieve the all the properties, but how to change it!

Can some one explain me then meaning of "[Object]" and why the method GetType doesn't appear in the c.methods

tnx again
/r