[2008] Different Methods in class and ways to access them

Bozzy

Active member
Joined
Aug 31, 2007
Messages
27
Programming Experience
Beginner
Hello,

I have a class in Visual Basic 2008 that is used in two ways.

The first way it can be used is if you use it to create an instance. In this case, I want it to be able to access certain methods that I have defined. These can be methods such as, new obviously, and SetRange.

The second way it can be used, is as a way to use general functions in it. Because this is a light, such a function might be SetAmbient etc...

Is there a way that I can hide the SetAmbient function when it is being called by an instance, but show it when it is simply been called as a general command.
VB.NET:
Light.SetAmbient

And then, show all the other methods that are available to be called from an instance:

VB.NET:
Dim Light1 as New Light
Light1.SetRange


Thanks,
Bozzy
 
Declare SetAmbient as a Shared method. Instance methods are hidden when you qualify the member only by its class name. Shared methods are not hidden from an instance, but if you try to use it it will give you a compiler warning that the instance is ignored.
 
Sorry, how do I declare an instance method?
I really didn't get what you said, sorry.

Thanks,
Bozzy
 
Instance methods are regular methods you have to create an instance of a class to use, Shared methods you have to add the "Shared" keyword to the declaration.
Sub method()
Shared Sub method()
 
Thanks.

Oh, and is there a way I can put methods into categories?

For Example:
VB.NET:
Dim L as New Light
L.Entity.Move(5)


Thanks,
Bozzy
 
Thanks.

Oh, and is there a way I can put methods into categories?

For Example:
VB.NET:
Dim L as New Light
L.Entity.Move(5)


Thanks,
Bozzy
No, and why would you want to? If the Light class has a property named Entity that returns an object whose type has a Move method then that code would be valid, but if Move is a member of the Light class then you call it on a Light instance.
 
Here is the diagram:



I want the Entity class methods to show up seperately from the Light class methods in the intellisense list, if that is at all possible.

Thanks,
Bozzy
 
It's not, at least not to the best of my knowledge, and I still don't see why you would want them to. If a Light IS an Entity then those methods inherited from the Entity class ARE members of the Light class. If you want to treat a Light object as an Entity object specifically then cast it as that type, then you'll only get Entity members. Otherwise you'll get all the members of the Light class, and so you should.

Given that the Form class inherits the ContainerControl class, which inherits the ScrollableControl class, which inherits the Control class, I'm sure glad it isn't done that way.
 
From the looks of your class diagram you might also want to learn about properties. Maybe it's appropriate that all you members are methods but given the number of methods whose names begin with Get and Set I'm guessing that at least some of them should be properties.
 
i guess there is a way, but I agree with the others, I don't see why you would want to.

If you must, you could create a nested class or struct, where you put all the variables in there and then create an instance of the nested class/ struct in the constructor and then access the instance of the nested class during a property.

so kind of like this...
VB.NET:
public class baseClass

private mEntity as EntityClass

public sub new()
    instanceNestedClass = new EntityClass
end sub

public readonly property Entity as EntityClass
get
    return Entity
end get
end property

public class EntityClass
    public Light as string
    ' list of properties etc.
end class

end class

In that manner when you have an instance of the baseClass, you can go
myInstance.Entity.Light to get the property.
 
Oh ok... you've won me over about the categories.

@jmcilhinney

I'd only previously used properties for variables, and I didn't know you could use them for methods. But actually now, it seems like a good idea. How could I allow the user to choose from more than one return value?

For example,
VB.NET:
Dim X as double = Light.Position.X
???

As well as this, do you think it will actually be useful in this way, because I guess you have to do:

VB.NET:
 light.Position = something

You wouldn't be able to do anything else would you?

You couldn't go:
VB.NET:
 light.Position.Set(x,y,z)
... could you?

Thanks,
Bozzy
 
Look at the Control class. It has a Bounds property that's type Rectangle, a Size property that's type Size, a Location property that's type Point and Top, Bottom, Left, Right, Width and Height properties that are type Integer. They are all related, some can be set while others are read-only, and they're all properties. You Position member should be a property and it should be of a type of your own creation, like Point3D. Then you can do this:
VB.NET:
light.Position = New Point3D(x, y, z)
You might also provide separate X, Y and Z properties that could be set individually, just as the Control class provides a Location property as well Top and Left properties.
 
How do I get it so that I have a Location property... then I can choose:

Location.X or Location.Y

I get I need seperate properties but how do I acess them in that sort of Hierachy
 
Like I said, look at the Control class. It has a Location property that is type Point. The Point type has properties X and Y, which are type Integer.

Your situation is quite different. Your Light class doesn't have a property that is type Entity, which then has all the Entity members. Your Light class inherits the Entity class, so a Light IS an Entity. Control doesn't inherit Point. It's completely different.
 
I'm really sorry, but I still don't get what you mean.

I have looked at the point class and understand about the structure of it. I am just wondering how I can get that structure myself. I'm talking about that Location property. Does it have sub-properties or what?
And how do I do stuff like in the Anchor property? (like methods etc)

I thought a property was just allowed Get/Set
 
Last edited:
Back
Top