4 Replies - 1506 Views - Last Post: 05 December 2012 - 02:44 PM Rate Topic: -----

#1 RangerDanger816  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-October 12

How to make a "super" class in C#?

Posted 05 December 2012 - 09:30 AM

I've had experience in Java, but new to C#. Here is the Java version of a superclass that I need to convert to C#:

//Java Version
        public Warrior() 
        {
		       super(BASE_NAME,BASE_HEALTH,BASE_STRENGTH,BASE_INTELL,
			     BASE_DEX,BASE_GOLD,BASE_JOB, BASE_SKILLS, BASE_ITEMS);
	    }


I've tried

//attempted C# version
        public Warrior()
        {
		    base(BASE_NAME,BASE_HEALTH,BASE_STRENGTH,BASE_INTELL,
			       BASE_DEX,BASE_GOLD,BASE_JOB, BASE_SKILLS, BASE_ITEMS);
	    }


but an error says that 'base' is not valid in this content.

Is This A Good Question/Topic? 0
  • +

Replies To: How to make a "super" class in C#?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: How to make a "super" class in C#?

Posted 05 December 2012 - 09:43 AM

There is a tutorial linked in my signature block on C# classes that should help you out.
Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: How to make a "super" class in C#?

Posted 05 December 2012 - 09:50 AM

Just because the syntax of the two languages looks a lot alike doesn't mean they are the same language. You can't just jump in and feel around and think everything is going to work exactly the same.

I'd suggest you run through an intro to C# book just to familiarize yourself with the differences. Since you have a foundation in JAVA one of those "in 30 days" books should be more like "in 15 hours" and save you a lot of wrong assumptions, misunderstands and general frustration. At the very least go through the learning C# series of articles.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4312
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: How to make a "super" class in C#?

Posted 05 December 2012 - 10:42 AM

Definitely do the reading that tlhIn`toq assigned.

The short answer for posterity's sake is that you invoke a base constructor by using the colon operator.

public class MySuper
{
	public MySuper(string name)
	{
		Console.WriteLine(name);
	}
}

public class MyDerived : MySuper
{
	public MyDerived(string name, int age) : base(name)
	{
		Console.WriteLine(age);
	}
}
 

Was This Post Helpful? 4
  • +
  • -

#5 RangerDanger816  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-October 12

Re: How to make a "super" class in C#?

Posted 05 December 2012 - 02:44 PM

Thanks so much everyone. I'll definitely do some of those readings. :) and Curtis, thank you, that made a lot of sense.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1