Solved

using Classes

Posted on 2009-05-11
13
128 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
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

When deciding to adopt any help desk solutions many factors should be explored before taking decisions. This will change from business to another but in general there are some kind of rule of thumb. Here are some quick tips: Do we need only ticket…
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…

770 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