Handling Error While Compiling Using CodeDom - vb.net

So after fixing my problem while compiling code using CodeDom, I've encountered another problem relating to the code. This is confusing because the code works perfectly fine when I run it - it only happens when I try to compile it.
My code does this:
For every file in the directory C:\temp\ - if the file name contains "123" it adds it to a list of strings.
Dim file_list As New List(Of String)
Dim temp_directory As New IO.DirectoryInfo("C:\temp\")
Dim get_file_info As IO.FileInfo() = temp_directory.GetFiles()
Dim item As IO.FileInfo
For Each item In get_ssfn_files
If item.ToString.Contains("123") Then
file_list.Add(item.ToString)
End If
Next
Then, it will do something with each file in a for each statement.
For Each file_found in file_list
'Do Something
Next
The error seems to happen on this line:
For Each file_found in file_list
I dont know why, because like I said, the code works fine when I'm not compiling it with CodeDom. Anyone know how to go about fixing this? Thanks in advance.

If the code is being compiled with Option Infer Off, you need to specify a type for file_found.
For Each file_found As String In file_list
'Do Something
Next

Related

Receiving an error from trying to make a startup program shortcut

I am trying to make a shortcut for my program so that it starts automatically with windows. The following code is the sub that creates the shortcut and how it gets it's variables but it fails.
Dim ShortCutPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup) & "/ScreenRot.lnk"
Shared Sub CreateShortCut(File As String, ShortCutPath As String)
Dim oShell As Object
Dim oLink As Object
'you don’t need to import anything in the project reference to create the Shell Object
Try
oShell = CreateObject("WScript.Shell")
oLink = oShell.CreateShortcut(ShortCutPath)
oLink.IconLocation = File
oLink.TargetPath = File
oLink.Arguments = ""
oLink.WindowStyle = 1
oLink.Save()
Catch ex As Exception
End Try
End Sub
The line it seems to fail on is.
oLink = oShell.CreateShortcut(ShortCutPath)
The error I am getting is
DirectCast(ex, System.MissingMemberException).Message
Public member 'CreateShortcut' on type 'IWshShell3' not found.
I am using this in my program.
Imports IWshRuntimeLibrary
I have tried a couple different ways to make the shortcut but this seems to be the one that should work for what I need. I've read a bit about using this code and watched a video but nothing talks about the error. I've googled the error but nothing resembles a solution. I've tried to adjust the code slightly by using other examples but it still fails with more or less the same error. I don't really understand what the error is saying, so I can try and figure it out. thanks for your time and any help you guys can give.
After reviewing many posts and trying a lot of different things I found a solution on Stackoverflow which I can confirm actually works.
Stackoverflow Post
This post has the solution, hopefully it helps other people with this problem.

IsNothing showing error VB.NET VS2017 Community

My first post here and really basic question as I have just started learning.
I am following a step by step tutorial from youtube. https://www.youtube.com/watch?v=6utWyl8agDY code is working alright in the video. below is the code:
Imports System
Imports System.IO
Module Program
Sub Main()
Dim myReader As StreamReader = New StreamReader("values.txt")
Dim line As String = ""
While Not IsNothing(line)
line = myReader.ReadLine()
If Not IsNothing(line) Then
Console.WriteLine(line)
End If
End While
myReader.Close()
Console.ReadLine()
End Sub
End Module
Problem I am facing is that I have red error (squiggly) line under IsNothing. One thing to note is the in the video tutorial version of VS under use is 2013 while I am using VS2017 community. any idea what I am doing wrong here?
I'm not sure what your tutorial says but there are issues with that code. It's fairly poorly structured and, to be honest, you shouldn't really use IsNothing anyway. The most immediate issue is where you're reading the text and that you're testing for Nothing twice. The code would be better written like this:
Dim myReader As New StreamReader("values.txt")
Dim line As String = myReader.ReadLine()
While line IsNot Nothing
Console.WriteLine(line)
line = myReader.ReadLine()
End While
myReader.Close()
Console.ReadLine()
Comparing directly to Nothing using Is or IsNot is the way to go and there's also no need for that check inside the loop.
If you want to get a bit more advanced, you can also create the StreamReader with a Using statement, so it gets closed implicitly:
Using myReader As New StreamReader("values.txt")
Dim line As String = myReader.ReadLine()
While line IsNot Nothing
Console.WriteLine(line)
line = myReader.ReadLine()
End While
End Using
Console.ReadLine()
Better still would be to not create the StreamReader yourself at all but let the Framework do that for you:
For Each line As String In File.ReadLines("values.txt")
Console.WriteLine(line)
End Using
Console.ReadLine()
Note that that code uses ReadLines rather than ReadAllLines. Both would work but the former will only read one line at a time while the latter will read the whole file first, load the lines into an array and return that. In this case it probably doesn't really matter which you use but ReadLines is generally preferable unless you specifically need all the lines first or you want to loop over them multiple times.
You have to decide whether your console application requires .net Core or .net Framework is sufficient.
As others have written in responses, your code is in need of improvement.
However, your code will run correctly if you choose the .net framework for your new project in Visual Studio 2017 Community as shown in the screenshot below.

VB can't string to arraylist when read from file

