Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 25th, 2007, 08:48 AM
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default new style object initialize in vc# 2008 express

something like:
var persons = new Person(Name = "Adam");
gives error in vc# 2008 express
i need to add a few of references since there is nothing like linqpreview?

 
Old September 25th, 2007, 11:33 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

uh? That isn't C# syntax?

var is a JavaScript or J# keyword.

Also, this is incorrect initiliazation of an object new Person(Name = "Adam"); ideally your constructor would look like

public void Person(string Name)
{
}

And your initialization would be Person person = new Person("Adam");

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
 
Old September 25th, 2007, 01:19 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
quote:uh? That isn't C# syntax?
In the old days it wasn't. In today's (or actually tomorrow's) 3.5 world it is (almost, see below).... ;)

var persons = new Person(Name = "Adam");

var is a new C# keyword for type inference. The compiler infers the type by looking at the right hand side of the assignment and then types the persons variable appropriately. It looks like weakly type JavaScript but it isn't. After this line, you won't be able to assign another object type to persons (other than a type inheriting from Person that is).

var persons = new Person(Name = "Adam");

Almost correct. You are trying to use object initializers, but got the syntax wrong. You use () first to instantiate the object, then use {..} to initialize the properties:

var persons = new Person(){Name = "Adam"};

Doug: this is just compiler magic that eventually results in:

var persons = new Person();
persons.Name = "Adam";

It allows you to set one or more properties without the need to create specialized constructors.

Nooree: does that fix the error you're getting?

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old September 25th, 2007, 03:11 PM
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks, that worked...


 
Old September 25th, 2007, 03:23 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

That is pretty cool Imar, thanks for the info! ^^

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
 
Old September 25th, 2007, 03:33 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome. There is some pretty cool stuff coming in VS 2008 / ASP.NET 3.5....


Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old September 25th, 2007, 03:59 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I have seen and worked with some of it. WPF is like GDI on Steriods, I am absolutely in love with it. So your new book is going to be out, what, next week right? ;]

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
 
Old September 25th, 2007, 04:05 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

WPF is already available today in .NET 3.0. .NET 3.5 has backwards compatibility for all of that, but comes with a lot more goodies.

Book is coming along quite nice, thank you. Next week would be nice, but I don't think I'll make it. Think March 2008 and you'll be pretty close.....

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004





Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Web Developer 2008 Express jjc9809 Visual Web Developer 2008 2 December 23rd, 2008 12:06 PM
Classic ASP in Visual Developer Express 2008 esstepansky Classic ASP Basics 2 August 25th, 2008 03:17 PM
Visual Web Developer 2008.. Express Edition Neamat .NET Framework 3.5 14 August 3rd, 2008 09:47 AM
Downloading VB 2008 Express Kiddi Visual Basic 2008 Essentials 1 June 11th, 2008 12:29 PM
Please, need help with VC 2005 express IDE! pghTech Visual C++ 2005 0 June 27th, 2006 06:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.