Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

Please can someone explain this error to me? I don't know what to make of it.

Thanks in Advance

Type var is not defined

VB
Dim sb As New StringBuilder(50)
				Dim path As String = "..\debug\CarSalesFiles\Commissions" + DateTime.Today.Year + DateTime.Today.Month + DateTime.Today.Day + ".txt"
				If Not File.Exists(path) Then
					Using sw As StreamWriter = File.CreateText(path)
                        For Each item As var In result
                            sb.Append(item.SalesRepID + _seperator)
                            sb.Append(item.LastName + _seperator)
                            sb.Append(item.FirstName + _seperator)
                            sb.Append(item.PhotoFile + _seperator)
                            sb.Append(item.StartDate + _seperator)
                            sb.Append(item.Model + _seperator)
                            sb.Append(item.Manufacturer + _seperator)
                            sb.Append(item.Color + _seperator)
                            sb.Append(item.Year + _seperator)
                            sb.Append(item.OdometerReading + _seperator)
                            sb.Append(item.VehicleID + _seperator)
                            sb.Append(item.AcquisitionCost + _seperator)
                            sb.Append(item.AquiredDate + _seperator)
                            sb.Append(item.DateSold + _seperator)
                            sb.Append(item.MaintenanceCost + _seperator)
                            sb.Append(item.SellingPrice + _seperator)


		End Sub
Posted
Comments
Sergey Alexandrovich Kryukov 4-Mar-12 2:18am    
What is the type of result?
--SA

1 solution

There is not such keyword as var in VB.NET, but there is one in C#. And this is not a type. And you did not define type named var. Instead of var, use appropriate type explicitly.

I don't know your result type, so I don't know the type of item, but if the type of result is a collection or an array, use its element type.

Background:

The type supporting for each implements the generic interface System.Collections.IEnumerable<T>. The method System.Collections.IEnumerable.GetEnumerator returns a value of the type implementing the interface System.Collections.IEnumerator<code><T>. This type works with the elements of type T, and this is the type of the variable of for each loop.

So:
C#
// Having

class SomeType { /* in fact, any type, not just class or structure */ }

class SomeEnumerableType : System.Collections.IEnumerable<SomeType> {
    System.Collections.IEnumerator<SomeType> System.Collections.IEnumerable<SomeType>.GetEnumerator() {
        //implementation of this interface
    }
    //...
}

// we can write:
SomeEnumerableType items = new SomeEnumerableType( /* ... */ ); 

For Each item As SomeType In items
    //...


Please see:
http://msdn.microsoft.com/en-us/library/9eekhta0.aspx[^],
http://msdn.microsoft.com/en-us/library/78dfe2yb.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Techhi 4-Mar-12 2:33am    
I converted from C# to VB.NET. I don't have a clue on how to go about that.Can you explain to me further please?
Sergey Alexandrovich Kryukov 4-Mar-12 3:19am    
OK. Read again if something is not clear and ask particular questions. "further" is not informative.
Do you have C# background?
--SA
Techhi 4-Mar-12 2:55am    
Hmm still very much confused friend.
Sergey Alexandrovich Kryukov 4-Mar-12 3:21am    
For you, please note. Anyway, how can I help you?
You have enough referenced. Read. And I solved you particular problem in the second sentence.
Show me the declaration of "result" is you still don't know how to complete this like of code...
--SA
Techhi 4-Mar-12 8:22am    
Hmm, i think i have an idea but here is my result declaration.


<pre lang="vb"> Dim result = From s In reader.GetSales() _
Join c In reader.GetSalesRep() _
On s.SalesRepID Equals c.SalesRepID _
Order By c.SalesRepID _
Select New _
With { _
.SalesRepID = c.SalesRepID, _
.FirstName = c.FirstName, _
.LastName = c.LastName, _
.PhotoFile = c.PhotoFile, _
.Manufacturer = s.Manufacturer, _
.Model = s.Model, _
.Color = s.Color, _
.Year = s.Year, _
.DateSold = s.DateSold, _
.AcquisitionCost = s.AcquisitionCost, _
.MaintenanceCost = s.MaintenanceCost, _
.SellingPrice = s.SellingPrice, _
.AcquiredDate = s.AcquiredDate, _
.OdometerReading = s.OdometerReading, _
.StartDate = c.StartDate, _
.VehicleID = s.VehicleID _
}




Dim sb As New StringBuilder(50)
Dim path As String = "..\debug\CarSalesFiles\Commissions" + DateTime.Today.Year + DateTime.Today.Month + DateTime.Today.Day + ".txt"
If Not File.Exists(path) Then
Using sw As StreamWriter = File.CreateText(path)
For Each item As var In result</pre>

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