Object Casting - TryCast Statement And Base Class

Jun 17, 2009

Here is a test program I wrote to explore a facet of object casting ...
Module Module1
Sub Main()
Dim class1 As MyBaseClass = New MyBaseClass()
Dim class2 As MyInheritedClass = New MyInheritedClass()
Dim class3 As MyBaseClass = Nothing
[Code] .....
The problem comes with the TryCast statement on class3. My intention is that class3 remains as a baseclass, but when the TryCast is applied, class3 morphs itself into an Inherited class which is not what I wanted. So is there a gap in my OOP/VB.NET knowledge and this action is legitimate or should class3 still be regarded as a base class?

View 6 Replies


ADVERTISEMENT

.net - Casting DataTypes With DirectCast, CType, TryCast?

Apr 24, 2010

Ever since I moved from VB6 to VB.NET somewhere in 2005, I've been using CType to do casting from one data type to another. I do this because it is simply faster to type, used to exist in VB6 and I do not know why I have to be using DirectCast if there is apparently no difference between them.I use TryCast once in a while because I understand that sometimes casting can fail. I however cannot get the difference between CType and DirectCast.

View 3 Replies

Flattening An Object Hierarchy Using A Shim Class - Initializing Derived Object Properties From The Base Object?

Jul 10, 2010

I am somewhat new to object oriented programming and am attempting to flatten a Linq object hierarchy by using a shim class.how to initalize a derived class with property values from a base class?I have two objects, a base object with about 100 properties, and a derived object which inherits from the base object and adds a few additional properties beyond those of the base object. My simple constructor creates the derived object, but I am looking for a way to initialize the derived object properties with values from the base object.Right now I am using reflection to iterate over the properties individually and I suspect there may be a better way. The following example shows my shim class constructor for the derived class, and two properties:

newProperty1 - a new string property of the derived class

flattenedProperty2 - a new string property of the derived class, copied from a 2nd-level object of the base class

Code example:

Public Class derivedObj
Inherits baseObj
Private _newProperty1 As String[code].......

Is this the correct constructor approach to flatten the object hierarchy using a shim class? My second question relates to initialization of properties in the derived class. The constructor above creates the derived object, but what is the best way to initialize the derived object properties with values from the base object? The following code uses reflection to iterate over the properties individually, but I suspect there may be a better way.

Code example:

' property names are in the string array fieldNames

'baseObjQuery is an ienumerable of baseObj

'derivedObjList is a list of derivedObj[code].....

Is there a simple way to initialize values for the properties in the derived object based upon the values of the common properties in the base object?

View 7 Replies

Get Base Class Object Reference Outside The Class's Definition?

Dec 10, 2010

If we are within the derived class then ofcource we can use MyBase keyword to access base class's object reference . That's fine , how can we take that base class's object reference outside that derived class's definition.My following code will explain what i want . Actually that is giving error right now, but it is explaining my requirement .

Public Class Base
Public x As String
End Class

[code]....

Actually there is error in ReadOnly Property BaseReference's Getter . Error is "Error 1 'MyBase' must be followed by '.' and an identifier. " how can i get base class object reference in Main method ?

View 1 Replies

Get Base Class Object Reference Outside It's Definition?

Feb 13, 2012

If we are within the derived class then ofcource we can use MyBase keyword to access base class's object reference . That's fine , how can we take that base class's object reference outside that derived class's definition.

My following code will explain what i want . Actually that is giving error right now, but it is explaining my requirement .[code]...

View 6 Replies

"upgrade" An Object To An Inherited Class, And Keep All The Base Class's Property Values?

Jul 5, 2010

bare with me during this silly example. :-) Let's say I have a class like this:

Public Class Animal
Public iLegs as int32 ' Number of legs
Public bWings as Boolean ' Wings? Yes/No
End Class

And another class that inherits the "Animal" class and adds one more property:

Public Class ScaryAnimal
Inherits Animal
Public iScariness as int32 ' 0-100, how scary is it?
End Class

Now, if I have an instance of "Animal" with some values in it, and I decide I want to cast it to a ScaryAnimal for some reason, how do I do that without having to create a new instance of "ScaryAnimal" and copy each property value?Basically I'm looking for a way to do this, without having to write the lines marked with '*** below:

[Code]...

View 2 Replies

VS 2008 Casting Subclasses, Base Classes?

Jun 26, 2010

I am a VB6 programmer learning VB.Net and am having trouble with the following concept.

[Code]...

View 4 Replies

Deep Clone Of Object With Base Class System.Windows.Forms.Control

Apr 18, 2012

I have an object (a third component gridview) which is serializable, and I need a deep clone of this object. I have the following code which throws the exception."The type System.Windows.Forms.Control in assembly System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKey Token=b77a5c5 61934e089 is not serializable".So the gridview's base class is System.Windows.Forms.Control, which is not serializable. How can I get a deep clone from the gridview? [code]

View 5 Replies

Error - Base Class 'System.Windows.Forms.Panel' Specified For Class 'MenuButton' Cannot Be Different From The Base Class 'System.Windows.Forms.UserControl'

Mar 12, 2010

When I do this

Public Class cInherits : Inherits Panel

I get this: Base class 'System.Windows.Forms.Panel' specified for class 'MenuButton' cannot be different from the base class 'System.Windows.Forms.UserControl' of one of its other partial types.

How do I inherit?

View 4 Replies

Form Inheritance - Error1Base Class 'MenuStrip' Specified For Class 'Lesson2' Cannot Be Different From The Base Class

Apr 15, 2010

I have put this code in the global form Inherits System.Windows.Forms.Form. And then in the form that will inherit this from the global Inherits MenuStrip. "MenuStrip" is what the global form is called. But keep getting this error: Error1Base class 'MenuStrip' specified for class 'Lesson2' cannot be different from the base class 'System.Windows.Forms.Form' of one of its other partial types.

View 5 Replies

Casting From Base Type To Derived Type

Jun 29, 2010

I'm wrestling with this treeview control. I have extended the TreeNode class to attach some extra data to the node. Basically, I added one property:[code]Is there a way to cast from a base type to a derived type?

View 11 Replies

Casting An Object Through An Interface (.NET 3.5)?

May 14, 2010

I have an object that inherits from an interface. I can create and return the following:

Dim o As IRequest
o = New Request
Return o

This works fine. Now I want to return a List(Of Requests), so I try the following:

Dim o As List(Of IRequest)
o = New List(Of Request)
Return o

The error below is being given on o = New List(Of Request)

System.Collections.Generic.List(Of MyObjects.Request)' cannot be converted to System.Collections.Generic.List(Of MyInterfaces.IRequest)'.Consider using 'System.Collections.Generic.IEnumerable(Of MyInterfaces.IRequest)'

View 2 Replies

Instance Of Object Won't Go Away Even After Casting

Feb 28, 2012

i have the following code which walks through an Excel file and this works fine note that the following line does work well (folder is being set earlier in the code)

[Code]...

View 1 Replies

VS 2008 Casting An Object?

Sep 30, 2009

I've got a project called Service.Inside the project, i've got 2 windows classes.ClassA is the main windows class which opens up when I run the program.On ClassA, I've got a button which, when I click it, opens up ClassB which is the other windows class. Inside the button's eventhandler (click), I've got a string which contains the name of ClassB's class ie "ClassB". I will probably get this from a table. Is it possible to initiate the object ClassB without using if statments to check what the type of object is. In the code below, i've managed to create a o of type object, but i need to new it with the ClassB

vb.net Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

[Code]...

View 7 Replies

VS 2008 Object Inheritance And Casting

Mar 23, 2011

