error : 'classA' doesnot exist in current context - c#

I started new project of Windows Forms. First page is UserLogin. in form load event I am taking the logintable from database into memory.
I am using a class in which all read and write operations methods are stored.
when I am calling the class in formload event, the error comes "doesnot exist in current context"
I may be missing some references or headerfilename..
Please Help
Thanks in Advance
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Web;
namespace InvestmentAndReturns
{
public partial class UserLogin : Form
{
public UserLogin()
{
InitializeComponent();
CenterToScreen();
}
public string UserID = null;
private string userPass = null;
public string UserGroup = null;
DataTable UserLoginTable;
int logintry = 1;
public bool LoginStatus = false;
private void Form1_Load(object sender, EventArgs e)
{
txtUserID.Select();
string cmd = "Select * from UserLogin";
UserLoginTable = DbRdRw.SqlDbRead(cmd, "UserLogin");
}
DbRdRw: this is the Class
SqlDbRead: this is read method

i have included that class by directly pasting it in the project folder and then opened solution and then included in the project
It looks like you have copy pasted source file containing definition of DbRdRw from some other project - which mostly means the namespace declaration in the file will be from older project.
Things will work if you change the namespace to your current project InvestmentAndReturns.
However, why are you copy pasting this around? You could make a library dll for your DbRdRw and reference the dll. That way you will keep your source code at one place.

Related

public variable in App class can't get referenced in other class xamarin forms

I have this as my App.xaml.cs
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using System.Threading.Tasks;
using Xamarin.Forms.Xaml;
using Newtonsoft.Json;
using System.Net.Http;
namespace MyShop
{
public partial class App : Application
{
public string productsJSON = {{very large string}};
public List<Product> jsonDataCollection = new List<Product>();
public App()
{
InitializeComponent();
jsonDataCollection = JsonConvert.DeserializeObject<List<Product>>(productsJSON);
foreach (Product p in jsonDataCollection)
{
Application.Current.Properties[p.Id] = 0;
}
MainPage = new NavigationPage(new Page1());
}
...
In my ProductsPage.xaml.cs, I want to reference the public variable productsJSON so I call it as App.productsJSON and assigned to a string variable productsJSON.
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Newtonsoft.Json;
using System.Net.Http;
namespace MyShop
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ProductsPage : ContentPage
{
public ProductsPage(string title)
{
InitializeComponent();
Title = title;
string productsJSON = App.productsJSON;
displayProducts(productsJSON);
}
...
For that, I get the error:
CS0120: An object reference is required for the non-static field, method, or property 'App.productsJSON'
I'm confused because from the solutions online when referencing a property from another class, you just call the class then dot, and then the property name. I don't quite get the object reference error and it didn't work when I casted it to string like <string>App.productsJSON
you need a reference to the instance of the App class in order to access a non-static field or property
var json = ((App)Application.Current).productsJSON;

How to read a json file stored online and change image to url when found

I am making an app in unity and i need the user to search for a keyword and it will return all the imageurls stored in an online json file that relate to that keyword (slug)
My colleague wrote me the below code, as she has way more knowledge with the language than me, but she doesnt use unity and i dont know if its suitable for unity or changing the texture of the image, as i cant seem to trigger it. The json file is currently stored in the project, but i would prefer it to read something that is online.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class UrlOpener : MonoBehaviour
{
public string imageaddress;
public void Open()
{
using (StreamReader r = new StreamReader("Assets/document.json"))
{
string json = r.ReadToEnd();
var img= JsonUtility.FromJson<ArtImage>(json);
imageaddress = img.imageurl;
}
}
}
[Serializable]
class ArtImage
{
public string name { get; set; }
public string imageurl { get; set; }
}
You can use WebClient to download content of remote file:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using UnityEngine;
public class UrlOpener : MonoBehaviour
{
public string imageaddress;
public void Open()
{
using (var client = new WebClient())
{
string json = client.DownloadString("http://www.example.com/some.json");
var img= JsonUtility.FromJson<ArtImage>(json);
imageaddress = img.imageurl;
}
}
}
Note that depending on .NET profile you are using, you may need to add assembly references for System.Net.WebClient.dll and System.Net.dll. You can find more details on how to add assembly references here.

I get an error in C# "The type or namespace name does not exist in namespace"

I get an error in C# "The type or namespace name does not exist in namespace". I checked everywhere but it didn't solve my problem here is the main program
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
using BlockChainMySelf;
using Formatting = Newtonsoft.Json.Formatting;
namespace BlockChainMySelf
{
class Program
{
static void Main(string[] args)
{
var startTime = DateTime.Now;
BlockChainMySelf.BlockChain StepCoin = new BlockChain();
StepCoin.CreateTransaction(new Transaction("Henry", "MaHesh", 10));
StepCoin.CreateTransaction(new Transaction("lkjsdf", "MaADLKHesh", 15));
StepCoin.CreateTransaction(new Transaction("Henry", "MaHesh", 20));
StepCoin.CreateTransaction(new Transaction("Henry", "MaHesh", 60));
StepCoin.ProcessPendingTransactions("Bill");
And here is the class that I want to call
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
using BlockChainMySelf;
using Formatting = Newtonsoft.Json.Formatting;
namespace BlockChainMySelf
{
public class BlockChain
{
IList<Transaction> PendingTransactions = new List<Transaction>();
public IList<Block> Chain { set; get; }
public int Difficulty { set; get; } = 2;
Here are the Screendhots
Main
Class
answerquestion
answerquestion2
The second screenshot in an earlier edit of your question clearly shows the BlockChain class as being 'Miscellaneous Files' in Visual Studio:
The MSDN page for the Miscellaneous Files Project says (emphasis mine):
When a user opens project items, the IDE assigns to the Miscellaneous Files project any items that are not members of any projects in a solution.
Presumably you were in the middle of trying to fix the issue, so you put static in - but that won't work because then you can't create an instance of a BlockChain.
Your question is a duplicate of Visual Studio - project shows up as “Miscellaneous Files”.
The/a solution is to right-click on the bad file in Solution Explorer, remove it from the project, then re-add it, e.g. this answer.
I had this issue... however, I had two classes under the same namespace but in different projects. All I had to do to fix this was to add a reference to the project directly.

Using System.Windows.Form not greyed

I've created a windows form called Grades and when I click the Submit button, it displays the "System.Windows.Form" onto the label and then the text I've entered onto the textbox. For example, it displays "System.Windows.Form Tom". Now I've been researching online and people say that I should add it as a reference. I already did that and it seems as though that the using statement isn't greyed in like the other using statements. Please Help!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Grades
{
public partial class frmGrades : Form
{
public frmGrades()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
lblDisplayStudentName.Text = Convert.ToString(txtBoxStudentName);
lblDisplayMessage.Text = Convert.ToString(txtBoxStudentGrade);
}
}
}
You need to get the text property of the textbox :
lblDisplayStudentName.Text = txtBoxStudentName.Text;
lblDisplayMessage.Text = txtBoxStudentGrade.Text;

namespace not being recognized?

I created a very very simple domain layer in visual studio (2010). I then used the new test wizard to create a basic unit test. However when I try to put in the using statement so that I can test my code.. it says my namespace could not be found... This is my first time using visual studio so I am at a loss as to what I am doing wrong.
My code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Home
{
class InventoryType
{
/// <summary>
/// Selects the inventory type and returns the selected value
/// </summary>
public class InventorySelect
{
private string inventoryTypes;
public String InventoryTypes
{
set
{
inventoryTypes = value;
}
get
{
return inventoryTypes;
}
}
/// <summary>
/// Validate that the inventory is returning some sort of value
/// </summary>
/// <returns></returns>
public bool Validate()
{
if (InventoryTypes == null) return false;
return true;
}
}
}
}
My Test Code
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Home.InventoryType.InventorySelect;
namespace HomeTest
{
[TestClass]
public class TestInventoryTypeCase
{
[TestMethod]
public void TestInventoryTypeClass()
{
InventorySelect select = new InventorySelect();
select.inventoryTypes = "Collection";
if (Validate() = true)
Console.WriteLine("Test Passed");
else
if (Validate() = false)
Console.WriteLine("Test Returned False");
else
Console.WriteLine("Test Failed To Run");
Console.ReadLine();
}
}
}
using refers to a namespace, not a specific class (unless you add an alias for the class name). Your using statement should only include the word Home.
using Home.InventoryType.InventorySelect;
//becomes
using Home;
Here is a link to MSDN on using directive: using Directive (C#)
I'm assuming your test class is in its own project, so you need to add a reference to that project. (A using statement doesn't add a reference, it merely allows you to use a type in your code without fully qualifying its name.)
Declare InventoryType class as public
InventorySelect class can be private rather than public
When you create a "multi-project" in a solution (by adding projects to any existing solution), the projects do not know about each other.
Go to your test project on Solution Explorer and under "References", right click and select "Add Reference". Then select "project" tab and you will be able to add your project's reference to the test project.
Also, make sure you define the classes in the project as "public" to be able to access them in test project.
namespace Home
{
public class InventoryType
{
...
}
}
Note that you still need the "using" keyword on top of your C# test class:
using Home;
namespace HomeTest
{
public class TestInventoryTypeCase
{
...
}
}

Categories

Resources