Either I'm missing something really obvious or something about vb is really messed up. I'm trying to read in from a file and add the lines to an arraylist... pretty simple If I add strings to the arraylist this way
selectOptions.Add("Standard")
selectOptions.Add("Priority")
selectOptions.Add("3-Day")
selectOptions.Add("Overnight")
I have no problems
But when I do this it appears to end up empty which makes no sense to me.
Dim reader As StreamReader = My.Computer.FileSystem.OpenTextFileReader(path)
Dim line As String
Do
line = reader.ReadLine
selectOptions.Add(line)
Loop Until line Is Nothing
reader.Close()
Messagebox.show line all day so I know it is reading the file and the file isn't empty and I have checked the type of line which comes back as string. This makes no sense to me.
Checking for reader.EndOfStream in a While loop will probably work better:
Dim reader As New StreamReader(path)
Dim line As String
While Not reader.EndOfStream
line = reader.ReadLine
selectOptions.Add(line)
End While
reader.Close()
You can also get an exception if selectOptions isn't declared as a New ArrayList, if you properly have all your Options turned On.
Another thing to remember, if your code is in the form's Load Handler, it won't throw an exception it will just break out of the handler routine and load the form. This makes it really hard to find things like bad file names, badly declared objects, etc.
One thing I do is put suspect code in a button's Click handler and see what exceptions it throws there.
Of course this could all be moot if you use the File.ReadAllLines method and add it directly to the ArrayList:
selectOptions.AddRange(File.ReadAllLines(path))

How do I pass ItemSpec into GetBranchHistory()?

I'm trying to get information about specific branches in TFS, so to start, I'm trying to create a variable to assign as a BranchHistoryTreeItem. However, when I pass in the ItemSpec, I'm getting an error on Spec (not the definition, but where it's passed into GetBranchHistory):
Value of type 'Microsoft.TeamFoundation.VersionControl.Client.ItemSpec' cannot be converted to '1-dimensional array of Microsoft.TeamFoundation.VersionControl.Client.ItemSpec'
I understand the error, but I'm not entirely sure why it throwing it. Isn't this data type exactly what it's looking for? I believe I have the ItemSpec declared correctly, but I'm a bit lost here. Can anyone offer some advice on why I'm getting this? Code:
Sub GetBranchInfo()
Dim tfs As New TfsTeamProjectCollection(Common.BuildServerURI)
Dim Version = tfs.GetService(Of VersionControlServer)()
Dim Spec As New ItemSpec("$/Project1", RecursionType.None)
Dim BranchHistory As New BranchHistoryTreeItem(Version.GetBranchHistory(Spec, VersionSpec.Latest))
End Sub
GetBranchHistory takes an array of ItemSpecs.
My VB is a little rusty, but I think you want something like:
Dim Spec As New ItemSpec("$/Project1", RecursionType.None)
Dim Specs(1) = new ItemSpec() {Spec}
Dim BranchHistory As New BranchHistoryTreeItem(Version.GetBranchHistory(Specs, VersionSpec.Latest))

LINQ VB.Net variable not declared error

I am trying to run this line of code:
Dim OrderedFiles() As String = Directory.GetFiles(FilePath).OrderBy(x >= x.CreationTime)
I get an error on x saying x is not declared.
I have my project set to Option Strict Off and Option Infer On. If I turn ON Option Strict then I get thousands of errors from the project(it is inherited) and I don't have the time to fix all of them, but x no longer gives me an error. I have googled until I want to throw my computer out the window.
Any help on how to correct this statement would be appreciated.
Edit:
I was hoping for a more elegant solution but here is what I came up with to solve this particular problem.
Dim fileList() As String = Directory.GetFiles(FilePath)
Dim fileDate(fileList.Length - 1) As DateTime
For i As Integer = 0 To fileList.Length - 1
fileDate(i) = New FileInfo(fileList(i)).CreationTime
Next
Array.Sort(fileDate, fileList)
With EmailTemplates_DropDownList
.DataSource = fileList.Reverse.Take(5)
.DataBind()
End With
It is not particularly elegant but it does the job. I was hoping for a one liner LINQ solution and I just don't have the background in LINQ to know how to do the job, time to go buy a book.
I'm not sure where you got this syntax from:
OrderBy(x >= x.CreationTime)
That almost looks like the C# syntax of
OrderBy(x => x.CreationTime)
but I believe in VB you would use
OrderBy(Function(x) x.CreationTime)
That's certainly what the example in Enumerable.OrderBy would suggest.
EDIT: At that point you'll get a different error, as per Steve's post... but he hasn't corrected the syntax. I suspect you want:
Dim OrderedFiles() As FileInfo = new DirectoryInfo(FilePath).GetFiles().
OrderBy(Function(x) x.CreationTime).
ToArray()
Directory.GetFiles() returns the names of files (including their paths) in the specified directory.
It's not possible to use x.CreationTime, x is a string
Probably you should use DirectoryInfo
Dim di as DirectoryInfo = new DirectoryInfo(FilePath)
Dim OrderedFiles = di.GetFiles().OrderBy(Function(x) x.CreationTime).Take(3)
Dim fi as FileInfo
For each fi in OrderedFiles
Console.WriteLine(fi.FullName)
Next