I have two custom objects.On of them inherits the other one, appends few more properties and methods, ovverides some other.When I try to cast the Parnet Object to Child I receive an error that it could`nt be done.

View 1 Replies

Create A New Class That Inherits From The Base Form Class And Define A New() Method With Parameters?

Oct 1, 2008

I've been creating short test apps repeatedly to try to understand some of the concepts in VB.NET.For the most part it has been illuminating.I read Bucky's .NET knowlegebase tutorial on passing objects as parameters to newly created forms. He shows how to create a new class that inherits from the base form class and define a New() method with parameters Extending the concept I thought about doing the same thing with a form that was created at design-time (In this case Form2).

[Code]...

View 6 Replies

Call Method Base Class In Program Passing Sub Class Objects?

Apr 22, 2012

Base class has one field to hold numeric balance value. With 2 methods that accept arguments for adding and subtracting the new input calculating new balance. Sub class has four fields dates, transaction, memo and amount.I have a deposit form, and withdraw form. Each time one transaction is entered it creates an object with sub class fields, then adds to the account collection. My problem is not understanding how to call the deposit/withdraw method and pass the current transaction amount back to the base class to alculate the new balance. Does anyone have any links to information/tutorials on how to perform something like this? As you can see with my code I have tried various different approaches without any success.

[Code]...

View 5 Replies

Casting From Generic IList To Object During DataBind?

Feb 18, 2010

Okay, so I have a Generic IList of custom objects that I want to use as a datasource for a repeater.

Dim productRows As IList(Of MyCustomObject)()
'fill list with data
rptResults.DataSource = productRows

[code]......

View 1 Replies

.net - Suppress A Property Or Method From A Base-class In A Derived Class?

Apr 7, 2011

Supose a base class

Public Class airplane
Private var_num_seats As Integer
Private var_num_engines As Integer

[code]....

Obviously, I don't wish that the class Glider has the method "start_engines" neither the property "num_engines". Otherwise, other child classes may have. How can I supress these property and method in child class, not just ignoring (if I can)?

View 2 Replies

.NET Class Inherits A Base Class And Implements An Interface (works In C#)?

Apr 9, 2010

I am trying to create a class in VB.NET which inherits a base abstract class and also implements an interface. The interface declares a string property called Description. The base class contains a string property called Description. The main class inherits the base class and implements the interface. The existence of the Description property in the base class fulfills the interface requirements. This works fine in C# but causes issues in VB.NET.

[Code]...

View 5 Replies

Can't Inherit Shared Properties From A Base Class In A Child Class?

Dec 29, 2010

This is another one of my "I think it's not possible but I need confirmation" questions.I have a base class for a bunch of child classes. Right now, this base class has a few common properties the children use, like Name. The base is an abstract class (MustInherit)Technically, this means that everytime a child class is instantiated, it lugs around, in memory, its own copy of Name. The thing is, Name is going to be a fixed value for all instances of a given child. I.e., Child1.Name will return "child_object1", and Child2.Name will return "child_object2".

View 1 Replies

Does An Inherited Class Automatically Implement An Interface From Its Base Class

Jun 10, 2011

Suppose I have piece of code like this:

Public Interface ISomething
....
End Interface

[code]....

View 2 Replies

Returning A Inherited Class As Base Class Type With Web Services?

Sep 25, 2009

I suspect I am being very silly here but I have the following setup

Class MustInherit myBaseClass
'some stuff
End Class
Class myInheritedClassA

[code]....

Running this results in the following error

Unhandled Exception: System.InvalidOperationException:
There was an error generating the XML document. --->
System.InvalidOperationException:

[code]....

is there any way of 'Widening' the inherited class to the base class so this would work?

EDIT:regarding XmlInclude(typeof inheritedClass), currently this method could potentially return a number of types of inherited class (i.e myInheritedClassA and myInheritedClassB) is it case of simply having to add each of the inheritedTypes in this tag?

View 2 Replies

Access The Base Class Inside A Derived Class?

Oct 22, 2010

How do I access the base class inside a derived class?

View 1 Replies

VS 2008 Object Casting Error When Clicking On A Web Browser Bookmark?

May 31, 2010

Unable to cast object of type 'System.Windows.Forms.ToolStripButton' to type 'System.Windows.Forms.ToolStripMenuItem'.This is the error, when I click on the bookmark(When I add the bookmark, It's a non-clickable menu item; the next time i debug it, it becomes a clickable url(button)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
...
Call minginimi()
End Sub

[code]....

View 2 Replies

Base Class Collection With Sub Class Objects

Jul 19, 2010

[code]The reason that this is a problem is that this is a collection of EventBase objects, but I'm trying to populate it with child classes instead. For example, I might have a JumpEvent class that inherits EventBase, and this is being put into the EventBaseCollection. That means that value.GetType() returns the type of JumpEvent, which as you might guess does not equal the type of EventBase.The goal of course is to simply loop through all of the various events without having to know anything about the sub-classes. Is there a way to determine the type of the base class so that the OnValidate call will work? Or is this just the wrong way to go about it altogether?

View 2 Replies

Convert From Base Class To Inherited Class?

Jun 2, 2009

I understand that inherited classes can't be converted from base classes. But is there any way to write a conversion sub, so that the base class gets assigned to MyBase, then extra properties are added?[code]...

View 7 Replies

Utilizing A Centralized Class object That Needs To Call Another Class Object?

Jun 7, 2010

I am utilizing a centralized class object that needs to call another class object. The program itself will do something like the following

sFieldValue = CentralObject.ObjectHandler(1, TestFunction, "FunctionVar,FunctionVar2,FunctionVar3")

In the central object I have a function

Public
Function ObjectHandler(ByVal ObjectType
As
Integer,

[code]....

View 5 Replies

[02/03] Base Class Called PageBase And Then A Derived Class TestPage That Inherits From PageBase

Feb 4, 2009

I have a base class called PageBase and then a derived class TestPage that inherits from PageBase. In PageBase I have overridden the OnInit() and when it is called and I inspect the value of 'Me', it is of the type of the derived class and not the base class. My question is how does inheritance really work? For instance, when instantiating TestPage does an instance of PageBase get created too?

Here is my code;

Partial Public Class TestPage
Inherits PageBase

Public Sub New()

[code]...

View 7 Replies

What Is The Base Class Of .NET?

Oct 19, 2009

What is the base class of .NET?

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved