I'm getting confused with constants problems which I'm facing during trial of learning Selenium in C#.
First of all, each class which I'm creating inherits a class which sets a new driver (BaseClassTest):
public class BaseApplicationPage
{
protected IWebDriver Driver { get; set; }
public BaseApplicationPage(IWebDriver driver)
{
Driver = driver;
}
}
Next, one of my "main" class (HomePage) starts from inheriting elements from "BaseApplicationPage" and later creates constructor which (in most cases) has empty body. However in that case, inside the body there is a line which: creates a new "Slider" class.
internal class HomePage : BaseApplicationPage
{
public HomePage(IWebDriver driver) : base(driver)
{
Slider = new Slider(driver);
}
public Slider Slider { get; internal set; }
My questions:
Is it necessary to fill all new class with something like it (constructor + inheriting from BaseClass)?
Why in my case inside the body there is reference to slider class rather than leaving it empty and adding something like this:
public SliderSection Slider => new SliderSection(Driver);
Answer 1: Is it necessary to fill all new class with something like it (constructor + inheriting from BaseClass)? -- If you need driver object in any particular class like one you've defined(HomePage) , you need a constructor to initialise driver object. Then only you can use driver reference anywhere in that particular class.
Answer2 :
You can use both
public SliderSection Slider => new SliderSection(Driver);
and Slider = new Slider(driver); provided here, Slider type must be defined in this class or it's base class.
Related
So im trying to use compToGet string that have been passed through the parameter into slot.GetComponent().level++;
upgradeFoundation() will be called on button click.
and there is actually quite a lot of buttons with similar functionality (like: upgradeTurret(), upgradeTurret2(), etc)
thats why im trying to change the value of compToget string base on which button you click and use that new string to get component under the name of that new string but it seems it doesn't work that way and I dont know how it would work any other way, any help would be much appreciate.
public void upgradeFoundation()
{
float upgFoundationCost = slotGroup.transform.Find(slotName).gameObject.GetComponent<Slot>().upgFoundationCost;
Upgrade(upgFoundationCost, "Foundation");
}
public void Upgrade(float upgCost, string compToGet)
{
GameObject slot = slotGroup.transform.Find(slotName).gameObject;
if (inGameUIManagerScript.money >= upgCost)
{
Type compToGetType = Type.GetType(compToGet); //im not sure how to convert a string into a type
slot.GetComponent<compToGetType>().level++; //this is the error line saying im treating a var like a type
}
}
Thank you in advance.
Exactly the same issue as in your previous question => You can not use the generic! Instead use GetComponent(compToGetType);
However I removed the duplicate since you still would need to cast to your actual type which is anything but trivial!
=> Again I can only recommend: Don't use strings!
Rather have a common Base class or interface like e.g.
public abstract class BaseComponent : MonoBehaviour
{
private int level;
// public read-only access
public int Level => level;
public virtual void Upgrade()
{
level++;
}
// Other properties and methods all your components have in common
// Also get into "virtual" and "abstract" members!
}
and inherit your stuff from it like
public class Foundation : BaseComponent
{
// Additional stuff specific to the foundation
// overrides for the virtual and abstract members
}
public class Turret : BaseComponent
{
// Additional stuff specific to the turret
// overrides for the virtual and abstract members
}
//Maybe this would even inherit from Turret instead?
public class Turret2 : BaseComponent
{
// Additional stuff specific to the turret2
// overrides for the virtual and abstract members
}
and finally use that common base instead:
public void UpgradeComponent()
{
slot.GetComponent<BaseComponent>().Upgrade();
}
When you create new class and mark it as [System.Serializable] your inspector will create and show its default object for property of new class' type in your MonoBehaviour component.
When creating custom PropertyDrawer though you need to create this default object on your own and put its reference into SerializedProperty.objectReferenceValue (as far as I understand).
But this field is of type UnityEngine.Object and my new class cant be assigned there. How to overcome it? Inheriting your class from UnityEngine.Object doesnt help as SerializedProperty.objectReferenceValue is still null, even after assigning in there the newly created object (which is actually of the same type – UnityEngine.Object).
I hope I understood your question correctly, taken from the Unity documentation:
using UnityEngine;
public enum IngredientUnit { Spoon, Cup, Bowl, Piece }
// Custom serializable class
[Serializable]
public class Ingredient
{
public string name;
public int amount = 1;
public IngredientUnit unit;
}
public class Recipe : MonoBehaviour
{
public Ingredient potionResult;
public Ingredient[] potionIngredients;
}
[CustomPropertyDrawer(typeof(Ingredient))]
public class IngredientDrawerUIE : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
// Create property container element.
var container = new VisualElement();
// Create property fields.
var amountField = new PropertyField(property.FindPropertyRelative("amount"));
var unitField = new PropertyField(property.FindPropertyRelative("unit"));
var nameField = new PropertyField(property.FindPropertyRelative("name"), "Fancy Name");
// Add fields to the container.
container.Add(amountField);
container.Add(unitField);
container.Add(nameField);
return container;
}
}
So when you view a GameObject with the Recipe component on it, Unity's inspector will show something like this:
So you do not need to inherit from anything, simply mark the class you want to create a property drawer as Serializable, and create a property drawer class for it (Make sure to place it in the Editor folder, or create a assembly definition file which targets the editor only if you are working with assembly definition files).
I have a C# Windows Phone 8 app that for phones that have it, uses the Compass sensor for a particular feature. I want to modify the class so that it can work on phones that don't have a compass sensor. I don't want to have to wrap every call to the Compass object in if (_compass != null) statements. I thought I could create a dummy Compass class with stubs for the Compass methods and properties, and create an instance of it instead of Compass if the phone doesn't have a compass, but I'm running into a problem with the abstract base class for the Compass class called SensorBase. No matter what I do, I get the compile time error:
Error 1 The type 'Microsoft.Devices.Sensors.SensorBase<TSensorReading>' has no constructors defined
The Compass class I'm trying to emulate can inherit from SensorBase without getting this error. I grabbed the exact declaration line for the Compass class, from the Sensors metadata file, and plugged it into my dummy class after changing the class name to CompassDummy, and I still get the error.
My guess is that the Compass class can inherit from SensorBase while my dummy class can't because it is in the same Assembly as SensorBase. Perhaps the constructor for SensorBase is marked internal or there is some other similar "friend" like relationship involved.
My question is, is there anything I can do in this situation to inherit from SensorBase without getting this error? If not, is there another solution involving dependency injection (IOC) that would help achieve my goal of instantiating a dummy class when the phone doesn't have a compass?
Here is my code for the CompassDummy class that is getting the error:
public sealed class CompassDummy : SensorBase<CompassReading>
{
public CompassDummy()
{
}
} // public class CompassDummy
My guess is that the Compass class can inherit from SensorBase while my dummy class can't because it is in the same Assembly as SensorBase. Perhaps the constructor for SensorBase is marked internal
Correct. You're not supposed to derive from SensorBase<T> yourself: it's just the base class for Accelerometer, Compass, Gyroscope, and Motion.
Are you planning to just use this dummy Sensor class within your own code, and not pass it anywhere? If so, use composition rather than inheritance here:
public interface ICompass
{
void Start();
// Whatever other methods you need
}
public class RealCompass : ICompass
{
private readonly Compass compass;
public RealCompass(Compass compass)
{
this.compass = compass;
}
public void Start()
{
this.compass.Start();
}
}
public class StubCompass : ICompass
{
public void Start()
{
// Do nothing...
}
}
Then, in your code, use your ICompass interface.
Using an interface to allow substitution of the concrete compass type, as suggested by canton7, is a good solution to this problem. Another solution to consider, depending on your needs, is the proxy pattern.
Basically, you'd create a CompassProxy class that you'd use throughout your code. This class would have similar or identical methods/properties to the framework Compass class, and each instance of it would contain a corresponding instance of either Compass or CompassDummy, depending on the phone's hardware.
Calls to the proxy class would be forwarded to the contained "back-end" compass class, which would do the real work and any result would then be passed back through to the caller.
Just as an illustration, here is some prototype code for a CompassProxy class:
class CompassProxy {
private readonly Compass _realCompass = null;
private readonly CompassDummy _dummyCompass = null;
private readonly bool _hasCompass = false;
public CompassProxy() {
// the creation logic could be moved out of this class, if need be
if ( HasRealCompassHardware() ) {
_realCompass = Compass.GetDefault(); // ...or whatever is the proper way to obtain an instance
_hasCompass = true;
} else {
_dummyCompass = new CompassDummy();
}
}
public CompassReading GetCurrentReading() {
return _hasCompass ? _realCompass.GetCurrentReading() : _dummyCompass.GetCurrentReading();
}
}
I have a game project built upon Cocos2D XNA and MonoGame. I wanted to add a little bit of custom logic into CCSprite class, so I created a class which inherits from CCSprite. I added a dummy auto property and tried to use this class, but for some reason sprites being created as instances of my custom sprite class are not shown on the layer, while sprites which are instances of CCSprite class - are completely OK.
Code looks like this:
public class Sprite: CCSprite {
public string SomeProp {get; set;}
}
...
line1: var mySprite1 = new Sprite("texture.png");
line2: var mySprite1 = new CCSprite("texture.png");
AddChild(mySprite1);
If I use line1 and comment out line 2, then mySprite 1 is not shown. Otherwise - if mySprite is an instance of CCSprite - it works well.
What could be the source of this problem?
You are not calling the constructor of the CCsprite with your own Sprite class.
Sprite:CCSprite{
public Sprite():base()
{
//blabla
}
}
the base() is calling the constructor of CCSprite the class you are inheriting
if you want to pass through parameters then do something like this:
Sprite:CCSprite{
public Sprite(string imgpath):base(imgpath)
{
//blabla
}
}
Now I've passed a string through the contructors.
If I have a class that is based off another class, how do I access the properties of the first class if it can have any name? I was thinking of using generics to access the properties, but the generics are "generic" for a reason...
For example:
public class AGameInXNA : Microsoft.Xna.Framework.Game
{
int ExampleGameProperty;
}
// ... another class ... //
public class ReferenceToAGameInXNA
{
Game gameInstance;
public void SetGameInstance(Game game)
{
gameInstance = game;
}
public void SetExampleGameProperty()
{
gameInstance.ExampleGameProperty = 21; // I don't know the name of
// AGameInXNA, so I want to
// access it using a generic
// class.
}
}
I know that that does not work, so how would I use generics in this case to access the AGameInXNA's properties in another class if I don't know AGameInXNA's name?
EDIT: I am trying to make it so that I can reuse this code later on. I want to be able to have a class that is unknown, such as public class unknownclassname that extends another class, such as Microsoft.Xna.Framework.Game, and be able to access the class unknownclassname without directly calling/implementing it in the library code.
I would recommend looking into XNA Services.
So for example, you would create a service which could be as simple as an
interface IExamplePropertyService
{
int ExampleProperty { get; set; }
}
public class AGameInXNA : Microsoft.Xna.Framework.Game, IExamplePropertyService
{
int ExampleGameProperty { get; set; }
void Initialize()
{
// Do other initialization
Services.Add( typeof(IExamplePropertyService), this );
}
}
public class ReferenceToAGameInXNA
{
IExamplePropertyService propertyService;
public void GetGameInstance(Game game)
{
propertyService = (IExamplePropertyService)game.GetService( typeof(IExamplePropertyService) );
}
public void SetExampleGameProperty()
{
propertyService.ExampleGameProperty = 21;
}
}
Implement it, and register it with the Game component, then in your ReferenceToAGameInXNA, you would query for this service and store it (rather than the Game) for use later.
As a bonus benefit, The IExamplePropertyService no longer even needs to be implemented by the Game class, it could be implemented by any GameComponent.
This makes for an easy way to seperate classes from having to know about the inner workings of other classes in the Game. So long as the services exist somewhere, your ReferenceToAGameInXNA can be used.
I don't think generics are what you are actually looking for here. In your second class, just change the type of all of the gameInstance to the type of the class you created for your game, in this case AGameInXNA. There should only be a need for one subclass of the Game type in each XNA game. That will allow you to access any public members of AGameInXNA from the Reference class.
If this isn't what you are after, please give a more detailed explanation of what you are trying to accomplish and I'll try to help you.
I don't know XNA, but if you want to have several classes that inherit from Game and have the same property on all of them, you could create an abstract class that inherits from Game and let the other classes inherit from that instead.
(Also, your GetGameInstance() is badly named, because it sets the field, it doesn't get it. And it's probably better as property anyway.)
public abstract class GameBase : Microsoft.Xna.Framework.Game
{
public int ExampleGameProperty { get; set; }
}
public class AGameInXNA : GameBase
{
// code specific to AGameInXNA
}
public class ReferenceToAGameInXNA
{
public GameBase GameInstance { get; set; }
public void SetExampleGameProperty()
{
GameInstance.ExampleGameProperty = 21;
}
}
If the other classed that have ExampleGameProperty shouldn't inherit from Game, you could create an interface instead. AGameInXNA would then inherit from Game directly and it would also implement the interface. And you would work with that interface in ReferenceToAGameInXNA.
using "Game gameInstance;" you can not acess ExmpleProp. You should use "AGameInXNA gameInstance;" too access ExampleProp.