Click here to Skip to main content
15,887,683 members
Home / Discussions / C#
   

C#

 
GeneralRe: How do I open a flush from the resource folder in C # Pin
Member 1362111718-Jan-18 4:52
Member 1362111718-Jan-18 4:52 
QuestionHow to generate auto no for property Pin
Mou_kol16-Jan-18 22:08
Mou_kol16-Jan-18 22:08 
AnswerRe: How to generate auto no for property Pin
OriginalGriff16-Jan-18 23:10
mveOriginalGriff16-Jan-18 23:10 
AnswerRe: How to generate auto no for property Pin
Jochen Arndt16-Jan-18 23:24
professionalJochen Arndt16-Jan-18 23:24 
AnswerRe: How to generate auto no for property Pin
Gerry Schmitz17-Jan-18 9:30
mveGerry Schmitz17-Jan-18 9:30 
AnswerRe: How to generate auto no for property Pin
BillWoodruff20-Jan-18 15:03
professionalBillWoodruff20-Jan-18 15:03 
GeneralRe: How to generate auto no for property Pin
Mou_kol23-Jan-18 20:59
Mou_kol23-Jan-18 20:59 
GeneralRe: How to generate auto no for property Pin
BillWoodruff23-Jan-18 23:51
professionalBillWoodruff23-Jan-18 23:51 
The context is what matters here: depending on context I might:

1. if the value of 'Id is local to the class, then a static class Int32 variable incremented in the constructor is all you need.

1.a. if you need to keep a collection of instances of 'PersonalDetail:
public class PersonalDetail
{
    public PersonalDetail(string firstName, int age)
    {
        if (IdToInstance.Any(kvp => kvp.Value.FirstName.Equals(firstName)))
        {
            throw new DuplicateNameException();
        }

        FirstName = firstName;
        Age = age;

        IdToInstance.Add(Id++, this);
    }

    public string FirstName { get; set; }
    public int Age { get; set; }

    // static constructor
    static PersonalDetail()
    {
        Id = 1;
        IdToInstance = new Dictionary<int, PersonalDetail>();
    }

    static int Id { set; get; }

    static Dictionary<int, PersonalDetail> IdToInstance { get; }

    public static void Add(params (string name, int age)[] pds)
    {
        foreach (var pd in pds)
        {
            new PersonalDetail(pd.name, pd.age);
        }
    }

    public static PersonalDetail InstanceFromId(int id)
    {
        PersonalDetail pd = null;

        return IdToInstance.TryGetValue(id, out pd) ? pd : null;
    }
}

/* usage example

// multiple instances created
// using C# 7 tuple syntax
PersonalDetail.Add(
    (name: "Tapan", age: 11), // names optional
    ("Joy", 21),
    ("Madhu", 31)
);


// test index data
var id2 = PersonalDetail.InstanceFromId(2);

// check index function won't crash on bad dara
var idbad = PersonalDetail.InstanceFromId(11);

// force error
PersonalDetail.Add(("Tapan", 200));
*/
2. if 'Id is also used by other classes, or can be changed for a given instance: for that, I would consider a static Id manager class.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12

SuggestionHow do I run adb or fastboot command in the folder location? Pin
Member 1362111716-Jan-18 17:29
Member 1362111716-Jan-18 17:29 
GeneralRe: How do I run adb or fastboot command in the folder location? Pin
Richard MacCutchan16-Jan-18 22:08
mveRichard MacCutchan16-Jan-18 22:08 
PraiseRe: How do I run adb or fastboot command in the folder location? Pin
Member 1362111717-Jan-18 7:36
Member 1362111717-Jan-18 7:36 
GeneralRe: How do I run adb or fastboot command in the folder location? Pin
Pete O'Hanlon17-Jan-18 8:59
mvePete O'Hanlon17-Jan-18 8:59 
GeneralRe: How do I run adb or fastboot command in the folder location? Pin
Richard MacCutchan17-Jan-18 23:26
mveRichard MacCutchan17-Jan-18 23:26 
GeneralRe: How do I run adb or fastboot command in the folder location? Pin
Member 1362111717-Jan-18 11:54
Member 1362111717-Jan-18 11:54 
QuestionError: Object reference not set to an instance of an object in C# Pin
Member 1030297116-Jan-18 13:52
Member 1030297116-Jan-18 13:52 
AnswerRe: Error: Object reference not set to an instance of an object in C# Pin
OriginalGriff16-Jan-18 21:12
mveOriginalGriff16-Jan-18 21:12 
GeneralRe: Error: Object reference not set to an instance of an object in C# Pin
Member 1030297117-Jan-18 2:47
Member 1030297117-Jan-18 2:47 
GeneralRe: Error: Object reference not set to an instance of an object in C# Pin
Pete O'Hanlon17-Jan-18 2:55
mvePete O'Hanlon17-Jan-18 2:55 
AnswerRe: Error: Object reference not set to an instance of an object in C# Pin
Ralf Meier16-Jan-18 21:14
mveRalf Meier16-Jan-18 21:14 
AnswerRe: Error: Object reference not set to an instance of an object in C# Pin
V.16-Jan-18 22:06
professionalV.16-Jan-18 22:06 
GeneralRe: Error: Object reference not set to an instance of an object in C# Pin
Member 1030297117-Jan-18 3:00
Member 1030297117-Jan-18 3:00 
GeneralRe: Error: Object reference not set to an instance of an object in C# Pin
F-ES Sitecore17-Jan-18 4:05
professionalF-ES Sitecore17-Jan-18 4:05 
GeneralRe: Error: Object reference not set to an instance of an object in C# Pin
Pete O'Hanlon17-Jan-18 4:41
mvePete O'Hanlon17-Jan-18 4:41 
GeneralRe: Error: Object reference not set to an instance of an object in C# Pin
V.18-Jan-18 3:25
professionalV.18-Jan-18 3:25 
QuestionException raised at wpf designer level Pin
abboudi_ammar16-Jan-18 7:35
abboudi_ammar16-Jan-18 7:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.