-3

Can someone please explain to me what this error means? I am new to VB programming and don't understand this.

Error : Type 'var' is not defined

Here's my code

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
                     ''# truncated for brevity
                 }

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)

        ''# Here's the error line
        For Each var In result
            sb.Append(item.SalesRepID + _seperator)
            sb.Append(item.LastName + _seperator)
            sb.Append(item.FirstName + _seperator)
        Next
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
C'est Moi
  • 39
  • 1

4 Answers4

4

The C# keyword var does not have a direct equivalent in VB.NET, rather, you need to set Option Infer On.

Alternatively, you can specify the type explictly:

For Each foo As Sale In result
Community
  • 1
  • 1
Justin M. Keyes
  • 6,679
  • 3
  • 33
  • 60
  • unfortunately `result` is coming out as an anonymous type, so specifying the type without a [poco](http://en.wikipedia.org/wiki/Plain_Old_CLR_Object) could be tough. – Chase Florell Mar 04 '12 at 17:59
  • @JustinMKeys if he's not making his Anonymous type a `List`, then the `For Each` loop isn't going to know that var is a variable instead of a type. The problem isn't with the loop. – Chase Florell Mar 04 '12 at 21:47
0

Here's what I came up with. Your anonymous type isn't being set ToList() so your loop isn't going to work as expected. Also you were using a var variable and then item within the loop.

See my adjustments.

' I did NOT test your LINQ query, I just added .ToList()
Dim results = (From s In reader.GetSales() _
                  Join c In reader.GetSalesRep() _
                  On s.SalesRepID Equals c.SalesRepID _
                  Order By c.SalesRepID _
                  Select New List(Of Results) With { _
                      .SalesRepID = c.SalesRepID, _
                      .FirstName = c.FirstName
                      ''# truncated for brevity
                  }).ToList() 

'...

If Not File.Exists(path) Then
    ''# not sure the purpose of the streamWriter here.
    ''# Using sw As StreamWriter = File.CreateText(path)

        For Each result In results ''# Notice the 'item' not 'var'
            sb.Append(result.SalesRepID + _seperator)
            ''# ...
        Next
End If

Edit

In descussing with @cHao, if Option Strict is turned on in your code, you'll probably need to make a POCO for your Anonymous Type result ... see below.

Public Class Result
    Private Property _SalesRepID As Integer
    Public Property SalesRepID As Integer
        Get
            return _SalesRepID
        End Get
        Set(ByVal Value As Integer)
            _SalesRepID = value
        End Set
    End Property

    ' Wash / Rinse / Repeat for ALL your properties
End Class

Then in your example code

' Again, I did NOT test your LINQ query, I just added .ToList()
Dim results As List(Of Result) = (From s In reader.GetSales() _
                                  Join c In reader.GetSalesRep() _
                                  On s.SalesRepID Equals c.SalesRepID _
                                  Order By c.SalesRepID _
                                  Select New Result With { _
                                     .SalesRepID = c.SalesRepID, _
                                     .FirstName = c.FirstName
                                     ''# truncated for brevity
                                  }).ToList()

' ....

For Each result As Result In results ''# Notice the 'result' not 'var'
    sb.Append(result.SalesRepID + _seperator)
    ''# ...
Next

' Apologies for minor mistakes, the SO editor isn't quite as awesome as Visual Studio.
Community
  • 1
  • 1
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • While true, and while it does fix the problem that would appear next...if this were *the* problem, wouldn't the error say something about the undeclared identifier 'item' rather than an undefined type? – cHao Mar 04 '12 at 18:06
  • so with the code above you're getting `Error : Type 'item' is not defined`? – Chase Florell Mar 04 '12 at 18:12
  • I would think so. I don't have VS on this machine and don't feel like installing it, but it seems to me that the problem is that `Option Infer` is off while `Option Strict` is on, making `For Each var/item/whatever In result` bad grammar. – cHao Mar 04 '12 at 18:26
  • Please test it in an IDE. If you're using a more current version of VB.NET (v10 I think), the above code should work just fine. You don't even have to add `Option Infer`. I've been doing this in my project without issue. – Chase Florell Mar 04 '12 at 18:31
  • It's in the same boat as Auto Properties in VB.. they work now too. `Public Property myProperty As String` is the same as `public string myProperty {get; set;}` – Chase Florell Mar 04 '12 at 18:32
  • I'm aware of this. (I have VS2010 on my other machine -- and there's a setting for `Option Infer` in the IDE options.) But if the problem is really just the misnaming of `var`, then why is the error about "undefined type 'var'" rather than "undeclared identifier 'item'"? – cHao Mar 04 '12 at 18:40
  • If you don't have an IDE, are you seeing this error in deployed code? If so, do you have a link to the stack trace? – Chase Florell Mar 04 '12 at 18:42
  • Bah. Hold on...lemme install VB2010. (I don't have stack traces or error messages other than what the OP provided. Maybe i should see with my own eyes.) – cHao Mar 04 '12 at 18:47
  • If `Option Strict` is on, then the op will need to make a poco for `Result` – Chase Florell Mar 04 '12 at 18:49
  • Try as i might, i can not seem to get it to complain about an undefined type 'var'. Even with the names different. Something's a bit odd here. The only thing it complains about is `item` not being defined. – cHao Mar 04 '12 at 18:56
  • Just did an edit... you had me thinking ;-), but all in all, yeah... it's weird that it's complaining about item. Could be because the Anonymous Type is not `ToList()` – Chase Florell Mar 04 '12 at 19:00
  • Calling .ToList() is _almost never_ the right thing to do and should be avoided. Learn to work directly with the enumerables instead. – Joel Coehoorn Mar 04 '12 at 22:34
  • Thanks Mehn.. u guys rock...SOLVED – C'est Moi Mar 05 '12 at 05:37
  • @JoelCoehoorn, calling `.ToList()` allows you to cache the data within the list. Otherwise, if you're caching the query, you'd be defeating the purpose of caching. Not that we're caching anything here, just saying, `ToList()` can be helpful in this instance. – Chase Florell Jan 26 '13 at 20:45
0

You seem to be using "var" yet you have not defined it. Note: in VB a variable is defined with the predicate DIM. You would need to declare a variable before you use it. The C# construct for VAR does not fully apply here verbatim. However, I am not a strong users of VB myself, but do recall that you can do:

DIM var (creates the var) DIM var as String (type declared later)

Dim can be taken as "I am declaring a variable but can't tell you about how it behaves yet."

Simply put, in VB you can create the variable before you assign it a type or a value. I would, however, point you to a much better explanation on the differences of DIM and VAR.

Scott Hanselman - Back to Basics: var != Dim

I assume you are writing some kind of office code? C# should generally be just as good (if not better in some regards) to using VB. One exception of course is referenced in the article stated when dealing with OFFICE integrated applications.

Hope this helps.

Community
  • 1
  • 1
Zack Jannsen
  • 622
  • 7
  • 17
  • All readers, I generally prefer C# myself. That in no way indicates that one code is better than the other (as both get translated to CIL in the end). Please do not downgrade my response based on "personal code preference" as that is a mute topic most of the time. – Zack Jannsen Mar 04 '12 at 18:09
  • In VB you don't have to `Dim` the variable before using it in a For Each loop. `For Each item As result In results` works just fine. Alternatively you can use `For Each item In results` as well. – Chase Florell Mar 04 '12 at 18:11
  • 3
    @Zack: "moot" topic, not "mute". – cHao Mar 04 '12 at 18:21
0

Try this:

For Each item In result

For this to work, you must have Option Infer set to On. Also, it looks like you're using a stringbuilder as a buffer that you will eventually write to file. It would be better to write directly to your file stream.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794