Click here to Skip to main content
14,334,896 members
   

C#

 
AnswerRe: call enum as argument in Method? Pin
BillWoodruff7-Sep-19 15:29
mveBillWoodruff7-Sep-19 15:29 
AnswerRe: call enum as argument in Method? Pin
BillWoodruff7-Sep-19 19:01
mveBillWoodruff7-Sep-19 19:01 
AnswerRe: call enum as argument in Method? Pin
OriginalGriff7-Sep-19 20:03
protectorOriginalGriff7-Sep-19 20:03 
AnswerRe: call enum as argument in Method? Pin
Gerry Schmitz8-Sep-19 8:09
mveGerry Schmitz8-Sep-19 8:09 
AnswerRe: call enum as argument in Method? Pin
Sharp Ninja9-Sep-19 4:56
memberSharp Ninja9-Sep-19 4:56 
QuestionVlookup a Value on a CSV file C# Pin
Member 1457406731-Aug-19 9:52
memberMember 1457406731-Aug-19 9:52 
AnswerRe: Vlookup a Value on a CSV file C# Pin
OriginalGriff31-Aug-19 19:55
protectorOriginalGriff31-Aug-19 19:55 
AnswerRe: Vlookup a Value on a CSV file C# Pin
BillWoodruff1-Sep-19 12:00
mveBillWoodruff1-Sep-19 12:00 
Assuming you are someone who really wants to learn C#, consider this pattern:

1 define a Class for 'Person:
// required
// using System.Collections.Generic;
// using System.IO;
// using System.Linq;

public class Person
{
    public Person(string name, int age, int telNum, string emailId)
    {
        Name = name;
        Age = age;
        TelNum = telNum;
        EmailId = emailId;
    }

    public string Name { get; set; }
    public int Age { get; set; }
    public int TelNum { get; set; }
    public string EmailId { get; set; }
}

// 2 define a factory that reads a file, and creates a List<Person>

public static class PersonFactory
{

    static char[] delimiter = new []{','};
    
    public static Person MakePerson(string line)
    {
        try
        {
            var data = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
            var name = data[0];
            var age = Int32.Parse(data[1]);
            var tel = Int32.Parse(data[2]);
            var eml = data[3];
    
            return new Person(name, age, tel, eml);
        }
        catch (Exception ex)
        {
            throw new InvalidDataException(ex.Message);
        }
    }
    
    public static List<Person> PersonsFromFile(string filepath)
    {
        // note use of Skip to ignore first line
        // which contains header informatiomn
        var lines = File.ReadAllLines(filepath).Skip(1);
    
        return lines.Select(line => MakePerson(line.Trim())).ToList();
    }
}
Sample usage:
var persons = PersonFactory.PersonsFromFile(@"C:\Users\###\##\##?.txt");
            
int chrisage = persons.First(p => p.Name == "Chris").Age;

«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot


modified 1-Sep-19 22:04pm.

Questionreading text file as an array by using streamreader Pin
Member 1457152028-Aug-19 19:37
memberMember 1457152028-Aug-19 19:37 
AnswerRe: reading text file as an array by using streamreader Pin
OriginalGriff28-Aug-19 19:56
protectorOriginalGriff28-Aug-19 19:56 
AnswerRe: reading text file as an array by using streamreader Pin
Richard MacCutchan28-Aug-19 21:27
protectorRichard MacCutchan28-Aug-19 21:27 
QuestionRe: reading text file as an array by using streamreader Pin
Maciej Los28-Aug-19 21:42
protectorMaciej Los28-Aug-19 21:42 
AnswerRe: reading text file as an array by using streamreader Pin
Maciej Los28-Aug-19 22:32
protectorMaciej Los28-Aug-19 22:32 
AnswerRe: reading text file as an array by using streamreader Pin
BillWoodruff28-Aug-19 23:04
mveBillWoodruff28-Aug-19 23:04 
AnswerRe: reading text file as an array by using streamreader Pin
Gene M10-Sep-19 14:48
memberGene M10-Sep-19 14:48 
AnswerRe: reading text file as an array by using streamreader Pin
Jacek Gajek12-Sep-19 9:35
memberJacek Gajek12-Sep-19 9:35 
QuestionTool to add code in my existing class Pin
Bastien Vandamme28-Aug-19 5:48
memberBastien Vandamme28-Aug-19 5:48 
AnswerRe: Tool to add code in my existing class Pin
Richard MacCutchan28-Aug-19 5:54
protectorRichard MacCutchan28-Aug-19 5:54 
AnswerRe: Tool to add code in my existing class Pin
Richard Deeming28-Aug-19 7:47
mveRichard Deeming28-Aug-19 7:47 
AnswerRe: Tool to add code in my existing class Pin
BillWoodruff28-Aug-19 18:35
mveBillWoodruff28-Aug-19 18:35 
QuestionValidating textbox value against an excel worksheet and append to datagridview Pin
Member 1457117728-Aug-19 4:51
memberMember 1457117728-Aug-19 4:51 
AnswerRe: Validating textbox value against an excel worksheet and append to datagridview Pin
ZurdoDev28-Aug-19 5:20
professionalZurdoDev28-Aug-19 5:20 
AnswerRe: Validating textbox value against an excel worksheet and append to datagridview Pin
OriginalGriff28-Aug-19 5:23
protectorOriginalGriff28-Aug-19 5:23 
GeneralRe: Validating textbox value against an excel worksheet and append to datagridview Pin
BillWoodruff28-Aug-19 18:49
mveBillWoodruff28-Aug-19 18:49 
AnswerRe: Validating textbox value against an excel worksheet and append to datagridview Pin
BillWoodruff28-Aug-19 18:46
mveBillWoodruff28-Aug-19 18:46 

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.