Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# – Change value inside a class from a form

Hey I have the following two classes:

public class YoloCocoP7Model : YoloModel
{
    public override int Width { get; set; } = 640;
    public override int Height { get; set; } = 640;

    public YoloCocoP7Model()
    {
    }
}

public class YoloScorer<T> : IDisposable where T : YoloModel
{
    public YoloScorer(string weights, SessionOptions opts = null) : this()
    {
        _inferenceSession = new InferenceSession(File.ReadAllBytes(weights), opts ?? new SessionOptions());
    }
}

Now I can call a function like this:

 public YoloScorer<YoloCocoP7Model> _scorer;

I want to change the Width and Height inside the YoloCocoP7Model class and Initialize it to the YoloScorer class and tried it the following way:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var test = new YoloCocoP7Model();
test.Width = 10;
test.Height = 10;

This works but however If I want to use that changed class then with:

var _scorer = new YoloScorer<test>;

I get an error saying "test" is a "variable" but is used like "Type".

How can I use the changed Class then?

>Solution :

The YoloScorer<T> in your class definition is a generic class, meaning it can work for different types.

You can now implement methods with that type like public T GetNewObject() or public string[] GetAllPropertyNames<T>() that use that Type T.

The type is, however, not the object itself, it’s the type of object. In your case, the type is YoloCocoP7Model. The type has no instance.

If you want to give your class YoloScorer a object YoloCocoP7Model, you need to declare a member of that type and add it, i.e. via constructor:

public class YoloScorer : IDisposable
{
    public YoloModel Model {get; set;};

    public YoloScorer(YoloModel model) : this()
    {
        Model = model;
    }
}

Then, you can modify it by calling

var _scorer = new YoloScorer<test>;
_scorer.Model.Width = 1337;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading