Click here to Skip to main content
15,388,892 members
Home / Discussions / C#
   

C#

 
GeneralRe: Time Elapse Pin
Aelmfr16-Mar-16 15:44
MemberAelmfr16-Mar-16 15:44 
GeneralRe: Time Elapse Pin
Patrice T16-Mar-16 15:58
mvePatrice T16-Mar-16 15:58 
GeneralRe: Time Elapse Pin
Garth J Lancaster16-Mar-16 16:06
professionalGarth J Lancaster16-Mar-16 16:06 
GeneralRe: Time Elapse Pin
Patrice T16-Mar-16 16:11
mvePatrice T16-Mar-16 16:11 
JokeRe: Time Elapse Pin
Richard Deeming17-Mar-16 2:00
mveRichard Deeming17-Mar-16 2:00 
PraiseRe: Time Elapse Pin
Patrice T17-Mar-16 4:32
mvePatrice T17-Mar-16 4:32 
AnswerRe: Time Elapse Pin
V.16-Mar-16 22:15
professionalV.16-Mar-16 22:15 
QuestionConverting Array To Dictionary Pin
MadDashCoder16-Mar-16 11:16
MemberMadDashCoder16-Mar-16 11:16 
I am trying to convert an array to a dictionary but my code is throwing a null exception. Below is my code
C#
    static void Main(string[] args)
    {
        Product p1 = new Product();
        p1.ProductSkew = 99;
        p1.ProductName = "Accord";
        p1.Color = "Black";
        p1.Price = 35000;

        Product p2 = new Product();
        p2.ProductSkew = 46;
        p2.ProductName = "Corvette";
        p2.Color = "Black";
        p2.Price = 65000;

        Product p3 = new Product();
        p3.ProductSkew = 35;
        p3.ProductName = "Altima";
        p3.Color = "Black";
        p3.Price = 32000;

        Product[] products = new Product[3];

        products[0] = p1;
        products[1] = p2;
        products[2] = p3;

        Dictionary<int, Product> productlist = products.ToDictionary(p => p.ProductSkew, p => p);

        foreach (KeyValuePair<int, Product> item in productlist)
        {
            Product product = new Product();
            product = item.Value;

            Console.WriteLine(
            "\nSkew: {0} \n" + "Name: {1} \n" +  "Color: {2} \n" +  "Price: {3} \n",
            product.ProductSkew, product.ProductName, product.Color, product.Price );
        }
        Console.ReadLine();
    }
}

public class Product
{
    public int ProductSkew { get; set; }
    public string ProductName { get; set; }
    public string Color { get; set; }
    public double Price { get; set; }

    public void AddProduct()
    {
        Console.WriteLine("Enter Product Skew.");
        this.ProductSkew = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter Product Name.");
        this.ProductName = Console.ReadLine();

        Console.WriteLine("Enter Product Color.");
        this.Color = Console.ReadLine();

        Console.WriteLine("Enter Product Price.");
        this.Price = Convert.ToDouble(Console.ReadLine());
    }
}


The syntax seems correct but when I step through the code inside of ToDictionay(), p.ProduckSkew is empty and I don't know why. Any help is greatly appreciated, thanks.
AnswerRe: Converting Array To Dictionary Pin
John Torjo16-Mar-16 11:22
professionalJohn Torjo16-Mar-16 11:22 
GeneralRe: Converting Array To Dictionary Pin
MadDashCoder16-Mar-16 11:32
MemberMadDashCoder16-Mar-16 11:32 
AnswerRe: Converting Array To Dictionary Pin
Matt T Heffron16-Mar-16 11:47
professionalMatt T Heffron16-Mar-16 11:47 
AnswerRe: Converting Array To Dictionary Pin
Mathi Mani16-Mar-16 11:47
MemberMathi Mani16-Mar-16 11:47 
GeneralRe: Converting Array To Dictionary Pin
MadDashCoder16-Mar-16 12:04
MemberMadDashCoder16-Mar-16 12:04 
SuggestionRe: Converting Array To Dictionary Pin
Sascha Lefèvre16-Mar-16 11:56
professionalSascha Lefèvre16-Mar-16 11:56 
SuggestionRe: Converting Array To Dictionary Pin
Richard Deeming17-Mar-16 1:58
mveRichard Deeming17-Mar-16 1:58 
AnswerRe: Converting Array To Dictionary Pin
Luc Pattyn17-Mar-16 7:41
sitebuilderLuc Pattyn17-Mar-16 7:41 
AnswerRe: Field Naming Pin
OriginalGriff16-Mar-16 5:55
mveOriginalGriff16-Mar-16 5:55 
GeneralRe: Field Naming Pin
Pete O'Hanlon16-Mar-16 6:36
mvaPete O'Hanlon16-Mar-16 6:36 
GeneralRe: Field Naming Pin
OriginalGriff16-Mar-16 6:41
mveOriginalGriff16-Mar-16 6:41 
GeneralRe: Field Naming Pin
jsc4217-Mar-16 23:06
professionaljsc4217-Mar-16 23:06 
GeneralRe: Field Naming Pin
OriginalGriff16-Mar-16 6:47
mveOriginalGriff16-Mar-16 6:47 
AnswerRe: Field Naming Pin
Gerry Schmitz16-Mar-16 6:45
mveGerry Schmitz16-Mar-16 6:45 
GeneralRe: Field Naming Pin
Gerry Schmitz16-Mar-16 9:08
mveGerry Schmitz16-Mar-16 9:08 
AnswerRe: Field Naming Pin
Sascha Lefèvre16-Mar-16 7:49
professionalSascha Lefèvre16-Mar-16 7:49 
GeneralRe: Field Naming Pin
Sascha Lefèvre16-Mar-16 8:44
professionalSascha Lefèvre16-Mar-16 8:44 

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.