Click here to Skip to main content
15,896,111 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# When I get Focallength find errs. Pin
Richard MacCutchan14-Mar-19 23:03
mveRichard MacCutchan14-Mar-19 23:03 
GeneralRe: C# When I get Focallength find errs. Pin
Member 1415621218-Mar-19 15:36
Member 1415621218-Mar-19 15:36 
Questiondynamic URL for soap webservice Pin
Member 302085113-Mar-19 22:46
Member 302085113-Mar-19 22:46 
AnswerRe: dynamic URL for soap webservice Pin
dan!sh 14-Mar-19 5:27
professional dan!sh 14-Mar-19 5:27 
QuestionTrying to find the problem in this simple C$ program Pin
Brian_TheLion13-Mar-19 20:16
Brian_TheLion13-Mar-19 20:16 
AnswerRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff13-Mar-19 21:09
mveOriginalGriff13-Mar-19 21:09 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion13-Mar-19 23:13
Brian_TheLion13-Mar-19 23:13 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff13-Mar-19 23:41
mveOriginalGriff13-Mar-19 23:41 
Think about it!

Main has to be static - it can't have an instance or it can't be called.

So Main wants to call other methods - are they static or instance?

What should they be?
C#
public static void Main()
   {
   MyMethod();
   }
Is calling MyMethod from a static method (called Main, it this applies to all static methods)
Let's go back to cars - can you do this?
C#
public static void AskAboutCar()
   {
   Color col = WhatColourIsIt();
   }
No - because you aren't specifying a car you can't have an answer to that question - it needs an instance of a car in order to have a colour. You could make WhatColourIsIt static and the error goes away ... but you can't return any meaningful information from the method because you still don't know which car you are referring to! You can do this:
C#
public static void AskAboutCar()
   {
   Car myCar = GetMyCar();
   Color col = myCar.WhatColourIsIt();
   }
Because you are specifying which car you are talking about. myCar contains an object reference to my car - not your car, not that car over there; just my car - so it has a colour. If I change my car, the variable myCar doesn't change, but the value it contains (the reference) changes to a different instance of a car, which may or may not be a different colour (I like black cars but Herself may want one that matches her eyes).

So you need to think about the rest of the methods you are trying to call: are they independent of any particular instance of the containing class or not (do they need a car?)
If they don't, they can be static and you can "just use them" - possibly with the class name in front of them:
C#
public static void AskAboutCar()
   {
   int wheels = Car.HowManyWheels():
   }
If they do, then you need to create an instance, and then access the method via that:
C#
public static void AskAboutCar()
   {
   Car myCar = GetMyCar();
   Color col = myCar.WhatColourIsIt();
   }

Bear in mind that static methods can't access any instance related items: the variables, fields, methods, properties, delegates, and events are all unavailable unless you have a specific instance to refer to.

Inside a instance method, all instance data is automatically available, either directly:
C#
public class Car
   {
   public Color Color { get; set; }
   public Color WhatColourIsIt()
      {
      return Color;
      }
   }
or via a special word this which directly specifies the current instance:
C#
public class Car
   {
   public Color Color { get; set; }
   public Color WhatColourIsIt()
      {
      return this.Color;
      }
   }
(But you don't need to use this unless a local variable or a method parameter "masks" the class version:
C#
public class Car
   {
   private Color Color { get; set; }
   public void SetColour(Color Color)
      {
      this.Color = Color;
      }
   }
In "normal" code, Main builds an instance of a class, and uses that (finds my car, and does things with it), and static isn't used much at all.

Make sense?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!

GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 0:11
Brian_TheLion14-Mar-19 0:11 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 0:26
mveOriginalGriff14-Mar-19 0:26 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Eddy Vluggen14-Mar-19 3:38
professionalEddy Vluggen14-Mar-19 3:38 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Richard MacCutchan13-Mar-19 23:52
mveRichard MacCutchan13-Mar-19 23:52 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion13-Mar-19 23:59
Brian_TheLion13-Mar-19 23:59 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 0:28
mveOriginalGriff14-Mar-19 0:28 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Richard MacCutchan14-Mar-19 1:51
mveRichard MacCutchan14-Mar-19 1:51 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 0:28
Brian_TheLion14-Mar-19 0:28 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 0:44
mveOriginalGriff14-Mar-19 0:44 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 1:09
Brian_TheLion14-Mar-19 1:09 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 1:19
Brian_TheLion14-Mar-19 1:19 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Richard MacCutchan14-Mar-19 1:54
mveRichard MacCutchan14-Mar-19 1:54 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 1:57
Brian_TheLion14-Mar-19 1:57 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Richard MacCutchan14-Mar-19 2:16
mveRichard MacCutchan14-Mar-19 2:16 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 2:22
Brian_TheLion14-Mar-19 2:22 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Richard MacCutchan14-Mar-19 2:25
mveRichard MacCutchan14-Mar-19 2:25 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 2:01
mveOriginalGriff14-Mar-19 2:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.