Solved

using Classes

Posted on 2009-05-11
13
132 Views
Last Modified: 2012-05-06
Is it possible to use the public string in a class in aspx pages without declaring a class in the aspx page?

I am trying to have less lines of code

        AceClasses aClass = new AceClasses();
        string connString = aClass.GetConnString();

How can I write the above in one line only?
0
Comment
Question by:goodk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 12

Expert Comment

by:rgn2121
ID: 24361074
You could create your AceClasses to take a string parameter when you declare and instantiate it by overloading your New()..
I don't use a lot of C#, but the New() could look something lke what is below.

Public Sub New( byval s as string)
 
verify 's' and set it equal to the string in your class...
 
End Sub
 
'''Then you could delare it by doing this...If GetConString returns a string...
dim aClass as New AceClasses(GetConnString())

Open in new window

0
 

Author Comment

by:goodk
ID: 24364553
so how it is one line?
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24370983
In VB, your one line is the :
Dim aClass as New AceClasses(GetConnString())

The only way I know to make it one line is if your NEW() constructor takes a parameter.  If it doesn't then I don't think there is anything you can do.  That's why I said to modify your class so that you can declare and instantiate your class in one line as well as provide the connection string.
 
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:goodk
ID: 24381647
Sorry I am not familiar with vb, I use c#.

        AceClasses aClass = new AceClasses();
        string connString = aClass.GetConnString();

How would I combine the above into one line
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24381945
Following what I have stated about overloading your New() constructor, you would be able to use the following code:
AceClasses aClass = new AceClasses(GetConnString());  
 If not...since GetConnString() is a method in your AceClasses class then you have to declare a variable as AceClasses to be able to use the GetConnString().   Which would mean your code is as good as it gets...although not ideal.
Unless, you have created the GetConnString as shared (I think this is static in C#)...then you could use code similar to the line below.
AceClasses aClass = AceClasses.GetConnString();  
This is assuming your GetConnString() is similar to what is below:
VB:
Public Shared Function GetConnString() as String
'Code to get the connection string
End Function
 C# by way of a converter.   http://www.developerfusion.com/tools/convert/vb-to-csharp/

Public Shared Function GetConnString() as String
'Code to get the connection string
End Function
 
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24381953
Sorry...That last section of code should have been in C#..
public static string GetConnString()
 {    
//Code to get the connection string
}
 
0
 

Author Comment

by:goodk
ID: 24384492
AceClasses aClass = new AceClasses();
        string connString = aClass.GetConnString();

so you are saying I could write the above as

AceClasses connString = new AceClasses(GetConnString());

??
 
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24391860
Unless I am completely misunderstanding, you are trying to delare a class of type AceClasses correct?
I assumed that you were trying to get a connection string to use with the class you just created.  If that is not the case, then I am confused by what you are doing.  
The top section of code in the window below is declaring aClass as an AceClasses Type.  Then you are declaring a string and setting it equal to the string returned by the AceClasses function GetConnString()..
The bottom section of code is totally different...  In it you are declaring connString as type AceClasses and setting it equal to the string returned by GetConnString();  I doubt that you want to or you are able to set your AceClasses object equal to a string.

AceClasses aClass = new AceClasses();
        string connString = aClass.GetConnString();
 
 
 
AceClasses connString = new AceClasses(GetConnString());

Open in new window

0
 
LVL 12

Expert Comment

by:rgn2121
ID: 24391877
I know nothing about AceClasses, but I would assume you have created a class called AceClasses.  In this class there is a function that returns a string called GetConnString().  Is this correct?
If the above is true, then my next question is do you need to declare an object of type AceClasses or do you just want to get the string returned by GetConnString() into a different string variable?
0
 

Author Comment

by:goodk
ID: 24392062
first paragrph -- YES

second paragraph -- True, all I want is move the return string into a local variable.  Is it possible to do it in one line. If possible, how?

0
 
LVL 12

Accepted Solution

by:
rgn2121 earned 500 total points
ID: 24392392
Yes, but your GetConnString() will have to be declared as Static so that you don't have to create an instance of AceClasses to use it.
Like I stated above your code for GetConnString() would have to look something like what I have below:

public static string GetConnString()
{    
//Code to get the connection string
}
If it does, then you should be able to do the following:
 string connString = AceClasses.GetConnString();

Like I stated early...I really don't use C#, but I think this code works.  I use this all the time in VB.  I have numerous classes that have "Shared" members.  This way I don't have to create an instance of the class to use the functions, properties, methods, etc that it contains.  The "Static" keyword is used in C#...
If you use private or class level variables inside of GetConnString, then those will need to be declared Static as well or you will get an error about access to a Static member...
0
 

Author Comment

by:goodk
ID: 24395713
works, thanks - I think the trick was to declare the string as static.  I do not have a need to manupulate the string externally anyway.

thanks again - bye
0
 

Author Closing Comment

by:goodk
ID: 31580395
thanks.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this short web based tutorial, I wanted to show users how they can still use the powers of FrontPage in conjunction with Expression Web 3.  Even though Microsoft eliminated the use of Web components, we can still use them with FrontPage and edit …
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question