I can't seem to get my button to work. This is the first time i've tried to make an application. I just need a simple find / replace. I found some code on the internet and can't seem to get it to work.
http://pastebin.com/9v6TEFMs
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 WindowsFormsApplication1
{
public partial class Deneuralyzer : Form
{
public Deneuralyzer()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
using System;
using System.IO;
using System.Text.RegularExpressions;
string filePath = #"C:\Program Files (x86)\location\to\application\textfile.txt";
string searchText = "Count,2,";
string replaceText = "Count,200,";
ReplaceInFile(filePath, searchText, replaceText);
static public void ReplaceInFile(string filePath, string searchText, string replaceText)
{
StreamReader reader = new StreamReader(filePath);
string content = reader.ReadToEnd();
reader.Close();
content = Regex.Replace(content, searchText, replaceText);
StreamWriter writer = new StreamWriter(filePath);
writer.Write(content);
writer.Close();
}
}
}
}
also, does something specific need to be done, so that the application can edit the file? because doing it by hand I must change the file permissions and ownership.
errors out when i run the test
Error 3 Type or namespace definition, or end-of-file expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 59 1 WindowsFormsApplication1
Error 4 Syntax error, '(' expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 27 19 WindowsFormsApplication1
Error 6 Syntax error, '(' expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 28 19 WindowsFormsApplication1
Error 8 Syntax error, '(' expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 29 19 WindowsFormsApplication1
Error 2 Expected class, delegate, enum, interface, or struct C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 54 17 WindowsFormsApplication1
Error 1 } expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 35 70 WindowsFormsApplication1
Error 5 ) expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 27 25 WindowsFormsApplication1
Error 7 ) expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 28 28 WindowsFormsApplication1
Error 9 ) expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 29 49 WindowsFormsApplication1
You should read up on how to structure a class, or perhaps a C# tutorial. Normally you have the following order of elements in a class
// using statements, other components you use in your class
using System;
// namespace name (a group so to speak)
namespace NamespaceName {
// class, this gets nested under a namespace
public class MyClass {
// private variables
private int myVariable;
// constructors
public MyClass() {
// this is where you create the instance, set variables and stuff
myVariable = 314;
}
// methods
public void DoSomething() {
++myVariable;
}
private void anotherMethod() { }
}
}
Now, when the compiler tries to parse your codefile, since it's not structured in this manner, it complains
When you try to build the project the Error list window pops up with the errors you specified. You can double click each of these items and address them. What you can do is after each fix try to compile again, since some errors might be "follow up errors", i.e. errors which are fixed due to the first error being fixed.
In your case you have a method in the button click method. This is not allowed for a C# class, so you need to close the buttonClick method scope (the { } brackets that is) and move the using statements to the top of the cs file.
A tip is to indent your code since well formatted code is easier to read. Visual Studio makes this easy, you can click the Edit menu, choose Advanced and click Format Document (remember the shortcut Ctrl-k Ctrl-d). It can also help spot some errors, such as unmatched brackets.
Edit: Another tip btw is the Right click - Organize usings - Remove and sort option, to clear the cludder in the using statements in the beginning of the file. In many cases you don't need half of the ones which are included by default when Visual Studio creates a file. Later, if you find that you have a unrecognized class, press Ctrl + . ("ctrl dot") and you get the option to include the using statement needed for that class.
Related
I have tried to search for this but every example I find has a problem like them actually having the same namespace as their class or something.
I am simply trying to start using Linq. When I add new item Host is localhost. I have my database in Visualstudio and my project name is different than the DataContext name but I can't get it initialized. I get error:
'LinkedContext' is a namespace but is used like a type'
here is code...
namespace TryAgain
{
class Program
{
static void Main(string[] args)
{
LinkedContext db = new LinkedContext();
}
}
}
LinkedContext doesn't work? In settings of the Database Diagram it says the Entity Namespace is 'LinkedContext' So what am I missing. I thought I saw you could run that one line of code to connect your database that is already in VisualStudio due to adding a new item and then start playing with it? I just want to be able to practice with a database! Do stuff like:
var example = from x in example.Table
orderby x.field
select x;
you need using LinkedContext at the top of your file. the error you’re getting is telling you LinkedContext is a namespace but you’re treating like a type, ie a class. once you define it at the top you can then use the type that you need within that namespace.
added "using LinkedContext" to the top of code then also had to use LinkedDataContext not just LinkedContext:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LinkedContext;
namespace TryAgain
{
class Program
{
static void Main(string[] args)
{
LinkedDataContext db = new LinkedDataContext();
var example = from x in db.employees
orderby x.employee_id
select x;
foreach (var whatever in example)
{
Console.WriteLine(whatever.name);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
namespace SchoolPasswordLockFolder
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter the password: "); // the space character after the semicolon has an error
public string input = Console.ReadLine();
}
}
}
The errors:
Severity Code Description Project File Line
Error CS1513 } expected SchoolPasswordLockFolder c:\Users\CENSOREDSIJGIOFSGJIOFS\documents\visual studio 2015\Projects\App5\SchoolPasswordLockFolder\SchoolPasswordLockFolder\Program.cs 14
(for the one after the semicolon)
and
Severity Code Description Project File Line
Error CS1022 Type or namespace definition, or end-of-file expected SchoolPasswordLockFolder c:\Users\CENSOREDIDONTWANTSTALKERS\documents\visual studio 2015\Projects\App5\SchoolPasswordLockFolder\SchoolPasswordLockFolder\Program.cs 19
(for the last bracket)
I have not programmed in C# for a very long time as I was too busy with web development and lua...
change this:
public string input = Console.ReadLine();
to:
string input = Console.ReadLine();
local variables do not get accessibility modifiers like public.
I am trying to debug an existing application in VS2010, 4.0 framework. I get this compile-time error:
"The name 'b_resources' does not exist in the current context" .
I cannot see anything wrong in the code below:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
#if TR
using b_resources = Common.Resources.TResources;
#endif
#if EN
using b_resources = Common.Resources.EnResources;
#endif
namespace Common
{
public static class BResources
{
public static string caption { get { return b_resources.ProductName; } }
public static string company_name { get { return b_resources.CompanyName; } }
}
}
TResources and EnResources are resource files (.resx)
Am I missing some references related to .Net ?
The most obvious question is: do you define the TR or EN compilation symbol in your current build? If not, both #IF statements will be false and the using statement won't be included. In other words, if you do not define those symbols, the following code will be compiled:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
// Note: missing using statements
namespace Common
{
public static class BResources
{
public static string caption { get { return b_resources.ProductName; } }
public static string company_name{ get { return b_resources.CompanyName; } }
}
}
To check or set compilation symbols, go to your project's properties. Then on the Build tab you see as textbox named Conditional compilation symbols. Check if the symbols are defined there. If not, add one of them there.
If you are using Visual Studio, you can in fact immediately see if this is the case. If a symbol is not defined for the current build, the code within the #IF block will be greyed out. If the symbol is defined, it will display jsut like normal code.
You have no default case so if neither TR or EN is defined you get no definition of b_resources. You need to have some else cases in there so it can always compile. For example, if you wanted your EN resource as default:
#if TR
using b_resources = Common.Resources.TResources;
#elif EN
using b_resources = Common.Resources.EnResources;
#else
using b_resources = Common.Resources.EnResources; // or whatever you want as default
#endif
If neither TR or EN are defined the final else will be included.
For your code to compile, you need to define either TR or EN in the Build settings of your Project properties. Currently neither are defined, so b_resources never gets defined.
Environment: c#.net VS 2010
Solution has the following two projects:
A dll with several tested methods I've added.
A test project
The only thing in the test project is a form with following code: (names changed for readability)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using DLL_PROJECT; //Yes I remembered to include the dll project
namespace DLL_PROJECT_Test
{
public partial class frmTest : Form
{
private Class_1 myClass_1; //this comes from the dll - no errors here
private Class_2 myClass_2 = new Class_2(); // no errors here either
public frmTest()
{
InitializeComponent();
//TransparencyKey = BackColor;
this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = System.Drawing.Color.FromArgb(0, System.Drawing.Color.Black);
myDebouncer = new Debouncer(this);
this.SetDragging(true); //THIS EXTENSION COMES FROM THE DLL AND WORKS FINE
this.RoundCorners(40, 80); //AS DOES THIS ONE
myClass_2 = new Class_2();
myClass_2.HoldStartEvent += new Class_2EventHandler(myClass_2_HoldStartEvent);
myClass_2.DragStartEvent += new Class_2EventHandler(myClass_2_DragStartEvent);
}
private void myClass_2_DragStartEvent(Class_2 sender)
{
myClass_2("DragStart") += 1; //THE ONLY ERROR IS HERE AS FOLLOWS
//ERROR: "The name 'myClass_2' does not exist in the current context"
// - Yes, the DLL is included
// - Yes, the project is .Net 4 (not client profile)
// - Yes xxx WRONG xxx, this exact syntax has been tested before on an instance of
// this class, it's just a default parameter.
// xxx should be [] instead of () for the indexer in c#. #VB_Fails
}
void myClass_2_HoldStartEvent(Class_2 sender)
{
this.Close();
}
}
}
This code:
myClass_2("DragStart") += 1;
... is using myClass_2 as if it were either the name of a method or a delegate instance.
Did you actually mean to use the indexer? That would be:
myClass_2["DragStart"] += 1;
What does "DragStart" mean here? Is it actually a property name? Perhaps you want:
myClass_2.DragStart += 1;
I very much doubt that "this exact syntax has been tested before on an instance of this class".
Admittedly the error message doesn't make much sense in this case. I think it's actually more likely that you've got a typo in your real code - a typo which isn't propagated here because you've changed the names. If you could reproduce this in a short but complete program, it would make life a lot simpler.
I added as reference 3 dll's: Google.Apis , Google.Apis.Translate.v2 , System.Runtime.Serialization
In Form1 i have one line:
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Translator.translate(new TranslateInput());
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Now the error the exception is on the first line in the class Translator:
The line that throw the error is: var service = new TranslateService { Key = GetApiKey() };
The class code is:
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.Net;
using System.IO;
using System.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using Google.Apis.Util;
using Google.Apis.Translate.v2;
using Google.Apis.Translate.v2.Data;
using TranslationsResource = Google.Apis.Translate.v2.Data.TranslationsResource;
public class Translator
{
public static string translate(TranslateInput input)
{
// Create the service.
var service = new TranslateService { Key = GetApiKey() };
string translationResult = "";
// Execute the first translation request.
Console.WriteLine("Translating to '" + input.TargetLanguage + "' ...");
TranslationsListResponse response = service.Translations.List(input.SourceText, input.TargetLanguage).Fetch();
var translations = new List<string>();
foreach (TranslationsResource translation in response.Translations)
{
translationResult = translation.TranslatedText;
}
return translationResult;
}
private static string GetApiKey()
{
return "AIzaSyCjxMe6RKHZzd7xSfSh2pEsBqUdXYm5tA8"; // Enter Your Key
}
}
/// <summary>
/// User input for this example.
/// </summary>
[Description("input")]
public class TranslateInput
{
[Description("text to translate")]
public string SourceText = "Who ate my candy?";
[Description("target language")]
public string TargetLanguage = "fr";
}
The error is:
Could not load type 'Google.Apis.Discovery.FactoryParameterV1_0' from assembly 'Google.Apis, Version=1.1.4497.35846, Culture=neutral, PublicKeyToken=null'.
Tried to google for help and also tried to change the project type to x64 platform but it didnt help. So i put it back on x86
I have windows 7 64bit visual studio c# 2010 pro .net 4.0 profile client.
Cant figure out what is the error ?
This error as reported in the above-posted messages is due to a local copy in the bin\Debug folder of your solution or project. Even though you attempt to clean your solution, such copies will persist to exist.
In order to avoid this to happen, you have to force Visual Studio to refer to the correct DLL by adding reference paths within a project properties. Unfortunately, if you got several projects within your solutions, you will have to set the reference paths for the projects one after another until completed.
Should you wish to know how to setup reference paths follow these simple instructions:
1.Select your project, right-click, then click "Properties";
2.In the project properties, click "Reference Paths";
3.Folder, type or browse to the right location of your DLL, click [Add Folder].
You will need to perform these steps for as many different locations you may have for each of your DLLs. Consider setting an output path under the Build tab of the same project properties, so that you may output your DLLs in the same directory for each of them, thus assuring you to find all the latest builds under the same location, simplifying forward your referencing.
Note this can only be one reason for this error. But it is sure that is has to do something with a wrong copy of the mentioned assembly.