I am using Resharper 4.5 in Visual Studio 2008. Whenever I try to extract a block of code into a method, it tries to create a subroutine and not a function. The return type option is disabled. Does anyone have any advice as to how I can get it to create a function and not a subroutine?
thanks!
It's likely that the code you've highlighted doesn't have anything to return. If the code you've highlighted doesn't set variables that are used further down your code then there's nothing for your refactored code to return.
For example, if I highlight this code and Extract Method...
Program p = new Program();
p.DoStuff();
... there's nothing to return (I don't reference p beyond this code). If I highlight the first 2 lines from this code...
Program p = new Program();
p.DoStuff();
p.DoMoreStuff();
... then Resharper will create a method returning an instance of Program (i.e. "p").
Related
Since updating to Visual Studio 2017 I have observed unwanted behavior when constructing if statements, in that intellisense doesn't recognize variable names when I attempt to surround the clause in parenthesis. Consider the following incomplete code:
class AnythingClass
....
end class
public Sub doSomething()
Dim anythingInstance as new AnythingClass
if anyt
When typing code in this manner, the instance "anythingInstance" will be suggested by autocomplete. However, if I open parentasis when starting the if clause as below
class AnythingClass
....
end class
public Sub doSomething()
Dim anythingInstance as new AnythingClass
if (anyt
then the declared variable will not show in the autocomplete suggestions. The class name AnythingClass will show up in the suggestions, but not the instance
UPDATE
(edit) THIS ANALYSIS IS INCORRECT, please see my answer for details as to why
I have tried creating a brand new project and get the expected behavior. So this only appears to happen in projects that have previously been upgraded from older versions of visual studio.
I have attached a couple of screenshots for the skeptics!
Based on comments from #Maciej Los and #TnTinMn, I have managed to piece together the issue.
If you write the code as:
IF (an...
with a whitespace between the IF and the arguments, Then autocomplete appears to treat it as an If statement and will display instance variables. However if you write the code as
IF(an...
with no whitespace, autocomplete appears to treat it as the If operator and not show declared instances. Quite why this is the case, I don't know. My original analysis of there being a difference between new projects and previously imported ones was incorrect.
I'm building a program in VB whose purpose is to run in the background and automatically update other programs I've created. To do this, it has to check if those programs are still running and, if they are, wait for them to close.
Unfortunately, the program won't compile. The exception states: GetProcessesByName is not a member of String. (The string it refers to is the Process parameter shown in the code below.)
I can't understand why this is happening, because this method has always worked without problems. I'm using Visual Studio 2015. For your reference, here's the code block:
Private Function CheckIfRunning(Process As String) As Boolean
Dim MyProcess() As Process
MyProcess = Process.GetProcessesByName("ProcessName")
If MyProcess.Count > 0 Then
Return True
Else
Return False
End If
End Function
Try using System.Diagnostics.Process.GetProcessesByName("ProcessName")
Since you've declared Process as a string parameter, Process.GetProcessesByName refers to the string instead of the System.Diagnostics method. Alternatively, you can use a different name for the string parameter.
I have made a vb project that works with registry and system management but my code is like this:
CheckAnswer(question_id)
it means that each question has own works with sys registry and sys management and I wanted that the program read each question from file at runtime.
my CheckAnswer body function that I made is :
Private Sub CheckAnswer(ByVal id As Integer)
Dim mark As Integer
mark = 0
''''''''''''''''''''''
Select Case id
Case 1
Dim r As Rectangle = Nothing
If GetTaskBarPosition(r, Me.Handle) Then
If r.Y = 0 And r.X = 0 Then mark = 1
End If
Case 2
If My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\", "TaskbarSizeMove", 1) = 0 Then mark = 1
end case
but I wanted to do this work at runtime and say to program that read function from a file not from the source code for flexibility and adding new questions without changing the program source and just with changing the function file.
I have searched this site and others for code generation and the other things like this and code provider. I have get a project from "code project" but give me compile errors.
I do not want to generate separate class code because I have implemented imports and libraries in my own class and I want to say the program just which one of them I want to use in my function file.
It is possible to write VB.Net script files and execute them at runtime using the VBCodeProvider, in the future you will be able to make use of the Roslyn compiler if required.
I have a short code example for compiling and running C# Scripts, to make it work for VB.Net you would simply need to convert the code from C#, and use the VB.Net provider.
Another option would be to use F# which supports script files out-of-the-box (extension .fsx) which can be easily edited and executed.
I am just reworking my VB6 in .NET.
I have a function that is called NonNullString(byval uAny As Object) As String
In VB6 I worked with a sqlite wrapper, and a recordset's member could be accessed by using
Dim sString$
sString = r("somefield")
(without ".Value")
I have really many of these fields, and I changed most of them to ".Value", but for some I forgot it.
An exception is therefore raised in the function NoNullString, and I am looking for a way to quickly jump out of the function in order to see what the caller was and improve the code.
F5 does not do the job.
Does anybody have any ideas?
Thank you!
Press CTRL+L to see call stack. From there you can navigate through the stack.
You can then use Set Next Statement (CTRL+F9) on the End Function of your errored function. Two times F10 to complete execution of this function. Repeat this step till you are in the scope where you think the error originated. Then, if you are on x86 (so you have Edit&Continue available), fix your code, and drag your currently executed line to the moment when this fix would occur. And then try running your function again.
Unfortunately, you cannot Set Next Statement outside of the current block function/sub, which I was going to suggest originally.
I've got a workbook that declares a global variable that is intended to hold a COM object.
Global obj As Object
I initalize it in the Workbook_Open event like so:
Set obj = CreateObject("ComObject.ComObject");
I can see it's created and at that time I can make some COM calls to it.
On my sheet I have a bunch of cells that look like:
=Module.CallToComObject(....)
Inside the Module I have a function
Function CallToComObject(...)
If obj Is Nothing Then
CallToComObject= 0
Else
Dim result As Double
result = obj.GetCalculatedValue(...)
CallToComObject= result
End If
End Function
I can see these work for a bit, but after a few sheet refreshes the obj object is no longer initialized, ie it is set to Nothing.
Can someone explain what I should be looking for that can cause this?
Any of these will reset global variables:
Using "End"
An unhandled runtime error
Editing code
Closing the workbook containing the VB project
That's not necessarily an exhaustive list though...
I would suggest a 5th point in addition to Tim's 4 above: Stepping through code (debugging) and stopping before the end is reached. Possibly this could replace point number 3, as editing code don't seem to cause global variable to lose their values.