INCLUDE

  • Thread starter Thread starter Dmitri Shvetsov
  • Start date Start date
D

Dmitri Shvetsov

Hi All,

Does somebody know how can I include an external file into my C# source
file?

I need to insert the same strings (about 5-10) into about 75 different
files, probably I will need to modify all these strings later, and I see it
as a good idea to use an INSERT approach, but C# is not C++ and doesn't
allow me just to insert some external file as I used to do. What's a command
for that?

Thanks,
Dmitri.
 
Dmitri said:
Hi All,

Does somebody know how can I include an external file into my C# source
file?

I need to insert the same strings (about 5-10) into about 75 different
files, probably I will need to modify all these strings later, and I see it
as a good idea to use an INSERT approach, but C# is not C++ and doesn't
allow me just to insert some external file as I used to do. What's a command
for that?

Thanks,
Dmitri.

Create a class in its own file, and add the string constants to it.

e.g.:

public sealed class StringConstants
{
// private constructor to stop it getting instantiated
private StringConstants()
{
}

public const string One = "One";
public const string Two = "Two";
}

Then later you can use StringConstants.One, etc.
 
Not so easy. I need to do the same steps during ON_LOAD for every asp page.
That's why I need to insert the same strings to compile them in different
classes. If I could use an external file and define a static variables etc.,
it would be a big difference.

Dmitri.
 
Dmitri,

Why don't you create a new class (let's call it MyPage) derived from Page, overload the OnLoad() method in MyPage, and then change the base type for every of your web pages' classes to MyPage instead of Page.

This technique is a very common technique for adding common functionality to web pages (it's almost like the master template page concept which will be available in ASP .NET "Whidbey").

--
Sorin Dolha [MCAD, MCSD .NET]
Not so easy. I need to do the same steps during ON_LOAD for every asp page.
That's why I need to insert the same strings to compile them in different
classes. If I could use an external file and define a static variables etc.,
it would be a big difference.

Dmitri.
 
Hi,

You know if it should be a very short code it would be fine, but it's only a part of the code and these few strings should be executed before the rest of the On_Load code that's different for each page and already done. So, I can't just derive the page from my own class. Although it could be a good idea and I will think about it. Maybe in this case we can avoid some extra code and just derive some common part of the code just sending the predefined constants to the constructors. Thanks.

Dmitri.
Dmitri,

Why don't you create a new class (let's call it MyPage) derived from Page, overload the OnLoad() method in MyPage, and then change the base type for every of your web pages' classes to MyPage instead of Page.

This technique is a very common technique for adding common functionality to web pages (it's almost like the master template page concept which will be available in ASP .NET "Whidbey").

--
Sorin Dolha [MCAD, MCSD .NET]
Not so easy. I need to do the same steps during ON_LOAD for every asp page.
That's why I need to insert the same strings to compile them in different
classes. If I could use an external file and define a static variables etc.,
it would be a big difference.

Dmitri.
 
It doesn't matter how much code you need to run. And you can use the method I have described even if you have different code on the web pages OnLoad methods by overriding OnLoad twice, once in the MyBasePage (template) class and once in every web page's class, like this:

class MyBasePage : Page
{
public override OnLoad(...)
{
base.OnLoad(); // call the standard Page.OnLoad()

// next, your lines which should execute on every page
}

...
}

// repeat this for every page you currently have in the web site
// basically you will only need to insert "base.OnLoad()" line, and change : Page to : MyBasePage for every web page class
class AWebPage : MyBasePage
{
public override OnLoad(...) // override
{
base.OnLoad(); //calls MyBasePage.OnLoad()

// next, your specific code for this page
}

...
}

I hope it helps,

--
Sorin Dolha [MCAD, MCSD .NET]
Hi,

You know if it should be a very short code it would be fine, but it's only a part of the code and these few strings should be executed before the rest of the On_Load code that's different for each page and already done. So, I can't just derive the page from my own class. Although it could be a good idea and I will think about it. Maybe in this case we can avoid some extra code and just derive some common part of the code just sending the predefined constants to the constructors. Thanks.

Dmitri.
Dmitri,

Why don't you create a new class (let's call it MyPage) derived from Page, overload the OnLoad() method in MyPage, and then change the base type for every of your web pages' classes to MyPage instead of Page.

This technique is a very common technique for adding common functionality to web pages (it's almost like the master template page concept which will be available in ASP .NET "Whidbey").

--
Sorin Dolha [MCAD, MCSD .NET]
Not so easy. I need to do the same steps during ON_LOAD for every asp page.
That's why I need to insert the same strings to compile them in different
classes. If I could use an external file and define a static variables etc.,
it would be a big difference.

Dmitri.
 
O, yeah, it really helps, thanks a lot!

I think that the question is closed. It's very bad that we can't import an external file as an additional code, but using this inheritance can help to solve this problem.

Dmitri.
It doesn't matter how much code you need to run. And you can use the method I have described even if you have different code on the web pages OnLoad methods by overriding OnLoad twice, once in the MyBasePage (template) class and once in every web page's class, like this:

class MyBasePage : Page
{
public override OnLoad(...)
{
base.OnLoad(); // call the standard Page.OnLoad()

// next, your lines which should execute on every page
}

...
}

// repeat this for every page you currently have in the web site
// basically you will only need to insert "base.OnLoad()" line, and change : Page to : MyBasePage for every web page class
class AWebPage : MyBasePage
{
public override OnLoad(...) // override
{
base.OnLoad(); //calls MyBasePage.OnLoad()

// next, your specific code for this page
}

...
}

I hope it helps,

--
Sorin Dolha [MCAD, MCSD .NET]
Hi,

You know if it should be a very short code it would be fine, but it's only a part of the code and these few strings should be executed before the rest of the On_Load code that's different for each page and already done. So, I can't just derive the page from my own class. Although it could be a good idea and I will think about it. Maybe in this case we can avoid some extra code and just derive some common part of the code just sending the predefined constants to the constructors. Thanks.

Dmitri.
Dmitri,

Why don't you create a new class (let's call it MyPage) derived from Page, overload the OnLoad() method in MyPage, and then change the base type for every of your web pages' classes to MyPage instead of Page.

