Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,

I developed a website in .net framework 3.5 and than I was asked by my boss to integrate that to 2.0. Now almost I have fixed all the errors that were generating because of version changes but I am not able to over come an error which is not allowing me to use var keyword. Actually I am making use of EPPlus package for exporting data to excel sheets in my website. The code needs to use the var keyword at many places and I get an error which says are u missing an Assembly reference ?

Even i tried with
C#
type x = new type();
but still no go. Is there any alternative for using var ? Please help me out.
Here is my code:

C#
var cell = ws.Cells[rowIndex, colIndex];



                   //Setting the background color of header cells to Gray

                   var fill = cell.Style.Fill;
                   var vallign = cell.Style.VerticalAlignment;
                   var hallign = cell.Style.HorizontalAlignment;

Thank you !
Posted
Updated 10-Oct-12 20:39pm
v2

Var keyword is avaliable for.net framework 3.0 and above hence it will not work in .net f/w 2.0. For more information please refer:-

http://msdn.microsoft.com/en-us/library/bb383973.aspx[^]
 
Share this answer
 
v2
Comments
Oshtri Deka 11-Oct-12 2:44am    
That's right.
There were significant changes from v2.0 to 3.0(3.5).
My 5.
Peeyush Pachaori 11-Oct-12 5:32am    
Thanks Oshtri.
Taresh Uppal 11-Oct-12 2:45am    
ok i see... So is there any alternative to this referring to my code. Because the variables like cell,fill, vallign are custom type and they make me modify my excel sheet. Can i use any other substitute for the var keyword ??
Sushil Mate 11-Oct-12 2:59am    
There is no alternative for var in .net 2.0, You better upgrade your framework or explicitly convert to your custom type.
Taresh Uppal 11-Oct-12 3:11am    
how can I convert cell as a data type so that I can make it use further. I am confused a lot !
As opposed to using the var keyword, you can use the Object keyword.

The object type is an alias for Object in the .NET Framework. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object. You can assign values of any type to variables of type object. When a variable of a value type is converted to object, it is said to be boxed. When a variable of type object is converted to a value type, it is said to be unboxed.

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

Using .Net framework 3.0 and higher you'll use

C#
var myVar = new UnknownType();


Using .Net framework lower than 3.0, you'll use

C#
object myVar = new UnknownType();
 
Share this answer
 
I haven't used myself but on following link, it is explained you can use var.

http://weblogs.asp.net/shahar/archive/2008/01/23/use-c-3-features-from-c-2-and-net-2-0-code.aspx[^]

By the way, can you not use object ?
-Milind
 
Share this answer
 
Comments
Taresh Uppal 11-Oct-12 3:11am    
exactly !
I saw this link earlier also and I was amazed to see how this guy is working with the same ? Moreover I am not able to relate this blog to my code..:(
try using object in-place of var. use use the data type of the value that is being returned from the property.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900