Jump to content

[C#]How to use variables from base class?

- - - - -

  • Please log in to reply
7 replies to this topic

#1
MienTommy

MienTommy

    Newbie

  • Members
  • Pip
  • 2 posts
I'm learning how to program with C# WPF. I've created a class that derives from a base class. But there is only problem, it doesn't let me access variables from the base class even when my base class variables are public.

These are 2 seperate class files.

How do I use variables created from my base class, in my derived class?

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Post your code as there is no way for us to determine what you might be doing without it.

#3
MienTommy

MienTommy

    Newbie

  • Members
  • Pip
  • 2 posts
file1: baseclass.cs


class baseclass

{

public int A = 10;

}


file2: derivedclass.cs

class derivedclass : baseclass

{

A = 20; //Compiler error here

}



#4
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Of course there is an error, you can't put executable code in a class without a method. Has nothing to do with the base class.

#5
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
@Mien Tommy

try with this


...

class derivedclass: baseclass

{

  int b;

  b = a;

  //if you are using console application then write Console.Write(b); and if you are using winforms then write MessageBox.Show("{0}",b);

}



#6
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
this is the simple example

#7
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts

Tonchi said:

this is the simple example
You do realize that your code won't work either, as it's not in a method.

#8
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
i know...this is only a clue...he should know how to write Main function if he starts C#




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users