简体   繁体   English

在C#Visual Studio 2013中测试代码(在当前上下文中不存在方法名称)

[英]Testing Code in C# Visual Studio 2013 (Method Name Does Not Exist In Current Context)

I am trying to test some methods but it is saying "The name [method] does not exist in the current context." 我正在尝试测试一些方法,但是它说“名称(方法)在当前上下文中不存在”。

To test the methods AddUnsynchronizedPing() , GetUnsynchronizedPings() , and SetAllPingsSynchronized() I had to create a Ping object, which I did. 为了测试方法AddUnsynchronizedPing()GetUnsynchronizedPings()SetAllPingsSynchronized()我必须创建一个Ping对象。

The methods are from my DataManager class: 这些方法来自我的DataManager类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

using BB.Mobile.Models;
using SQLite;

namespace BB.Mobile
{
    /// <summary>
    /// A class to provide a single interface for interacting with all SQLite data operations for stored tracking points.
/// </summary>
/// 
class DataManager
{
    private SQLiteConnection db = null;

    public DataManager()
    {
        if (this.db == null)
        {
            string dbPath = Path.Combine(
             System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
             "bb.db3");

            db = new SQLiteConnection(dbPath);
            db.CreateTable<Ping>(); 
            db.CreateTable<PingGroup>();
        }
    }

    /// <summary>
    /// Will compile and return all matching unsynchronized ping data from the SQLite database.
    /// </summary>
    /// <returns></returns>
    public List<PingGroup> GetUnsynchronizedPings()
    {
        List<PingGroup> unsynchronizedPings = new List<PingGroup>();

        // TODO: Retrieve all unsynchronized pings from the SQLite database and return them to the caller.
        //var pGroup = db.Get<PingGroup>();
        //var pGroupList = db.List<PingGroup>();

        var pGroups = db.Table<PingGroup>();
        foreach (var pGroup in pGroups)
        {
           var pings = db.Query<Ping>("select * from Pings where PingGroupID = " + pGroup.ID.ToString());
           pGroup.Pings = pings;
        }

        return unsynchronizedPings;
    }

    /// <summary>
    /// Insert a single ping group into the SQLite ping database.
    /// </summary>
    /// <param name="pingGroup"></param>
    public void AddUnsynchronizedPing(PingGroup pingGroup)
    {
        // TODO: Add the passed ping group parameter into the SQLite database as new/unsynchronized.
        if (pingGroup != null)
        {
            // Add ping group to the database.
            db.Insert(pingGroup);

            foreach (var ping in pingGroup.Pings)
            {
                ping.PingGroupID = pingGroup.ID;
            }

            db.InsertAll(pingGroup.Pings);
        }
    }

    /// <summary>
    /// Mark all open and unsynchronized pings in the database as synchronized.
    /// </summary>
    public void SetAllPingsSynchronized()
    {
        db.DeleteAll<PingGroup>();
        db.DeleteAll<Ping>();
    }
}
}

Where I am testing said methods is in the OnCreate method of my in my MainActivity class. 我正在测试的方法位于MainActivity类中的OnCreate方法中。 Here is said method: 这里说方法:

 protected override void OnCreate(Bundle bundle)
    {

        Ping p = new Ping()
        {
            Latitude = +0.000000,
            Longitude = -90.000000,
            DateCreated = DateTime.UtcNow,
        };

        AddUnsynchronizedPing(p);
        GetUnsynchronizedPings(p);
        SetAllPingsSynchronized(p);
        GetUnsynchronizedPings(p);

        base.OnCreate(bundle);
        this.userSettings = new Settings(this.ApplicationContext);
        //this.setLayoutToSettings();
        if (this.userSettings.isUserLoggedin()) // check for logged in user to switch between layouts
        {
            this.setLayoutToSettings();
            InitializeAlarms();
        }
        else
        {
            this.setLayoutToLogin();
        }
    }

As implied above, the methods AddUnsynchronizedPing() , GetUnsynchronizedPings() , and SetAllPingsSynchronized() in the MainActivity class are returning the "The name [method] does not exist in the current context" error. 如上所述, MainActivity类中的方法AddUnsynchronizedPing()GetUnsynchronizedPings()SetAllPingsSynchronized()返回“名称[方法]在当前上下文中不存在”错误。

You need to create an instance of DataManager first (you also need to be sure DataManager is public) 您需要首先创建DataManager的实例(还需要确保DataManager是公共的)

DataManager mgr = new DataManager();

// this method requires a PingGroup argument, so you need to instantiate and pass one
PingGroup pg = new PingGroup();
mgr.AddUnsynchronizedPing(pg);

var pings = mgr.GetUnsynchronizedPings();
mgr.SetAllPingsSynchronized();

将DataManager类公开。

public class DataManager

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2013的当前上下文中不存在名称“我的” - The Name 'My' does not exist in current context in Visual Studio 2013 Visual C#-错误1名称&#39;a&#39;在当前上下文中不存在 - Visual C# - Error 1 The name 'a' does not exist in the current context C# - 当前上下文中不存在该名称 - C# - The name does not exist in the current context 该名称在当前上下文中不存在c# - The name does not exist in the current context c# C# 名称“...”在当前上下文中不存在 - C# The name "..." does not exist in the current context 当前上下文中不存在名称“ViewBag” - Visual Studio 2015 - The name 'ViewBag' does not exist in the current context - Visual Studio 2015 当前上下文中不存在名称“DependencyProperty”(Visual Studio) - The name 'DependencyProperty' does not exist in the current context (Visual Studio) C#方法“方法”没有重载接受0个参数,名称“名称”在当前上下文中不存在 - c# no overload for method “method” takes 0 arguments, the name “name” does not exist in the current context 当前上下文中不存在名称“CommandManager”(Visual Studio 2015) - The name “CommandManager” does not exist in the current context (Visual Studio 2015) 错误:该名称在当前上下文(Webpart,C#,Visual Studios,SharePoint 2010)中不存在 - Error: The name does not exist in the current context (Webpart, C#, Visual Studios, SharePoint 2010)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM