Home > Software engineering >  About the class to create a problem
About the class to create a problem

Time:10-09

I created a virtual class
Public MustInherit Class A
.
The End of the Class
Then create two classes inherit it
Public MustInherit Class B: Inherits A
Public AA As Single
The End of the Class
Public MustInherit Class C: Inherits A
Public BB As Single
The End of the Class
Now I want to use the Select a Case statement in the main program in the main program to create an object Obj
Sub Main ()
Select the I
Case 1
Dim Obj As New B
Case 2
Dim Obj As New B
End the Select
Obj. AA=123
Obj. BB=234
End Sub
Because I sent you a confirmation in the main program to create objects belong to which class, so the above code can not be achieved, I don't know if there is any good way to create an object only Obj, under certain conditions for the function of the function of class B or C,
I don't want to create the object of the class B and class C at the same time, because in the sample can save memory space, I know that I understand right,
Thank you teach,




CodePudding user response:

As for your scene, using inheritance entirely superfluous action,

CodePudding user response:

So, I sometimes need to use A class B and class C joint method, I can write this method in A class, so that it can reduce the rewrite of the code,

CodePudding user response:

You have nothing to do with whether inheritance,

CodePudding user response:

Why it doesn't matter? I just want to create an object that may need to B class or C class's personality, at the same time, need to class A common, don't need to use inheritance?
Just after creating object Obj, there is no way to use B class or C class personality methods or fields, do not know to have good method to solve,
I saw several design patterns to create form, but all seem to have no a way to solve my problem,

CodePudding user response:

I mean, you need to give different attribute assignment, this process has nothing to do with inheritance,

Although they have inheritance relationship, but here, for your needs, inherit to the not is not important,

Extremely, any class derived from the Object type, but we must always consider this not to write programs,

CodePudding user response:

If you must have been simplified, then you can consider to use reflection,

CodePudding user response:

I'm sorry, I'm a piece of code is a little problem, the main program should be like this:
 Sub Main () 
Select the I
Case 1
Dim Obj As New B
Case 2
Dim Obj As New C
End the Select
Obj. AA=123
Obj. BB=234
End Sub

Where
Obj. AA=123
Obj. BB=234
There will be a compiler error, is there any way to solve it

CodePudding user response:

You can define a Obj
Dim Obj As New A
Select the I
Case 1
Obj=New B
DirectCast (Obj, B). AA=123
.

CodePudding user response:

My question to be solved:
 Sub Main () 
Dim Obj As A
Select the I
Case 1
Obj=New B
DirectCast (Obj, B). AA=123
Case 2
Obj=New C
DirectCast (Obj, C). BB=234
End the Select

I first defines a base class variable Obj first, and then later create an object B or C, and assigned to the variable Obj, this should be late binding, but every time the need to control the object B or C field, or method, DirectCast or Ctype method must be used for type conversion, do not use DirectCast or Ctype method and other methods? thank you

CodePudding user response:

Reflection

Obj. GetType (). GetField (" AAA "). The SetValue (Obj, 123).

CodePudding user response:

Superclass type of pointer can point to subclass,
B, C class inherits from ray A
 dim Obj as A 
Select the I
Case 1
The Set Obj=New B
Case 2
The Set Obj=New C
End the Select
Obj. AA=123
Obj. BB=234
End Sub
  • Related