|
|
Hello,
I am trying to learn functions in C#. I wrote the following test functions and I am getting the error: Error 1 An object reference is required for the non-static field, method, or property 'Testing_Functions.Program.Test(double, double, double)' C:\Users\computerpublic\AppData\Local\Temporary Projects\Testing Functions\Program.cs 18 29 Testing Functions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Testing_Functions
{
class Program
{
public Double Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
return Test_Val;
}
static void Main(string[] args)
{
Double A = 1, B = 2, C = 3;
Double Result = Test(A,B,C);
}
}
}
|
|
|
|
|
You have to declare the Test method as "static", just like your Main method.
Either that, you have to new up an instance of the Program class and use the Test method through that.
Serisouly, this is C# 101 stuff. You really need to pick up a beginners book on C# and work through it.
|
|
|
|
|
I am using the a book and I am following the example. I don't know why I am getting the error.
|
|
|
|
|
Look up the keyword "static", as that is why you are getting that particular error.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
You're trying to call an instance method on an implicit this, but Main is static so there is no this.
|
|
|
|
|
The function "Main " is the start point of the program. When "Main " is called, no instance of any object could yet be created, hence it must be static .
From a static method, you can reach other static methods. Or you can create an object. When you create a e.g. new Windows Forms application in Visual Studio, it adds a line to Main : Application.Run(new Form1()); . This creates a new object of type Form1 .
Similarly, you could write another class, create the corresponding object, and call its function, e.g.
namespace Testing_Functions
{
class Addition
{
public Double Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
return Test_Val;
}
}
}
and then call it from your Main method:
Addition a = new Addition();
Double Result = a.Test(A,B,C);
|
|
|
|
|
I think it's been explained pretty well already, but just in case...
static methods are available without an instance of the class. Non static methods are only available through an instance of the class.
This gives you two options in your case. Either
1. Make the Test method static
public static Double Test(Double Val1, Double Val2, Double Val3)
or
2. Create a new instance of the class that holds the method (Program in this case).
Double Result = new Program().Test(A,B,C);
alternative
Program program = new Program();
Double Result = program.Test(A,B,C);
|
|
|
|
|
What is the C# equivalent to the following VB.NET code?
Delegate Sub SetStatus(ByVal Text As String)
Private Sub SetStatusStrip(ByVal Text As String)
Dim d As New SetStatus(Sub()
End Sub)
Me.Invoke(d, New Object() {Text})
End Sub
|
|
|
|
|
This seems homeworky but since you could just whip it through an autotranslater anyway, here you go:
delegate void SetStatus(string text);
private void SetStatusStrip(string text){
Invoke(text => {
}, new object[] { text } );
}
I think that's right. Invoke is a bit sniffy sometimes, you might need to cast the lambda, but I don't think so. Although it seems like the VB method you're generating for the delegate doesn't actually use a parameter so there's no reason you need to use that delegate for the inner method.
|
|
|
|
|
Doesn't sound like homework to me; I don't know any schools that mix VB/C#, but I do know that managers "insist" on keeping their codebase in a single language
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
I assure you it's not homework. I've been programming (promoted from drafting) for about three years. I started in VB.Net and am now trying to convert to C#. That doesn't seem to work. How can I cast a lambda expression? Thanks for the auto translate idea. I ran the code through a couple and none seem to work. Thanks for your help.
|
|
|
|
|
internal delegate void SetStatus(string Text);
private void SetStatusStrip(string Text)
{
SetStatus d = new SetStatus(() => {
});
this.Invoke(d, new object[] {Text});
}
David Anton
Convert between VB, C#, C++, & Java
www.tangiblesoftwaresolutions.com
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter
|
|
|
|
|
Thank you sir. The following is what I ended up using and it works great.
SetStatus d = new SetStatus( (string myText) =>
{
});
this.Invoke(d, new object[] { _text });
|
|
|
|
|
I want to send sms through asp.net. Can anybody give me working code please.I want it urgent
|
|
|
|
|
|
This has been posted in the .NET forum and (correctly) int the ASP forum.
|
|
|
|
|
You've posted this in 3 different forums, and the answer is easily Googled.
|
|
|
|
|
How can I save an Excel file which will prompt user to select where to save? What save mode should I use?
|
|
|
|
|
More detail is needed.
Is this a web app? WinForms? Excel VBA code? What?
How are you generating the workbook to "save"??
There's no such thing as a "save model". Your use of the term could mean anything.
modified 15-Aug-12 8:06am.
|
|
|
|
|
Prompting the user for a save-location is done using the standard SaveFileDialog . It doesn't matter what type of file you want to save, the dialog is always the same.
Check out the documentation[^].
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Ladies And Gentlemen:
Here I use a SerialPort to send and receive message(of Bytes).
But it cannot really work.
MCU can send a string to PC, and PC can send a string to MCU too.
The problem is , when PC sent a string first,later when you about to send a string to PC from MCU ,it losts a byte,the first or the second one.
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int temp = serialPort1.ReadByte();
if (IsAddrConfirmed == 0 && NeedAddrConfirm == 1)
{
IsAddr = confirmAddr((byte)temp);
IsAddrConfirmed = 1;
}
if (!IsAddr && AddrBacked_2 == 1)
{
try
{
textBox3.Text += "temp是:" + temp + "\r\n";
textBox3.Text += "收到字符串:" + (char)temp + serialPort1.ReadLine() + "\r\n";
When dataReceived,temp got a value
Look at the last two or three lines ,is it the point?
Thank you very much,but sorry for my bad English.
Little bird looking for heros
|
|
|
|
|
You're right, the English is rough, but we'll work through that... The sample code is interesting, but far too little to make an educated guess. The non-English characters are confusing to us, but to a serial port, bytes is bytes - it doesn't care. There's a clue in the fact that the first couple of bytes are being lost, I think. The fact that you get any meaningful data at all indicates that you have the baud rate set correctly, so we can forget that. But the handshaking might be mismatched. The PC port and the MCU port must be set to use the same protocol in order to properly sync the messages. Modern PCs will attempt to auto-negotiate handshaking if the mode is set to 'none', I believe, but MCUs aren't as sophisticated. Confirm that the handshaking protocols are identical.
Another possibility is that you (or the MCU) are expecting to have the PC's undivided attention to the data stream incoming on the serial port. That's not the case with a multi-tasking OS like Windows. You'll want to dedicate a thread to listening on the serial port with a fairly high priority in order to catch the leading bits of a randomly-generated message from the MCU. If you don't, Windows may be off doing something else when the first couple of bytes arrive, and they'll be lost.
Post a bit more of your code so that we can see what you've set up the system to do, and feel free to post the MCU code, as well. It helps to see what both sides of the conversation are expecting from each other. I know that serial communications is old and well established, but it's quirky at best, and the "standard" isn't too well enforced. Every bit of information you can provide will be helpful.
Will Rogers never met me.
|
|
|
|
|
I am trying to scrape a web page. The URL is
http://reports.ieso.ca/public/GenOutputCapability/PUB_GenOutputCapability.xml
If I open the link in a browser window there is information about when the report was created, totals, etc. If I scrape the URL from C# using a WebClient or WebRequest, only the XML file is returned. It appears that much of the information on the page is stored in a dymanically created style sheet on their web site.
Is there a way to use a WebClient or WebRequest to get exactly what I see on the web page?
Thanks,
Mark Jackson
|
|
|
|
|
Dunno. Maybe try the WebBrowser control and check its document property.
|
|
|
|