1

in my program pre-project, I have a first form. In this form, there are three radio buttons. The radiobttns say "option 1", "option 2", "option 3". Then I pick one of them. Based on my decision one object will be instantiated: if I choose "option 1", the object will be from class "option1", if I choose "option2", it will be from class "option2" and so on... At the same time, one WinForm will appear based on that decision too, if I choose "option 1" will be "WinForm1", if I choose "option 2" will be "WinForm2" and so on...

The idea on this second WinForm (the first was that with the radiobuttons) is to make the fields for filling the properties of the choosen object appear. After filling it all, I perform some calculations and do other stuff.

My question is: How do I instantiate the object I am gonna use (basically, I choose dynamically the object I am gonna use) ? Do I put it inside the "Load Event" of the Form ? If I do that I can´t work on the object on different events. Do I initialize it on each form ? Which is the better approach ?

Remembering, this is just and idea, there is no code yet. If you don´t understand, please let me now.

Thanks,

Ricardo S.

  • VBA does not support class polymorphism. If you want to do some inheritance then see [**`this`**](http://stackoverflow.com/questions/19373081/how-to-use-the-implements-in-excel-vba/19379641#19379641) –  Nov 06 '13 at 21:48
  • Lol, it is not VBA, I have written VB and the tag VBA poped and I haven´t realised. And it´s VB .NET. By the way, the problem isn´t to implement the polymorphism, the problem is how to instantiate the objects. Thanks, anyway. –  Nov 06 '13 at 21:54
  • Hi, Derek. What makes a car different from the other is also the size. For example, the large_car has a "buffer" - I don´t know how to say this in english, so I used GoogleTradutor, I think "buffer" is correct - whilst the other options don´t have. So, there will be a textbox for the dimensions of the buffer (height, material) while the other types of car won´t have. That´s why I use polymorphism for the objects and the interface. –  Nov 06 '13 at 22:00
  • It´s bumper or fender. The word in my language is "para-choques" (português). –  Nov 06 '13 at 22:31
  • Sorry - I just realized I might be getting us off-topic, since it looks like you were just using cars as an example for your problem. Are you just not sure of the syntax to instantiate a class in VB? Or how to know which exact subclass to instantiate? – Derek Nov 06 '13 at 22:43
  • Hi, Derek, thanks for answering. I know all cars should have a fender, but I think you are missing the point. I am just pre-designing and giving aleatory examples of what I want to do. The first division (small, medium, large) isn´t what I am actually doing and I just thought - as I said before I don´t have a code yet - it would be easier to explain this way. –  Nov 06 '13 at 22:46
  • Ok. I am gonna tell you in the next comment. –  Nov 06 '13 at 22:51
  • I have a first form. In this form there are three radio buttons. Each radio says "small", "medium", "large". Then I pick one of them. Based on my decision one of the objects will instantiate and the proper interface will appear. With the entries of this interface I am gonna fill the properties of my object, then perform the calculations and stuff. –  Nov 06 '13 at 22:54
  • The problem is: do I have to instantiate one object on the form ? If my forms are named: interface_small, interface_medium, interface_large. The best option is to instantiate one object of that kind in the proper form that I have firstly initialized ? <- That´s my answera and I don´t know if it is correct. I haven´t said nothing before to don´t spoil the answers. Done with comments for a while. lol –  Nov 06 '13 at 22:59
  • 1
    well this question is completely off topic now. voting to close –  Nov 06 '13 at 23:23
  • ???? The question is a example. The comments are just an explication because people were apparently misunderstunding what I have asked. I can change my post if you want to. If you look to what I have asked on the last comment: "The problem is: do I have to instantiate one object on the form ?" and on the topic: "My problem is: how do I initialize the object ?" they are the same. –  Nov 06 '13 at 23:30
  • Ricardo, you may have luck getting an answer if you edit your question and rephrase it to say your two comments immediately above your most recent one. Leave the idea of a car out, since that obviously confused us. And also be careful of your use of the word "interface", since it sounds like you are referring to the user interface in your comments, but a [Visual Basic interface](http://msdn.microsoft.com/en-us/library/vstudio/28e2e18x%28v=vs.110%29.aspx) means something entirely different for inheritance. Also mention what forms you are using (ASP.NET? WinForms? WPF?). – Derek Nov 07 '13 at 00:07
  • Ok. I have used the word "inteface" basically because that´s the exact word we use in portuguese, I know the use of "interface" on VB and you are right on telling me to change it - sometimes it´s harsh to deal with concepts in different languages. I am gonna change the first post too, but I want to make a stand that I disagree on the fact that it should be changed cause what has gotten people confused , if it is paid attention, was the fact that they were trying to fix the project, not to answerthe question. Even if there was a simpler way to do it, I wanted to know the answerto that approach. –  Nov 07 '13 at 00:27
  • Any further discussion should be moved to chat. – Sam Axe Nov 07 '13 at 02:45

1 Answers1

0

The simple way to do this is to instantiate the objects based on the option value, like so for example:

Public Sub optionsCheckedChangedHandler(sender as object, e as eventargs) Handles option1.CheckedChanged, option2.CheckedChanged, option3.CheckedChanged

  Dim item as Object = Nothing
  Dim form as Form = Nothing

  If sender is option1 Then
    item = New OptionItemOne
    form = New FormOne With {.OptionItem = item}
  End If

  If sender is option2 Then
    item = New OptionItemTwo
    form = New FormTwo With {.OptionItem = item}
  End If

  If Not Nothing Is form Then
    If DialogResult.OK = form.ShowDialog(Me)
      '  Do something with the "item" if you want to.
    End If
  End If

End Sub

You can add levels of complexity as you identify the need.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • I am still thinking in how I am gonna do other stuff of the program, but as soon as I make a try I evaluate your answer here. I don´t know yet how .OptionItem works and I am gonna look for it. Thanks for your time. Edit: Is this for VB .NET ? I have googled it and haven´t found consistent stuff about it - OptionItem. –  Nov 07 '13 at 09:24
  • @Ricardoytu: `.OptionItem` is a form-level variable or property of type Object, or perhaps an interface or base class type. It is meant as a way of conveying data to your form. – Sam Axe Nov 24 '13 at 16:02