Implicitly convertible to 'System.IDisposable' error
Asked Answered
C

10

16

This is what I'm trying to do:

private KinectAudioSource CreateAudioSource()
{
    var source = KinectSensor.KinectSensors[0].AudioSource;
    source.NoiseSuppression = _isNoiseSuppressionOn;
    source.AutomaticGainControlEnabled = _isAutomaticGainOn;
    return source;
}
private object lockObj = new object();
private void RecordKinectAudio()
{
    lock (lockObj)
    {
        using (var source = CreateAudioSource())
        {
        }
    }
}

The 'using' statement gives one error which states:

'Microsoft.Kinect.KinectAudioSource':type used in a using statement must be implicitly convertible to 'System.IDisposable'

How do I eliminate this error and what does it mean?

Cohere answered 12/2, 2014 at 5:38 Comment(0)
U
20

I am very late to the party, but I should say:

You must add a reference to Entity Framework if you get this error while using context inside using statement.

Underbred answered 29/6, 2016 at 18:41 Comment(3)
@Hits right click on the project and select nuget package manager. Search for Entity Framework and install it.Underbred
Already had reference to EF, I just updated my EF and that error gone. Thank you but I do not understand this solution.Nerynesbit
@Nerynesbit I don't remember anything about Entity Framework and .NET as I have left switched to Javascript and React.js since 2017. Sorry!Underbred
G
19

You can create like that :

public class HelloTest : IDisposable
{
    void IDisposable.Dispose()
    {

    }

    public void GetData()
    {

    }
}

after then you can able create object like

using (HelloTest Ht = new HelloTest())
        {
            Ht.GetData();
        }

I hope above example helpfull

Git answered 19/9, 2014 at 11:1 Comment(0)
O
5

I had a similar problem when creating a new project I had forgotten to install ENTITY FRAMEWORK via Nuget Package Manager. Sorry if this doesn't relate to kinect but this is where google took me when I was looking for the error in VS.

Oireachtas answered 6/10, 2015 at 17:53 Comment(0)
S
4

Using keyword required IDisposable interface implementation. If you are getting error 'Microsoft.Kinect.KinectAudioSource':type used in a using statement must be implicitly convertible to 'System.IDisposable.

So it means like Joachim said KinectAudioSource is not IDisposable.

Instead of using you can use

try
{
    Execute();
}
finally
{
    CleanupPart();
}

using is equivalent try-finally. You will only use try-finally when you want to do some clean up inside finally and don't care about the exception.

Scandinavia answered 12/2, 2014 at 5:54 Comment(0)
L
0

The class KinectAudioSource is supposed to implement the IDisposable interface in order to be using with using block. The classes that do not implement Idisposable could not be instantiated in using statement.

As a rule, when you use an IDisposable objAs a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned, MSDN

Lopsided answered 12/2, 2014 at 5:39 Comment(0)
C
0

KinectAudioSource is not IDisposable, so it cannot be used in a using block. What you probably mean to do is to close the data stream (which does implement IDisposable) instead when you're done recording, something like;

private Stream CreateAudioStream()
{
    var source = KinectSensor.KinectSensors[0].AudioSource;
    source.NoiseSuppression = _isNoiseSuppressionOn;
    source.AutomaticGainControlEnabled = _isAutomaticGainOn;
    return source.Start();
}
private object lockObj = new object();
private void RecordKinectAudio()
{
    lock (lockObj)
    {
        using (var source = CreateAudioStream())
        {
        }
    }
}
Casas answered 12/2, 2014 at 5:46 Comment(3)
Thank you for the immediate reply. The error vanished but now I have a new problem. The recorded audio (.wav/.mp3) file that I am saving is not being played by windows media player. It says - "Windows Media Player encountered a problem while playing this file".Cohere
@Cohere I'm not sure how you're saving it since it's not included in your code. The Stream returned contains data in 16-bit PCM format, sampled at 16 kHz, so if you're just copying it to a file it won't necessarily play "as is".Casas
What do you suggest I do??Cohere
G
0

Also adding .NET reference from System.EnterpriseServices version 2 will solve the error. This is especially useful if you are converting from and older version and you have multiple occurrences of "using" keyword to replace

Giguere answered 31/3, 2015 at 20:56 Comment(0)
F
0

The System.IDisposable error occurs because the connection you are trying to open might not close automatically, when out of scope of where the connection was opened.

Exclude your model connection creation from the using() clause, so that it just remains as :

var source = new CreateAudioSource(); /* Rest of code here */

Also make sure that you won't ommit the 'new' keyword for the object creation.

Farris answered 5/4, 2018 at 9:53 Comment(0)
N
0

I had a similar problem when creating a new console application .I had forgotten to add ENTITY FRAMEWORK reference in my project.

Adding reference on ENTITY FRAMEWORK resolve issue.

Nancinancie answered 10/9, 2018 at 15:15 Comment(0)
C
-1

You should add System.Data.Linq to your References in the project. This solved the problem for me.

Concentrated answered 21/4, 2015 at 9:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.