This technique is a very common technique for adding common functionality to web pages (it's almost like the master template page concept which will be available in ASP .NET "Whidbey").

--
Sorin Dolha [MCAD, MCSD .NET]
Not so easy. I need to do the same steps during ON_LOAD for every asp page.
That's why I need to insert the same strings to compile them in different
classes. If I could use an external file and define a static variables etc.,
it would be a big difference.

Dmitri.
 
One common use of include I am looking for including file with using statements. Typically in a big C# program I have to write around 15-20 using statements on the top every file.

like-
using System;
using System.Windows.Forms;
using System.Drawing;
using blah blah blah

If I put these using statement in a single C# file and include in every C# file. Life will be cool. Same problem with Java. Any alternate to this problem?


--------------------------------------------
Manish Agarwal <[email protected]>


It doesn't matter how much code you need to run. And you can use the method I have described even if you have different code on the web pages OnLoad methods by overriding OnLoad twice, once in the MyBasePage (template) class and once in every web page's class, like this:

class MyBasePage : Page
{
public override OnLoad(...)
{
base.OnLoad(); // call the standard Page.OnLoad()

// next, your lines which should execute on every page
}

...
}

// repeat this for every page you currently have in the web site
// basically you will only need to insert "base.OnLoad()" line, and change : Page to : MyBasePage for every web page class
class AWebPage : MyBasePage
{
public override OnLoad(...) // override
{
base.OnLoad(); //calls MyBasePage.OnLoad()

// next, your specific code for this page
}

...
}

I hope it helps,

--
Sorin Dolha [MCAD, MCSD .NET]
Hi,

You know if it should be a very short code it would be fine, but it's only a part of the code and these few strings should be executed before the rest of the On_Load code that's different for each page and already done. So, I can't just derive the page from my own class. Although it could be a good idea and I will think about it. Maybe in this case we can avoid some extra code and just derive some common part of the code just sending the predefined constants to the constructors. Thanks.

Dmitri.
Dmitri,

Why don't you create a new class (let's call it MyPage) derived from Page, overload the OnLoad() method in MyPage, and then change the base type for every of your web pages' classes to MyPage instead of Page.

This technique is a very common technique for adding common functionality to web pages (it's almost like the master template page concept which will be available in ASP .NET "Whidbey").

--
Sorin Dolha [MCAD, MCSD .NET]
Not so easy. I need to do the same steps during ON_LOAD for every asp page.
That's why I need to insert the same strings to compile them in different
classes. If I could use an external file and define a static variables etc.,
it would be a big difference.

Dmitri.
 
Hi,

I have no any fresh idea, MSDN keeps silence, there is no official way to include some external text files to be compiled as a part of a source code.

I just notice one thing - if you already included some USING namespace in your class and later you included this class into your next file all namespaces included before are accessible.

Dmitri.
One common use of include I am looking for including file with using statements. Typically in a big C# program I have to write around 15-20 using statements on the top every file.

like-
using System;
using System.Windows.Forms;
using System.Drawing;
using blah blah blah

If I put these using statement in a single C# file and include in every C# file. Life will be cool. Same problem with Java. Any alternate to this problem?


--------------------------------------------
Manish Agarwal <[email protected]>


It doesn't matter how much code you need to run. And you can use the method I have described even if you have different code on the web pages OnLoad methods by overriding OnLoad twice, once in the MyBasePage (template) class and once in every web page's class, like this:

class MyBasePage : Page
{
public override OnLoad(...)
{
base.OnLoad(); // call the standard Page.OnLoad()

// next, your lines which should execute on every page
}

...
}

// repeat this for every page you currently have in the web site
// basically you will only need to insert "base.OnLoad()" line, and change : Page to : MyBasePage for every web page class
class AWebPage : MyBasePage
{
public override OnLoad(...) // override
{
base.OnLoad(); //calls MyBasePage.OnLoad()

// next, your specific code for this page
}

...
}

I hope it helps,

--
Sorin Dolha [MCAD, MCSD .NET]
Hi,

You know if it should be a very short code it would be fine, but it's only a part of the code and these few strings should be executed before the rest of the On_Load code that's different for each page and already done. So, I can't just derive the page from my own class. Although it could be a good idea and I will think about it. Maybe in this case we can avoid some extra code and just derive some common part of the code just sending the predefined constants to the constructors. Thanks.

Dmitri.
Dmitri,

Why don't you create a new class (let's call it MyPage) derived from Page, overload the OnLoad() method in MyPage, and then change the base type for every of your web pages' classes to MyPage instead of Page.

This technique is a very common technique for adding common functionality to web pages (it's almost like the master template page concept which will be available in ASP .NET "Whidbey").

--
Sorin Dolha [MCAD, MCSD .NET]
Not so easy. I need to do the same steps during ON_LOAD for every asp page.
That's why I need to insert the same strings to compile them in different
classes. If I could use an external file and define a static variables etc.,
it would be a big difference.

Dmitri.
 
Back
Top