Welcome to Web-News
A Web-based News Reader
Subject Re: Hello .NET, D Here Calling
From bearophile <bearophileHUGS@lycos.com>
Date Tue, 23 Dec 2008 06:38:46 -0500
Newsgroups digitalmars.D

Chad J Wrote:
> This is exactly where I'm coming from.  I used to use C# properties a
> lot.  They are super effective.

In C# you can use for example:

class TimePeriod {
    private double seconds;

    public double Hours {
        get { return seconds / 3600; }
        set { seconds = value * 3600; }
    }
}


Or just:

public double TotalPurchases { get; set; }


Some people have proposed:

public int property Myval {
    get;
    
    set {
        if (value > 10)
            throw new Exception();
        else
            Myval = value;
    }
}


Time ago I have written this for D1, I don't know if it can be useful:

import std.metastrings: Format;

template AttributeGetSet(Type, string name) {
    const AttributeGetSet = Format!("
        private %s %s__;
        public %s %s() { return this.%s__; }
        public void %s(int %s__local) { this.%s__ = %s__local; }
    ", Type.stringof, name, Type.stringof, name, name, name, name, name, name);
}


C# also has indexers:

>Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.<

http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx

Usage example:

class SampleCollection<T> {
    private T[] arr = new T[100];

    public T this[int i] {
        get {
            return arr[i];
        }

        set {
            arr[i] = value;
        }
    }
}

But to me that looks a lot like the opIndex/opIndexAssign/opIndexLvalue of D.

Bye,
bearophile

Recent messages in this thread
 
-# Hello .NET, D Here Calling Walter Bright 22-Dec-2008 03:54 pm
.-# Re: Hello .NET, D Here Calling bearophile 22-Dec-2008 05:03 pm
.||# Re: Hello .NET, D Here Calling bearophile 22-Dec-2008 05:13 pm
.|-# Re: Hello .NET, D Here Calling Walter Bright 22-Dec-2008 05:20 pm
.||-# Re: Hello .NET, D Here Calling bearophile 22-Dec-2008 05:35 pm
.||||# Re: Hello .NET, D Here Calling Steven Schveighoffer 22-Dec-2008 05:49 pm
.|||\# Re: Hello .NET, D Here Calling Nick Sabalausky 22-Dec-2008 10:26 pm
.||\# Re: Hello .NET, D Here Calling Kagamin 23-Dec-2008 03:47 am
.|-# Re: Hello .NET, D Here Calling Steven Schveighoffer 22-Dec-2008 05:28 pm
.|.\# Re: Hello .NET, D Here Calling Kagamin 23-Dec-2008 03:56 am
.|# Re: Hello .NET, D Here Calling John Reimer 22-Dec-2008 05:30 pm
.-# Re: Hello .NET, D Here Calling Lutger 22-Dec-2008 05:54 pm
.|-# Re: Hello .NET, D Here Calling Chad J 23-Dec-2008 05:47 am
.|.-# Re: Hello .NET, D Here Calling (Current message) bearophile 23-Dec-2008 06:38 am
.|.|\# Re: Hello .NET, D Here Calling Mosfet 23-Dec-2008 08:01 am
.|.-# Re: Hello .NET, D Here Calling Daniel de Kok 23-Dec-2008 11:26 am
.|..-# Re: Hello .NET, D Here Calling Lutger 23-Dec-2008 12:39 pm
.|..|\# Re: Hello .NET, D Here Calling Andrei Alexandrescu 23-Dec-2008 01:23 pm
.|..-# Re: Hello .NET, D Here Calling Nick Sabalausky 23-Dec-2008 02:09 pm
.|..|-# Re: Hello .NET, D Here Calling Daniel de Kok 23-Dec-2008 03:19 pm
.|..|.|# Re: Hello .NET, D Here Calling bearophile 23-Dec-2008 03:38 pm
.|..|.-# Re: Hello .NET, D Here Calling Nick Sabalausky 23-Dec-2008 04:10 pm
.|..|.|-# Re: Hello .NET, D Here Calling Daniel de Kok 23-Dec-2008 05:03 pm
.|..|.|.-# Re: Hello .NET, D Here Calling Nick Sabalausky 23-Dec-2008 08:52 pm
.|..|.|.|\# Re: Hello .NET, D Here Calling Daniel de Kok 24-Dec-2008 03:57 am
.|..|.|.\# Re: Hello .NET, D Here Calling Chad J 23-Dec-2008 10:45 pm
.|..|.\# Re: Hello .NET, D Here Calling Christopher Wright 23-Dec-2008 07:10 pm
.|..\# Re: Hello .NET, D Here Calling Kagamin 24-Dec-2008 04:04 am
.\# Re: Hello .NET, D Here Calling Tim M 23-Dec-2008 12:36 am