2

My company have put me on an urgent project to update one of our existing programs from VB6 to VB.Net. This is happening in two stages, the first of which I personally see as a waste of time, but it's being insisted it be done, is to use the conversion process within Visual Studio and then clean-up errors just to get a compiled version. The size of the program is huge, but as I don't have a say at this point I'm trying to work through the hundreds of compiler errors. I do realise that what I'm doing isn't best practice and is quite a frustrating situation/waste of time.

Basically there are a number of forms, one of which can call any number of other forms, which is dictated by an If statement. When the form is loaded, variables within the new form as assigned and it opens. Some of the variables are commonly named, others are not. So in the VB6 code it would look something like this:

Dim frm As System.Windows.Forms.Form
If x=y Then
 frm = New frm1
 frm.Variable1 = "VarA"
 frm.Variable2 = "VarB"
Else
 frm = New frm2
 frm.Variable3 = "VarC"
 frm.Variable4 = "VarD"
End If
frm.Variable5 = "VarE"

I toyed with putting a separate form object within the If statements, but as they are needed outside of it as well, it doesn't really solve the issue, and as the generic items are used so much for other aspects it wouldn't be practical to duplicate them all within separate assignments.

I was hoping there would be a quick solution along the lines of

frm.Var("Variable1") = "VarA"

But I've not been able to find anything that could be simply implemented across such a large amount of conditions.

Apologies for not encouraging the best practice, because I do realise that any solution wouldn't be it, but I am looking for the quickest implementation so that I can move on to just rewriting the whole program.

3
  • Can't these forms set these properties themselves in their constructor, or get them supplied as constructor arguments? Commented Apr 17, 2015 at 13:08
  • A helper class that took a form reference and an IDictionary of name value pairs, and used reflection to find the members by key and then set them from the value? Commented Apr 17, 2015 at 13:20
  • I had contemplated something like an interim form or class, but for the amount of changes and places that I need to implement this would make it such a heavy change that I'm trying to avoid until I can get to a rewrite stage. For an idea of the scope, the amount of errors that I'm getting returned for just this issue is exceeding the VS limit (it's a really frustrating task to be given). Commented Apr 17, 2015 at 13:31

1 Answer 1

1

The ugly, but simplest solution is to add the line Option Strict Off to the top of any file that does that kind of thing. When Option Strict is turned off, VB.NET will perform the same kind of late-binding operation as VB6. If, at runtime, it discovers that the member name is invalid, it will throw an exception, but as long as the member exists and has the expected signature, it will work. This is typically bad-practice, in my opinion, but in a situation like this, sometimes it can be warranted.

As a side-note, what it's actually doing when you have Option Strict Off is it is using reflection to find the member by the specified name. Technically, you could use reflection manually, yourself, but it requires more code to do the same thing, which is precisely what you seem to be trying to avoid.

If you don't want to do that, you can use CType or DirectCast to cast the object to the specific type. For instance:

Dim frm As System.Windows.Forms.Form
If x=y Then
    frm = New frm1
    DirectCast(frm, frm1).Variable1 = "VarA"
    DirectCast(frm, frm1).Variable2 = "VarB"
Else
    frm = New frm2
    DirectCast(frm, frm2).Variable3 = "VarC"
    DirectCast(frm, frm2).Variable4 = "VarD"
End If
frm.Variable5 = "VarE"

Or you could create a variable of the correct type like this:

Dim frm As System.Windows.Forms.Form
If x=y Then
    Dim f As New frm1
    f.Variable1 = "VarA"
    f.Variable2 = "VarB"
    frm = f
Else
    Dim f As New frm2
    f.Variable3 = "VarC"
    f.Variable4 = "VarD"
    frm = f
End If
frm.Variable5 = "VarE"
Sign up to request clarification or add additional context in comments.

2 Comments

That gets me a bit closer, thank you. It works within the If statements now, but for anywhere that it's used outside of them, it still doesn't have the correct form assignment. But this has definitely given me something to go with, thank you. Didn't even consider using a DirectCast
I managed to tweak it slightly to get it to work past the If statements by setting it to initially be set to be frm1, so it still finds the values and passes the compiler. Thanks for your help, Steven, it's very much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.