Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
<pre>    
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If CheckBox1.Checked = True Then
            Dim a As String = "-clone"
        Else
        End If
        If CheckBox2.Checked = True Then
            Dim b As String = "-noerror"
        Else
        End If
        If CheckBox3.Checked = True Then
            Dim c As String = "-nocorr"
        Else
        End If
        If CheckBox4.Checked = True Then
            Dim d As String = "-notrunc"
        Else
        End If
        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter(Application.StartupPath & "test.bat", True)
        file.WriteLine("readom dev=0,0,0 f=Backup.raw" + a + b + c + d)
        file.Close()
    End Sub


What I have tried:

error is if ı added
+ a + b + c + d
code like this
file.WriteLine("readom dev=0,0,0 f=Backup.raw" + a + b + c + d)

say this Error BC30451 'a' is not declared. It may be inaccessible due to its protection
but ı declerate in a is
If CheckBox1.Checked = True Then
            Dim a As String = "-clone"
        Else
here
where did i go wrong ?
Posted
Updated 8-Aug-20 8:57am
v2

Sandeep's answer is partially correct, but I think you want the possibility of any combination of the options, so something like:
VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim someString  As String
    someString = ""     
    If CheckBox1.Checked = True Then
        someString += "-clone "
    End If
    If CheckBox2.Checked = True Then
        someString += "-noerror "
    End If
    If CheckBox3.Checked = True Then
        someString += "-nocorr "
    End If
    If CheckBox4.Checked = True Then
        someString += "-notrunc "
    End If
    Dim file As System.IO.StreamWriter
    file = My.Computer.FileSystem.OpenTextFileWriter(Application.StartupPath & "test.bat", True)
    file.WriteLine("readom dev=0,0,0 f=Backup.raw" + someString)
    file.Close()
End Sub

However, I would suggest the use of a StringBuilder object is more efficient than the += operators.
 
Share this answer
 
v2
Comments
[no name] 8-Aug-20 13:39pm    
Of course StringBuilder is more performant. But for a button click like this I think it is really something overkill. This in particular with regard to the final file handling ;) Anyway a 5.
Richard MacCutchan 8-Aug-20 16:04pm    
Thanks. But why is StringBuilder overkill?
[no name] 9-Aug-20 13:18pm    
I think StringBuilder is useful if one concatenate hundreds or thousands of strings. In case one concatennate only a view strings it does not really gives a benefit. But that is really only my view, don't take that too serious.

(Btw: I still don't get notifications for replies on Q&A comments, therefore my delayed response, sorry)
Richard MacCutchan 10-Aug-20 2:49am    
Thanks, I appreciate your comments.
Dark Frost 10-Aug-20 5:30am    
Thank You :),but if ı use this solution
.bat files just write last CheckBox4
like this;
readom.exe -notruncdev=0.0.0 f=Backup.Raw
VB
If CheckBox1.Checked = True Then
            Dim a As String = "-clone"
        Else
        End If
        If CheckBox2.Checked = True Then
            Dim b As String = "-noerror"
        Else
        End If
        If CheckBox3.Checked = True Then
            Dim c As String = "-nocorr"
        Else
        End If
        If CheckBox4.Checked = True Then
            Dim d As String = "-notrunc"
        Else
        End If

All of the variables a,b,c,d are declared in the local scope of If. Once the If is done, they are no more available and thus the error.

try this:
VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim someString  As String       
If CheckBox1.Checked = True Then
            someString = "-clone"
        Else
        End If
        If CheckBox2.Checked = True Then
            someString = "-noerror"
        Else
        End If
        If CheckBox3.Checked = True Then
            someString = "-nocorr"
        Else
        End If
        If CheckBox4.Checked = True Then
            someString = "-notrunc"
        Else
        End If
        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter(Application.StartupPath & "test.bat", True)
        file.WriteLine("readom dev=0,0,0 f=Backup.raw" + someString)
        file.Close()
    End Sub

For more details and understanding: Scope - Visual Basic | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Dark Frost 10-Aug-20 5:30am    
Thank You :)
Quote:
Where did I go wrong this code ?

Reread your code and think about what are a, b, c and d when respective checkboxes are unchecked.
Solution should be obvious.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
Comments
[no name] 8-Aug-20 15:49pm    
a neutral 3 for this "template answer" for a question which has two answers, sorry...
Patrice T 8-Aug-20 16:19pm    
I got so many times a downvote for speaking about debugger that a 3 is perfect for me.
the template is because other solutions where already spotting problem; so no need to repeat.

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