13 Replies - 2682 Views - Last Post: 30 September 2005 - 08:10 PM Rate Topic: -----

#1 chasboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-April 05

class help please set me straight

Post icon  Posted 29 September 2005 - 07:50 PM

I have programmed in c++ before and created classes and structs and include files. But for some reason when I do this in C# i can not call the classes, functions, or structures I have created in the included file. Errrrr what am i missing. I have tried to follow examples but I must be totally clueless because nothing is working. You are going to ask for a code sample but a general answer would do just great if appropriate.

Thanks for the reply.

Is This A Good Question/Topic? 0
  • +

Replies To: class help please set me straight

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1866
  • View blog
  • Posts: 20,278
  • Joined: 17-March 01

Re: class help please set me straight

Posted 29 September 2005 - 07:52 PM

Welcome to dream.in.code chasboi, can you post your code so we can take a look. Thanks!!
Was This Post Helpful? 0
  • +
  • -

#3 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: class help please set me straight

Posted 29 September 2005 - 07:54 PM

C# is not compiled the same way as c++; in C++, seperate header files and source files are all brought together in the order you include them to create a body of code.

In C#, you can have multiple source files for the sake of organization, but there are no header files. C# is pre-arranged into one body of code and then compiled (thus order of declaration is irrelevant, eliminating the need for function prototypes and headers and general).

Classes are not quite used in the same way as in C++; classes in C# function much more like classes in java, if you are familiar with that.
Was This Post Helpful? 0
  • +
  • -

#4 chasboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-April 05

Re: class help please set me straight

Posted 29 September 2005 - 08:14 PM

CODE

Class Peronal_Info_Validator()
.........

.........

Public EMail_Validator(string emailAddress)
{
// validate email via regular expression and other criteria
}

Thanks for the reply. I have been going nuts about this. :D

This post has been edited by chasboi: 29 September 2005 - 08:15 PM

Was This Post Helpful? 0
  • +
  • -

#5 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: class help please set me straight

Posted 29 September 2005 - 08:19 PM

I'm assuming Email_Validator() is intendend as a function as part of the above class?

In that case, you need to remember to have a function type with it:

public void/bool/int/char/whatever EMail_Validator(string emailAddress)
{
//ladedadeda
return whatever_you_need_to;
}
Was This Post Helpful? 0
  • +
  • -

#6 chasboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-April 05

Re: class help please set me straight

Posted 29 September 2005 - 10:45 PM

thanks Videege

I should have finished the function

public bool Email_validator(string emailAddress)
{
latada diddy dah

}

I want to use the functions in this class on multiple pages and in mmultiple applications but am running into a perverbial wall on the execution of the functions in the class.

Thanks for the help. :blink:
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: class help please set me straight

Posted 30 September 2005 - 05:51 AM

Are you getting any errors? Or is the class just not available? Can you post
  • The section of the file hwere you are specifying libraries, namespaces, and includes.
  • The section where you instantiate an instance of the class
  • Any errors you get upon compilation or instantiation.
Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 chasboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-April 05

Re: class help please set me straight

Posted 30 September 2005 - 06:29 AM

Thanks for the reply Amadeus

I have tried to just call the function from the current page by passing the information to it. I have tried to import the class that the functions are in. I am not understanding how to do it obviously. I know this seems a little vauge but this is how my thinking is getting cloudy about the whole thing. The purpose is to validate form data. The function works if it is included in the page the form data is on but I want to use this for other programms and other forms in this app as well. I would appreciate an example in how to instantiate an instance of the function from its class. There is no inheratence.

Thank you for the reply. :huh:
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: class help please set me straight

Posted 30 September 2005 - 08:02 AM

To be honest, I'm not sure I understand the problem. Are you referring to how to include a separate file into a C# project executable? If so, then you do not include in the same manner which you would use with C++. C# compiles all CS files into one executable. If you are using the Visual Studio IDE, simply putting all the .cs files in one project will compile them all together, and make each one available to the others. If you are using a command line compiler, you simply specify all .cs files to compile into one project.

here and here provide examples of using the command line compiler syntax.

There is no specific "include" syntax...you compile them together...for example. the file I have below is Class1.cs
using System;

namespace ConsoleApplication1
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
 	 int x = 100;
 	 int y = 200;
 	 Class2 c2 = new Class2();
 	 c2.C2Display(x,y);
 	 Console.ReadLine();
  }
	}
}


and then Class2.cs
using System;
	public class Class2
	{
  public Class2()
  {
            

  }
 	 public void C2Display(int x, int y)
 	 {
    Console.WriteLine("The coordinates are {0} and{1}\n", x,y);
    }
	}


Note I have not included Class2.cs in Class1.cs, but I can instantiate an instance and access it's methods, simply by compiling them together.

Please let me know if I've misunderstood the problem.
Was This Post Helpful? 0
  • +
  • -

#10 chasboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-April 05

Re: class help please set me straight

Posted 30 September 2005 - 09:29 AM

Amadeus

I was a little confused by this example. It mentioned to import the file so you can use the class's functions. I understand what I was doing wrogn thanks for the help. Thanks a ton. This is going to help. :D
Was This Post Helpful? 0
  • +
  • -

#11 chasboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-April 05

Re: class help please set me straight

Posted 30 September 2005 - 09:45 AM

Amadeus

I was also reading the the "this" could be used as well. I assume it is in the same manner as the new Class2 c2 = new Class2(); but it would and could be used like this.Class2.C2Display(x,y);. I think that is how it would be used if I am correct in my understanding from your previous reply.

Thanks again for helping me much appreciated.
Was This Post Helpful? 0
  • +
  • -

#12 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: class help please set me straight

Posted 30 September 2005 - 10:11 AM

The this keyword is essentially a pointer that refers to the current instance of any class you may be using. It can be used to access member methods of that class from within methods, constructors, etc... The link provides an example.
Was This Post Helpful? 0
  • +
  • -

#13 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: class help please set me straight

Posted 30 September 2005 - 04:06 PM

chasboi, on Sep 30 2005, 10:26 AM, said:

Amadeus

I was a little confused by this example. It mentioned to import the file so you can use the class's functions. I understand what I was doing wrogn thanks for the help. Thanks a ton. This is going to help. :D

As Amadeus pointed out (and me as well earlier :D), C# isn't like C++: you don't need to write anything in your code to "include" things.

If you're using MSVC# v whatever, simply putting all your files into the same project will compile them all together.
Was This Post Helpful? 0
  • +
  • -

#14 chasboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-April 05

Re: class help please set me straight

Posted 30 September 2005 - 08:10 PM

Thanks again. I really appreciate the help and will take all of what you mentioned into consideration in my next adventures in VS2003.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1